0
我试图构建多个属性分配给特定对象的表达式,但在调用编译代表,它不断从拉姆达身体内投掷NullReferenceException
后:代表与Expression.Block编译不断抛出的NullReferenceException
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
,但是我在这里做错了什么?
知道哪一行引发异常会很有用。 – Jon 2013-02-13 15:43:43
@Jon:在最后一行看到我的评论:“//调用func抛出一个NullReferenceException”。另请注意,包含堆栈跟踪不会为您提供额外的信息。调用堆栈在生成的lambda中结束。 – Steven 2013-02-13 15:44:22
是的,但至少你可以尝试评论一些东西,以便迅速找出罪魁祸首。这不是一个大的lambda。 – Jon 2013-02-13 15:56:52