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

Popular posts from this blog

how to do line continuation in perl debugger for entering raw multi-line text (EOT)? -

javascript - Create websocket without connecting -

android - Linear layout children not scrolling -