I have the following code to create a func expression to access a class property
public static Expression<Func<TObj, TProperty>> BuildGet<TObj, TProperty>(PropertyInfo property)
{
Type type = typeof(TObj);
ParameterExpression arg = Expression.Parameter(type, "x");
var prop = Expression.Property(arg, property);
return Expression.Lambda<Func<TObj, TProperty>>(prop, arg);
}
The problem is that I have to go through TObjand TProperty, even if they are known (properties in PropertyInfoclass - .DeclaringTypeand .PropertyType).
Is there any way around this, so I should not explicitly pass them?
The return type must remain as Expression<Func<TObj, TProperty>>
source
share