Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何组合两个表达式:result=exp1(exp2);_.net_Linq_Lambda_Expression - Fatal编程技术网

.net 如何组合两个表达式:result=exp1(exp2);

.net 如何组合两个表达式:result=exp1(exp2);,.net,linq,lambda,expression,.net,Linq,Lambda,Expression,作为主题,在这种情况下,如何将两个表达式组合成一个表达式: Expression<Func<IEnumerable<T>, IEnumerable<T>>> exp1; Expression<Func<IEnumerable<T>, IEnumerable<T>>> exp2; Expression<Func<IEnumerable<T>, IEnumerable<T&

作为主题,在这种情况下,如何将两个表达式组合成一个表达式:

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

Expression<Func<IEnumerable<T>, IEnumerable<T>>> result = ???; // exp1(exp2)
表达式exp1;
表达谱exp2;
表达式结果=???;//exp1(exp2)

这实际上只是两个
表达式
值组合的一种特定形式。下面是一个这样做的示例:

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

“new[]{invokedSecond}”是什么意思?它是否创建了invokedSecond类型的数组?或者是一个包含单个invokedSecond项的对象数组?它是一个隐式类型的数组,根据元素的静态类型键入-在本例中,它相当于
newexpress[]{invokedSecond}
    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);
    }