件名として、この場合の 2 つの式を 1 つの式に結合する方法は次のとおりです。
Expression<Func<IEnumerable<T>, IEnumerable<T>>> exp1;
Expression<Func<IEnumerable<T>, IEnumerable<T>>> exp2;
Expression<Func<IEnumerable<T>, IEnumerable<T>>> result = ???; // exp1(exp2)
件名として、この場合の 2 つの式を 1 つの式に結合する方法は次のとおりです。
Expression<Func<IEnumerable<T>, IEnumerable<T>>> exp1;
Expression<Func<IEnumerable<T>, IEnumerable<T>>> exp2;
Expression<Func<IEnumerable<T>, IEnumerable<T>>> result = ???; // exp1(exp2)
Expression<Func<T, T>>
これは実際には、2 つの値を組み合わせた特定の形式にすぎません。これを行う例を次に示します。
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));
}
}
ApplySequence
そうしたい場合は、これらの用語で簡単に書くことができます。
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);
}