如何将两个前pressions:结果= EXP1(EXP2); [英] How to combine two expressions: result = exp1(exp2);

查看:186
本文介绍了如何将两个前pressions:结果= EXP1(EXP2);的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为主体,如何两位前$​​ P $ pssions合并成一个单一的这种情况:

 防爆pression< Func键< IEnumerable的< T&GT ;,的IEnumerable< T>>> EXP1;
防爆pression< Func键< IEnumerable的< T&GT ;,的IEnumerable< T>>> EXP2;

防爆pression< Func键< IEnumerable的< T&GT ;,的IEnumerable< T>>>结果= ???; // EXP1(EXP2)
 

解决方案

这是真正将两个防爆pression&LT只是一种具体形式; Func键< T,T>> 值。下面是这样做的一个例子:

 使用系统;
使用System.Linq.Ex pressions;

公共类测试
{
    公共静态防爆pression< Func键< T,T>>应用< T>
        (出pression< Func键< T,T>>首先,防爆pression< Func键< T,T>>第二个)
    {
        ParameterEx pression输入=前pression.Parameter(typeof运算(T),输入);
        防爆pression invokedSecond =前pression.Invoke(第二,
                                                     新的前pression [] {输入});
        防爆pression invokedFirst =前pression.Invoke(第一,
                                                    新的[] {invokedSecond});
        返回前pression.Lambda< Func键< T,T>>(invokedFirst,新的[] {输入});
    }

    静态无效的主要()
    {
        VAR addAndSquare =应用<诠释>(X => X + 1,
                                      X => X * X);

        Console.WriteLine(addAndSquare.Compile()(5));
    }
}
 

您可以写 ApplySequence 在这些方面很容易,如果你想:

 公共静态防爆pression< Func键< IEnumerable的< T&GT ;,的IEnumerable< T>>>
         ApplySequence< T>
            (出pression< Func键< IEnumerable的< T&GT ;,的IEnumerable< T>>>首先,
             防爆pression< Func键< IEnumerable的< T&GT ;,的IEnumerable< T>>>第二)
    {
        返回应用(一,二);
    }
 

As subject, how to combine two expressions into a single one for this case:

Expression<Func<IEnumerable<T>, IEnumerable<T>>> exp1;
Expression<Func<IEnumerable<T>, IEnumerable<T>>> exp2;

Expression<Func<IEnumerable<T>, IEnumerable<T>>> result = ???; // exp1(exp2)

解决方案

This is really just a specific form of combining two Expression<Func<T, T>> values. Here's an example of doing that:

using System;
using System.Linq.Expressions;

public class Test
{
    public static Expression<Func<T, T>> Apply<T>
        (Expression<Func<T, T>> first, Expression<Func<T, T>> second)
    {
        ParameterExpression input = Expression.Parameter(typeof(T), "input");
        Expression invokedSecond = Expression.Invoke(second,
                                                     new Expression[]{input});
        Expression invokedFirst = Expression.Invoke(first,
                                                    new[]{invokedSecond});
        return Expression.Lambda<Func<T, T>>(invokedFirst, new[]{input});
    }

    static void Main()
    {
        var addAndSquare = Apply<int>(x => x + 1,
                                      x => x * x);

        Console.WriteLine(addAndSquare.Compile()(5));
    }
}

You could write ApplySequence in those terms easily, if you wanted to:

    public static Expression<Func<IEnumerable<T>, IEnumerable<T>>>
         ApplySequence<T>
            (Expression<Func<IEnumerable<T>, IEnumerable<T>>> first,
             Expression<Func<IEnumerable<T>, IEnumerable<T>>> second)
    {
        return Apply(first, second);
    }

这篇关于如何将两个前pressions:结果= EXP1(EXP2);的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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