I'm using System.Linq.Expressions
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.
For example ,this is my code
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
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()?
Thanks
The JavaScript exception " variable is not defined" occurs when there is a non-existent variable referenced somewhere. ReferenceError. What went wrong? There is a non-existent variable referenced somewhere. This variable needs to be declared, or you need to make sure it is available in your current script or scope .
However, a function can access all variables and functions defined inside the scope in which it is defined. In other words, a function defined in the global scope can access all variables defined in the global scope. Found a problem with this page?
The problem is that parameter expression objects that represents variable y in expressions e1 and e2 are different. The fact that the two variables are named the same and have the same type does not matter: e1.Parameters.First () and e2.Parameters.First () is not the same object.
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...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With