Is there a way (in c# .Net 4.0) to provide object property name in strongly type manner? For instance, if we have an object Person
Public class Person{
public string Name { get; set; }
public int Age { get; set; }
}
And I want to sent to some other method the parameters Name, Age property name but not as string but strongly type:
SomeMethodThatWantToKnowKeys(Person.Name,Person.Age);
What I want to achieve is that if someone change the property name, he will have to change they property names he is sending to 'SomeMethodThatWantToKnowKeys'. Maybe reflection? A better way will be without updating the object itsef or creating an instance of it.
Okay so while there are some ugly hacks there is a much better and clearner solution. This problem is because the abstraction is not in place.
What is a name? A String? It could be an int, double, char, float....anything...you do not really care about the underlying type. You care about the concept or notion of a name. I know this is a bit more deep, but this experience will help you with good design.
Here is what I suggest for now, but personally I might go do more.
So now your method signature is:
Type safety is an amazing thing!
If I understand what you want, you could use expressions: