特定のオブジェクトに複数のプロパティを割り当てる Expression を構築しようとしていますが、コンパイルされたデリゲートを呼び出した後NullReferenceException
、ラムダの本体内からa をスローし続けます。
var a = Expression.Parameter(typeof(A), "a");
var b = Expression.Parameter(typeof(B), "b");
var c = Expression.Parameter(typeof(C), "c");
LabelTarget returnTarget = Expression.Label(typeof(A));
GotoExpression returnExpression =
Expression.Return(returnTarget, a, typeof(A));
LabelExpression returnLabel = Expression.Label(
returnTarget, Expression.Constant(null, typeof(A)));
var expression = Expression.Lambda<Func<A, B, C, A>>(
Expression.Block(
typeof(A),
new[] { a, b, c },
Expression.Assign(
Expression.Property(a, typeof(A).GetProperty("B")),
b),
Expression.Assign(
Expression.Property(a, typeof(A).GetProperty("C")),
c),
returnExpression,
returnLabel
),
a, b, c);
Func<A, B, C, A> func = expression.Compile();
// Calling func throws a NullReferenceException
A result = func(new A(), new B(), new C());
私は間違って書いたと思いますが、Expression.Block
ここで何が間違っているのでしょうか?