I have the following code to create an expression of a func for accessing a property of a class
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, is that I have to pass in TObj
and TProperty
even though they are known (properties on PropertyInfo
class - .DeclaringType
and .PropertyType
).
Is there a way around this so I don't explicitly have to pass them in?
The return type must remain as Expression<Func<TObj, TProperty>>