我如何编译一个Ex pression树成一个可调用的方法,C#? [英] How do I compile an Expression Tree into a callable method, C#?

查看:87
本文介绍了我如何编译一个Ex pression树成一个可调用的方法,C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我已经使用在C#中前pression类解析XML创建的前pression树。 <一href=\"http://stackoverflow.com/questions/344741/how-do-i-create-an-ex$p$pssion-tree-by-parsing-xml-in-c\">See这个问题。

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

我只有加,减,除,乘,参数,并且还是在我的前pression树。
有没有一种办法,以这种防爆pressionTree转换成可调用的方法?
......还是我不得不手动发射IL?

I only have Add, Subtract, Divide, Multiply, Parameters, And and Or in my Expression Tree. Is there a way to convert this ExpressionTree into a callable method? ...or do I have to emit the IL manually?

亲切的问候,

推荐答案

下面是这两种方法的一个例子。如果我错过了一些东西,或者你想了解更多信息,只是让我知道。

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<decimal>();
    var composite = BuildComposite<decimal>();

    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, TDestination>(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<T,T> BuildSingle<T>()
{        
    var param = Expression.Parameter(typeof(T), "x");
    Expression body = Expression.Add(param, Expression.Multiply(
        ConvertConstant<int, T>(3), param));
    var lambda = Expression.Lambda<Func<T, T>>(body, param);
    return lambda.Compile();
}
// option 2: nested expression trees:
static Func<T, T> BuildComposite<T>()
{

    // step 1: do the multiply:
    var paramInner = Expression.Parameter(typeof(T), "inner");
    Expression bodyInner = Expression.Multiply(
        ConvertConstant<int, T>(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<Func<T, T>>(bodyOuter, paramOuter);

    return lambdaOuter.Compile();
}

我个人的目标是朝着第一种方法;它,它更简单,更有效的。这可能涉及整个传递嵌套code一叠原始参数,但就这样吧。我有一些code的地方,采取了调用的方式(复合),并重新写入树作为第一种方法(单) - 但它是相当复杂和漫长。但对于实体框架非常有用的(它不支持前pression.Invoke)。

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).

这篇关于我如何编译一个Ex pression树成一个可调用的方法,C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆