Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression tree - compile inner lambda in outer lambda - scoping resolution

I am creating expression tree and there is a situation where I need to create one lambda in another lambda and store inner one in a class and add that class in expression tree. This is simple example of what I am trying to do (this code does not compile):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace SimpleTest {
    public class LambdaWrapper {
        private Delegate compiledLambda;
        public LambdaWrapper(Delegate compiledLambda) {
            this.compiledLambda = compiledLambda;
        }
        public dynamic Execute() {
            return compiledLambda.DynamicInvoke();
        }
    }

    public class ForSO {

        public ParameterExpression Param;

        public LambdaExpression GetOuterLambda() {
            IList<Expression> lambdaBody = new List<Expression>();
            Param = Expression.Parameter(typeof(object), "Param");
            lambdaBody.Add(Expression.Assign(
                            Param, 
                            Expression.Constant("Value of 'param' valiable"))
                          );

            lambdaBody.Add(Expression.Call(
                            null, 
                            typeof(ForSO).GetMethod("Write"), 
                            Param)
                          );

            Delegate compiledInnerLambda = GetInnerLambda().Compile();
            LambdaWrapper wrapper = new LambdaWrapper(compiledInnerLambda);
            lambdaBody.Add(Expression.Constant(wrapper));
            //lambdaBody.Add(GetInnerLambda());
            return Expression.Lambda(
                        Expression.Block(
                                new ParameterExpression[] { Param }, 
                                lambdaBody));
        }

        public LambdaExpression GetInnerLambda() {
            return Expression.Lambda(
                    Expression.Block(
                        Expression.Call(null, 
                                typeof(ForSO).GetMethod("Write"), 
                                Expression.Constant("Inner lambda start")),
                        Expression.Call(null, 
                                typeof(ForSO).GetMethod("Write"), 
                                Param),
                        Expression.Call(null, 
                                typeof(ForSO).GetMethod("Write"), 
                                Expression.Constant("Inner lambda end"))
                    )
                );
        }

        public static void Write(object toWrite) {
            Console.WriteLine(toWrite);
        }

        public static void Main(string[] args) {
            ForSO so = new ForSO();
            LambdaWrapper wrapper = so.GetOuterLambda().Compile()
                                      .DynamicInvoke() as LambdaWrapper;
            wrapper.Execute();
            //(so.GetOuterLambda().Compile().DynamicInvoke() as Delegate).DynamicInvoke();
        }
    }
}

Problem is in GetInnerLambda().Compile() line in GetOuterLambda method. I am aware of one solution - it is in commented part of code. With that, everything works fine, but I need a wrapper as return value, not expression subtree (it might be ok to store inner lambda subtree in LambdaWrapper, and compile it later, but same problem occures).

Error I am getting is Unhandled Exception: System.InvalidOperationException: variable 'Param' of type 'System.Object' referenced from scope '', but it is not defined.

If I add Param to block variables in inner lambda, code compiles, but Param has not value assigned in outer lambda (and that makes sense).

How can this be solved?

like image 327
del-boy Avatar asked May 19 '12 21:05

del-boy


1 Answers

Well, since you can't use Param as a constant value in your inner lambda expression, I suggest you to add a lambda parameter to your expression:

public LambdaExpression GetInnerLambda()
{
    var param = Expression.Parameter(typeof(object));
    return Expression.Lambda(
        Expression.Block(
            Expression.Call(null,
                typeof(ForSO).GetMethod("Write"),
                Expression.Constant("Inner lambda start")),
            Expression.Call(null,
                typeof(ForSO).GetMethod("Write"),
                param),
            Expression.Call(null,
                typeof(ForSO).GetMethod("Write"),
                Expression.Constant("Inner lambda end"))
        ),
        param
    );
}

Then store the parameter's value in your LambdaWrapper class, and use it later as an argument in the DynamicInvoke call:

public class LambdaWrapper
{
    private object param;
    private Delegate compiledLambda;

    public LambdaWrapper(Delegate compiledLambda, object param)
    {
        this.compiledLambda = compiledLambda;
        this.param = param;
    }

    public dynamic Execute()
    {
        return compiledLambda.DynamicInvoke(param);
    }
}

That works, but the only issue is that it will call WriteLine on Param, which is a ParameterExpression object. To solve this, you have to create the wrapper class dynamically in your expression tree:

//lambdaBody.Add(Expression.Constant(wrapper));
lambdaBody.Add(Expression.New(
    typeof(LambdaWrapper).GetConstructor(new[] { typeof(Delegate), typeof(object) }),
    Expression.Constant(compiledInnerLambda),
    Param)
);

Then it will use the assigned value of Param. And since you don't use Param outside of GetOuterLambda, you can now use it as a local variable.

EDIT:

Here is my second attempt to solve this issue:

public LambdaExpression GetOuterLambda()
{
    ...
    //Delegate compiledInnerLambda = GetInnerLambda().Compile();
    //LambdaWrapper wrapper = new LambdaWrapper(compiledInnerLambda);

    lambdaBody.Add(Expression.New(
        typeof(LambdaWrapper).GetConstructor(new[] { typeof(Delegate) }),
        Expression.Call(
            Expression.Call(
                typeof(ForSO).GetMethod("GetInnerLambda", BindingFlags.Public | BindingFlags.Static),
                Param
            ),
            typeof(LambdaExpression).GetMethod("Compile", Type.EmptyTypes)
        )
    ));
    ...
}

public static LambdaExpression GetInnerLambda(object param)
{
    return Expression.Lambda(
        Expression.Block(
            Expression.Call(null,
                typeof(ForSO).GetMethod("Write"),
                Expression.Constant("Inner lambda start")),
            Expression.Call(null,
                typeof(ForSO).GetMethod("Write"),
                Expression.Constant(param)),
            Expression.Call(null,
                typeof(ForSO).GetMethod("Write"),
                Expression.Constant("Inner lambda end"))
        )
    );
}

This approach compiles this inner lambda when you run the outer delegate. By doing this, Param will be assigned before the inner lambda gets compiled.

like image 172
Balazs Tihanyi Avatar answered Nov 01 '22 00:11

Balazs Tihanyi