For properties there are GetGetMethod and GetSetMethod so that I can do:
Getter = (Func)Delegate.CreateDelegate(typeof(
Just to add more ways of doing it :D
public static Func CreatePropertyOrFieldReaderDelegate(string field)
{
var input = Expression.Parameter(typeof(T));
return Expression.Lambda>(Expression.PropertyOrField(input, field), input).Compile();
}
This will create a method that returns the value..
TEST CASE
class Testing {
public int Data = 2;
public string PropData { get; } = "Default";
}
[Fact]
public void CreateSingleFieldReader()
{
var a = ReflectionHelper.CreatePropertyOrFieldReaderDelegate("Data");
Assert.Equal(2, a(new Testing()));
}