views:

232

answers:

1

I'm building a LINQ expression tree but it won't compile because allegedly the local variable $var1 is out of scope:

variable '' of type 'System.Object' referenced from scope '', but it is not defined

This is the expression tree:

.Block() {
    $var1;
    .If ($n.Property1 == null) {
        .Block() {
            $var1 = null;
            .Return #Label1 { }
        }
    } .Else {
        .Default(System.Void)
    };
    $var1 = (System.Object)($n.Property1).Length;
    .Label
    .LabelTarget #Label1:;
    $var1
}

The following code is responsible for building the tree. It is part of something larger, therefore I don't expect its purpose to be perfectly clear from this example.

MemberExpression sourceExpression = ...;

List<Expression> expressions = new List<Expression>();
LabelTarget returnTarget = Expression.Label();
ParameterExpression resultVariable = Expression.Variable(typeof(object));

expressions.Add(resultVariable);

expressions.Add(
    Expression.IfThen(
        Expression.Equal(sourceExpression, Expression.Constant(null)),
        Expression.Block(
            Expression.Assign(resultVariable, Expression.Constant(null)),
            Expression.Return(returnTarget))));

expressions.Add(
    Expression.Assign(
        resultVariable,
        Expression.Convert(sourceExpression, typeof(object))));

expressions.Add(Expression.Label(returnTarget));
expressions.Add(resultVariable);

Expression finalExpression = Expression.Block(expressions);
object result = Expression.Lambda<Func<object>>(finalExpression).Compile()();

So the question is: how do I get the local variable into scope so that the expression compiles succesfully?

+1  A: 

Your'e adding the Expression.Variable to the list of "normal" expressions in the block - you should use the overload which specifies the variables do declare for the block separately:

Expression finalExpression = Expression.Block(new[] { resultVariable },
                                              expressions);

(And remove the call to expressions.Add(resultVariable);)

Jon Skeet
Thanks Jon, that worked! Solving this revealed a second, rather similar issue. The assignment of the sourceExpression variable makes the expression compiler throw: "variable 'n' of type 'TestClass' referenced from scope '', but it is not defined"
Sandor Drieënhuizen
I was able to fix that problem, thanks again.
Sandor Drieënhuizen
Thanks again for helping me out. The question was closely related to a blog post I was writing at http://blog.subspace.nl/post/Getting-rid-of-null-checks-in-property-chains.aspx which may of interest.
Sandor Drieënhuizen