views:

43

answers:

1

I have a question very similiar to another question: http://stackoverflow.com/questions/2820660/get-name-of-property-as-a-string.

His solution ended up with

// Static Property 
string name = GetPropertyName(() => SomeClass.SomeProperty); 

// Instance Property 
string name = GetPropertyName(() => someObject.SomeProperty); 

What I'd like is to have syntax similar to the Static Property but for an instance property.

The reason is that I have code now that uses reflection to get the value of a property for all objects within a collection, but i have to pass that in as a hardcoded string.

Example code:

double Sum = AmountCollection.Sum("thatfield");  

Well, this works great, but if "thatfield" was ever renamed, the code would no longer work. The compiler cant check for that since it's just a string. Also, Get References won't work either for the same reason.

So, is there a way to achieve the goal of getting a property name easily, (ie; just a function call), from an instance property?

Thanks.

+1  A: 

Try this:

string name = GetPropertyName(() => default(SomeClass).SomeInstanceProperty);

You may get a compiler warning about "always causing a System.NullReferenceException", but that's not actually happening, since you don't execute the expression, which means that you can safely discard this warning. If you want to get rid of it, either disable it via pragma or just move the default() call into a function like this:

public static T Dummy<T>() {
  return default(T);
}

string name = GetPropertyName(() => Dummy<SomeClass>().SomeInstanceProperty);
Lucero
oh, I like that. It does work, just tested it out. I'm still creating an object, but it's much cleaner indeed. Thanks.
David
@David, no, you're NOT creating an object. `default()` is `null` for all reference types, and an empty value for value types (no constructor is being called, no heap memory is being allocated).
Lucero
@ChrisF, as I wrote, this warning is correct if the expression was ever executed. However, it is only used to extract metadata, so that this is not happening.
Lucero
Now, the solution from the other question required the Lambda expression and that extra function, is it possible to do something that looks kinda like:string NameOfProperty = typeof(default(AccountEntity).Email).Name;This code doesnt work, but something similar?
David
I misspoke about creating an object, didnt mean that exactly, more like it's doing additional work related to the object. But, in either case, it does work well and is clean, but see my last post?
David
@David, you have make the compiler build an `Expression` instance in order to retrieve the metadata of the expression. Therefore there is currently no other way than to go through a lambda expression. Don't worry about the "extra work" however, the overhead is minimal (since this is the same technique as used for all the LINQ stuff, which gets done pretty efficiently by the compiler).
Lucero
ok, thanks for the info. Part of it is just limiting the actual text that needs to be written to perform what I want. But your solution works, and I'm quite content with that.
David
+1, exactly what I would have suggested ;)
Thomas Levesque