C# 对表达式树使用lambda返回值

C# 对表达式树使用lambda返回值,c#,lambda,expression-trees,C#,Lambda,Expression Trees,我试着玩一下表达式树。 我有一个具有列表的对象,我想构建一个表达式树,向该属性添加一个值,但我想通过Func指定要添加的值。 目前我尝试这个 public static Action<T> CreateMethodAddObjectToList<T, C>(this Type type, string property, Func<C> ctorFunction) { PropertyInfo fieldInfo = t

我试着玩一下表达式树。 我有一个具有
列表的对象,我想构建一个表达式树,向该属性添加一个值,但我想通过
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;
        }
公共静态操作CreateMethodAddObjectToList(此类型、字符串属性、函数)
{
PropertyInfo fieldInfo=type.GetProperty(属性);
如果(fieldInfo==null)
{
返回null;
}
ParameterExpression targetExp=表达式。参数(类型“target”);
MemberExpression fieldExp=Expression.Property(targetExp,Property);
var method=fieldExp.Type.GetMethod(“添加”,BindingFlags.Public | BindingFlags.Instance);
表达式ctorExpression=()=>ctorFunction();
//但是这不起作用,因为我不能用这种方式使用这个表达式
var callExp=Expression.Call(fieldExp、方法、表达式);
var function=Expression.Lambda(callExp,targetExp.Compile();
返回函数;
}
这个电话看起来像

var dummyObject = new DummyObject { IntProperty = 5 };

            Action<DummyObject> setter = typeof (DummyObject).CreateMethodAddObjectToList<DummyObject, string>("StringList", () => "Test" );
var dummyObject=new dummyObject{IntProperty=5};
Action setter=typeof(DummyObject).CreateMethodAddObjectToList(“StringList”,“测试”);

您可以将函数更改为
表达式,然后在生成的操作中调用它:

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;
}
公共静态操作CreateMethodAddObjectToList(此类型、字符串属性、表达式createExpr)
{
PropertyInfo fieldInfo=type.GetProperty(属性);
如果(fieldInfo==null)
{
返回null;
}
ParameterExpression targetExp=表达式。参数(类型“target”);
MemberExpression fieldExp=Expression.Property(targetExp,Property);
var method=fieldExp.Type.GetMethod(“添加”,BindingFlags.Public | BindingFlags.Instance);
var valueExpr=Expression.Invoke(createExpr);
var callExpr=Expression.Call(fieldExp,method,valueExpr);
var function=Expression.Lambda(callExpr,targetExp.Compile();
返回函数;
}