Reputation: 22406
I have the following simple code:
var b = Expression.Parameter(typeof(double), "b");
var negativeB = Expression.Negate(b);
If I stop there and try to quick watch Expression.Lambda(negativeB).Compile()
I get an error:
variable 'b' of type 'System.Double' referenced from scope '', but it is not defined
The other answers about this error message don't seem to address what's going on here, or how to fix it in this case.
Upvotes: 0
Views: 631
Reputation: 27609
You have an expression negativeB
that requires an input parameter of b
. However when you are defining your Lambda you are not defining any parameters.
What you need to do is this:
Expression.Lambda(negativeB, b).Compile();
This then compiles
Upvotes: 3
Reputation: 9365
You should specify the parameter (b
) in the Lambda:
var l = Expression.Lambda(negativeB, b).Compile();
var r = l.DynamicInvoke(32); // = -32
Upvotes: 3