次の設定を検討してください。
class A { public int x; }
class B { public int y; }
static class Helper
{
public static Expression<Func<B>> BindInput(
Expression<Func<A, B>> expression,
A input)
{
//TODO
}
}
static void Main(string[] args)
{
Expression<Func<B>> e = Helper.BindInput(
(A a) => new B { y = a.x + 3 },
new A { x = 4 });
Func<B> f = e.Compile();
Debug.Assert(f().y == 7);
}
このメソッドでやりたいのBindInput
は、式を変換してinput
埋め込まれるようにすることです。の使用例でMain
は、結果の式e
は次のようになります。
() => new B { y = input.x + 3 }
ここinput
で、はに渡された2番目の値ですBindInput
。
どうすればこれを行うことができますか?
編集:
次の式e
は私が探しているものではないことを追加する必要があります。
((A a) => new B { y = a.x + 3 })(input)
これは、既存の式の上にレイヤーを追加するだけなので、取得するのはかなり簡単です。