MemberExpression:InvalidOperationExpression变量“X”从范围'引用,但它没有定义 [英] MemberExpression: InvalidOperationExpression variable 'x' referenced from scope '', but it is not defined

查看:524
本文介绍了MemberExpression:InvalidOperationExpression变量“X”从范围'引用,但它没有定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 System.Linq.Expressions

我试图建立一个简单的 LambdaExpression ,其中包括一个<一个HREF =htt​​p://msdn.microsoft.com/en-us/library/system.linq.expressions.memberexpression.aspx> MemberExpression 。如果我明确地创造MemberExpression与System.Linq.Expressions API(如 MakeMemberAccess ),我会得到错误,从范围'引用变量InvalidOperationExpression'X',但它没有定义当我打电话编译()在LambdaExpression。

I was attempting to build a simple LambdaExpression that includes a MemberExpression. If I create the MemberExpression explicitly with the System.Linq.Expressions API (e.g. MakeMemberAccess), I will get the error "InvalidOperationExpression variable 'x' referenced from scope '', but it is not defined" when I call Compile() on the LambdaExpression.

例如,这是我的代码

Expression<Func<Customer, string>> expression1, expression2, expression3;
Func<Customer, string> fn;
expression1 = (x) => x.Title;
fn = expression1.Compile();//works
fn(c);
MemberExpression m;
m = Expression.MakeMemberAccess(
Expression.Parameter(typeof(Customer), "x"), typeof(Customer).GetProperty("Title"));
expression2 = Expression.Lambda<Func<Customer, string>>(m,
    Expression.Parameter(typeof(Customer), "x"));

m = Expression.Property(Expression.Parameter(typeof(Customer),"x"), "Title");
expression3 = Expression.Lambda<Func<Customer, string>>(m,
    Expression.Parameter(typeof(Customer), "x"));

fn = expression3.Compile();//InvalidOperationExpression variable 'x' referenced from scope '', but it is not defined
fn = expression2.Compile();//InvalidOperationExpression variable 'x' referenced from scope '', but it is not defined

表达式和表达式3抛出一个异常时,编译()方法被调用,但表达式没有;表达式的作品。为什么是这样?如何创建一个MemberExpression像在表达式2,3和让他们的工作(而不是抛出一个异常)当我打电话编译()?

expression2 and expression3 throw an exception when the Compile() method is called, but expression1 does not; expression1 works. Why is this? How do I create an MemberExpression like in expressions 2, 3 and get them to work (not throw an exception) when I call Compile()?

感谢

推荐答案

您正在创建的叫法不同的参数X数次。如果你使用一个 ParameterExpression ,它都应该做工精细。

You're creating different parameters called "x" several times. If you use a single ParameterExpression, it should all work fine.

ParameterExpression p = Expression.Parameter(typeof(Customer), "x");
MemberExpression m = Expression.MakeMemberAccess(p, 
    typeof(Customer).GetProperty("Title"));
expression2 = Expression.Lambda<Func<Customer, string>>(m, p);

m = Expression.Property(p, "Title");
expression3 = Expression.Lambda<Func<Customer, string>>(m, p);

fn = expression3.Compile();
fn = expression2.Compile();



基本上参数表达式不上名字匹配 - 你必须在任何地方使用同一个。这是一个有点痛,但我们去...

Basically parameter expressions aren't matched by name - you've got to use the same one everywhere. It's a bit of a pain, but there we go...

这篇关于MemberExpression:InvalidOperationExpression变量“X”从范围'引用,但它没有定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆