【问题标题】:Use lambda return value for expression tree对表达式树使用 lambda 返回值
【发布时间】:2013-09-25 14:41:55
【问题描述】:

我尝试使用表达式树。 我有一个带有List<string> 的对象,我想构建一个表达式树,为该属性添加一个值,但我想通过Func 指定要添加的值。 目前我正在尝试这个......

public static Action<T> CreateMethodAddObjectToList<T, C>(this Type type, string property, Func<C> ctorFunction)
        {
            PropertyInfo fieldInfo = type.GetProperty(property);

            if (fieldInfo == null)
            {
                return null;
            }

            ParameterExpression targetExp = Expression.Parameter(type, "target");
            MemberExpression fieldExp = Expression.Property(targetExp, property);
            var method = fieldExp.Type.GetMethod("Add", BindingFlags.Public | BindingFlags.Instance);

            Expression<Func<C>> ctorExpression = () => ctorFunction();

// but this doesnt work because I can't use the ctorExpression in this way
            var callExp = Expression.Call(fieldExp, method, ctorExpression);

            var function = Expression.Lambda<Action<T>>(callExp, targetExp).Compile();

            return function;
        }

电话看起来像

var dummyObject = new DummyObject { IntProperty = 5 };

            Action<DummyObject> setter = typeof (DummyObject).CreateMethodAddObjectToList<DummyObject, string>("StringList", () => "Test" );

【问题讨论】:

    标签: c# lambda expression-trees


    【解决方案1】:

    您可以将 ctorFunction 更改为 Expression&lt;Func&lt;C&gt;&gt;,然后在生成的操作中调用它:

    public static Action<T> CreateMethodAddObjectToList<T, C>(this Type type, string property, Expression<Func<C>> createExpr)
    {
        PropertyInfo fieldInfo = type.GetProperty(property);
    
        if (fieldInfo == null)
        {
            return null;
        }
    
        ParameterExpression targetExp = Expression.Parameter(type, "target");
        MemberExpression fieldExp = Expression.Property(targetExp, property);
        var method = fieldExp.Type.GetMethod("Add", BindingFlags.Public | BindingFlags.Instance);
    
        var valueExpr = Expression.Invoke(createExpr);
        var callExpr = Expression.Call(fieldExp, method, valueExpr);
    
        var function = Expression.Lambda<Action<T>>(callExpr, targetExp).Compile();
    
        return function;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多