Pregunta

Tengo un árbol de expresión que he creado al analizar un Xml usando la clase de expresión en C #. Consulte esta pregunta .

Solo tengo Agregar, Restar, Dividir, Multiplicar, Parámetros, Y y O en mi Árbol de expresión. ¿Hay alguna manera de convertir este ExpressionTree en un método invocable? ... o tengo que emitir la IL manualmente?

Saludos cordiales,

¿Fue útil?

Solución

Aquí hay un ejemplo de ambos enfoques. Si me he perdido algo, o si quieres más información, avísame.

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();
}

Personalmente, apuntaría al primer método; Es más simple y más eficiente. Esto podría implicar pasar el parámetro original a través de una pila de código anidado, pero que así sea. Tengo un código en algún lugar que toma la "invocación" enfoque (compuesto), y reescribe el árbol como primer enfoque (único), pero es bastante complejo y largo. Pero muy útil para Entity Framework (que no admite Expression.Invoke).

Otros consejos

Necesita crear una lambda, es decir,

var lambda = Expression.Lambda<Func<float,int>>(body, param);
Func<float,int> method = lambda.Compile();
int v = method(1.0); // test

donde " cuerpo " es su árbol de expresión (tomando un flotador, devolviendo un int) que involucra el parámetro ParameterExpression .

También puede encontrar this y esto útil.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top