c# - Expression.Body as MemberExpression returns null for primitive property -
i using code set value of property via reflection :
public static void set<t>(this t target, expression<func<t, object>> memberlamda, object value) { var memberselectorexpression = memberlamda.body memberexpression; if (memberselectorexpression != null) { var property = memberselectorexpression.member propertyinfo; if (property != null) { property.setvalue(target, value, null); } } }
but reason when :
myobject.set(x=>x.id, 1);
where id of type int, can see memberselectorexpression null. have no issue properties of reference type.
i not familiar yet expression trees, doing wrong ?
the solution use following signature :
public static void set<t, tprop>(this t target, expression<func<t, tprop>> memberlamda, tprop value)
to make sure memberexpression correctly inferred. "object" generic constraint not specific enough.
Comments
Post a Comment