动态创建表达式树

时间:2016-08-29 13:27:11

标签: c# lambda expression-trees

我有一个想法,想知道它是否可行。 我有一个带属性的简单类,并希望使用表达式生成访问器。 但最后我需要得到Func<Test, string>但我不知道使用它们时的类型。 一个小例子

class Program
{
    static void Main(string[] args)
    {
        Test test = new Test();
        test.TestString = "Blubb";
        var actionStub = typeof(Helper).GetMethod("CreatePropertyGetter").MakeGenericMethod(new Type[] { test.GetType(), typeof(string)});
        dynamic action = actionStub.Invoke(null, new object[] {test.GetType(), "TestString"});

        var x = action(test);
    }
}

public class Test
{
    public string TestString { get; set; }
}

public static class Helper
{
    public static Func<TType, TPropValueType> CreatePropertyGetter<TType, TPropValueType>(Type type,
                                                                                  string propertyName)
    {
        PropertyInfo fieldInfo = type.GetProperty(propertyName);
        ParameterExpression targetExp = Expression.Parameter(typeof(TType), "target");

        MemberExpression fieldExp = Expression.Property(targetExp, fieldInfo);
        UnaryExpression assignExp = Expression.Convert(fieldExp, typeof(TPropValueType));

        Func<TType, TPropValueType> getter =
            Expression.Lambda<Func<TType, TPropValueType>>(assignExp, targetExp).Compile();

        return getter;
    }
}

问题是我无法在没有动态的情况下调用表达式,因为我不能简单地将其转换为Func<object, object>

2 个答案:

答案 0 :(得分:2)

您正在生成(TType target) => target.Something。而是生成(object target) => (object)((TType)target).Something,以便您可以使用Func<object, object>

答案 1 :(得分:0)

目前还不清楚你究竟要求的是什么,但这是一个如何制作它的例子Func<object, object>

public static class Helper
{
    public static Func<object, object> CreatePropertyGetter(Type type, string propertyName)
    {
        var fieldInfo = type.GetProperty(propertyName);
        var targetExp = Expression.Parameter(typeof(object), "target");
        var fieldExp = Expression.Property(Expression.ConvertChecked(targetExp, type), fieldInfo);
        var getter = Expression.Lambda<Func<object, object>>(fieldExp,targetExp).Compile();
        return getter;
    }
}