Expression.Body as MemberExpression returns null f

2020-07-27 05:36发布

I am using this code to set the value of a property via reflection :

public static void Set<T>(this T target, Expression<Func<T, object>> memberLamda, object value)
{
    var memberSelectorExpression = memberLamda.Body as MemberExpression;
    if (memberSelectorExpression != null)
    {
        var property = memberSelectorExpression.Member as PropertyInfo;
        if (property != null)
        {
            property.SetValue(target, value, null);
        }
    }
}

But for some reason when I do :

myObject.Set(x=>x.ID, 1);

Where ID is of type int, I can see that memberSelectorExpression is null. However I have no issue with properties of a reference type.

I am not very familiar yet with expression trees, what I am doing wrong ?

标签: c# linq lambda
2条回答
等我变得足够好
2楼-- · 2020-07-27 05:54

The solution is to use the following signature :

public static void Set<T, TProp>(this T target, Expression<Func<T, TProp>> memberLamda, 
  TProp value)

To make sure a MemberExpression is correctly inferred. The "object" generic constraint is not specific enough.

查看更多
手持菜刀,她持情操
3楼-- · 2020-07-27 06:03

The thing to be aware of is that your expression body will most likely be wrapped in a Convert expression, representing the fact that your property is being implicitly cast as an object. So you'll probably need code something like this in your Setmethod.

var expressionBody = memberLamda.Body;
if (expressionBody is UnaryExpression expression && expression.NodeType == ExpressionType.Convert)
{
    expressionBody = expression.Operand;
}
var memberSelectorExpression = (MemberExpression)expressionBody;
查看更多
登录 后发表回答