We have code that sets the name of the property, uses reflection to implement Comparer.
I would like to keep the delegate / Func to get the value, and not pay the reflection price every time we need to get the value.
For a class like this:
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
I tried to write a function that would create a delegate for me
Func<T, object> CreateGetFuncFor<T>(string propertyName)
{
PropertyInfo prop = typeof(T).GetProperty(propertyName);
return (Func<T, object>)Delegate.CreateDelegate(typeof(Func<T, object>),
null,
prop.GetGetMethod());
}
The following code is great for getting a name
var person = new Person { Name = "Dave", Age = 42 };
var funcitonToGetName = CreateGetFuncFor<Person>("Name");
Console.WriteLine(funcitonToGetName(person));
var functionToGetAge = CreateGetFuncFor<Person>("Age");
but for Age proerty it throws an ArgumentException with the message "Target method binding failed"
What am I missing? Is there any other way to do this?
Argos source
share