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.