I could have a method like this:
public void MyMethod<T, TResult>( string propertyName, ... ){
var name = propertyName;
return {property with correct name from some object that is of type TResult}
}
And call it like this:
MyMethod<SomeClass>("SomePropertyName");
To get hold of the propertyname inside the method. However, I do not like sending that propertyname in as a string in case SomeClass
changes in the future, and the compiler cannot verify that the propertyName matches a property of type TResult
either.
I would much rather call it like this:
MyMethod<SomeClass>(c => c.SomePropertyName);
But I am unsure how my method would look like then. I have tried variants of this:
public void MyMethod<T>( Func<T,TResult> property, ... ){
var name = {do some cleverness on property here to extract the actual name of the property inside the expression};
return {property with correct name from some object that is of type TResult}
}
Are there any good clean way to do this in C#?