How do I compile an Expression Tree into a callable method, C#?

后端 未结 2 1735
青春惊慌失措
青春惊慌失措 2020-12-09 05:23

I have an expression tree I have created by parsing an Xml using the expression class in C#. See this question.

I only have Add, Subtract, Divide, Multiply, Paramete

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 06:14

    Here's an example of both approaches. If I have missed something, or you want more information, just let me know.

    static void Main()
    {
        // try to do "x + (3 * x)"
    
        var single = BuildSingle();
        var composite = BuildComposite();
    
        Console.WriteLine("{0} vs {1}", single(13.2M), composite(13.2M));
    }
    // utility method to get the 3 as the correct type, since there is not always a "int x T"
    static Expression ConvertConstant(TSource value)
    {
        return Expression.Convert(Expression.Constant(value, typeof(TSource)), typeof(TDestination));
    }
    // option 1: a single expression tree; this is the most efficient
    static Func BuildSingle()
    {        
        var param = Expression.Parameter(typeof(T), "x");
        Expression body = Expression.Add(param, Expression.Multiply(
            ConvertConstant(3), param));
        var lambda = Expression.Lambda>(body, param);
        return lambda.Compile();
    }
    // option 2: nested expression trees:
    static Func BuildComposite()
    {
    
        // step 1: do the multiply:
        var paramInner = Expression.Parameter(typeof(T), "inner");
        Expression bodyInner = Expression.Multiply(
            ConvertConstant(3), paramInner);
        var lambdaInner = Expression.Lambda(bodyInner, paramInner);
    
        // step 2: do the add, invoking the existing tree
        var paramOuter = Expression.Parameter(typeof(T), "outer");
        Expression bodyOuter = Expression.Add(paramOuter, Expression.Invoke(lambdaInner, paramOuter));
        var lambdaOuter = Expression.Lambda>(bodyOuter, paramOuter);
    
        return lambdaOuter.Compile();
    }
    

    Personally, I would aim towards the first method; it it both simpler and more efficient. This might involve passing the original parameter throughout a stack of nested code, but so be it. I have got some code somewhere that takes the "Invoke" approach (composite), and re-writes the tree as the first approach (single) - but it is quite complex and long. But very useful for Entity Framework (which doesn't support Expression.Invoke).

提交回复
热议问题