internal static Func <StrongBox <object>[], Delegate> BindLambda(LambdaExpression lambda, Dictionary <ParameterExpression, LocalVariable> closureVariables) { ParameterExpression expression; LightLambdaClosureVisitor visitor = new LightLambdaClosureVisitor(closureVariables, expression = Expression.Parameter(typeof(StrongBox <object>[]), "closure")); lambda = (LambdaExpression)visitor.Visit(lambda); return(Expression.Lambda <Func <StrongBox <object>[], Delegate> >(lambda, new ParameterExpression[] { expression }).Compile()); }
/// <summary> /// Walks the lambda and produces a higher order function, which can be /// used to bind the lambda to a closure array from the interpreter. /// </summary> /// <param name="lambda">The lambda to bind.</param> /// <param name="closureVariables">Variables which are being accessed defined in the outer scope.</param> /// <returns>A delegate that can be called to produce a delegate bound to the passed in closure array.</returns> internal static Func <StrongBox <object>[], Delegate> BindLambda(LambdaExpression lambda, Dictionary <ParameterExpression, LocalVariable> closureVariables) { // 1. Create rewriter var closure = Expression.Parameter(typeof(StrongBox <object>[]), "closure"); var visitor = new LightLambdaClosureVisitor(closureVariables, closure); // 2. Visit the lambda lambda = (LambdaExpression)visitor.Visit(lambda); // 3. Create a higher-order function which fills in the parameters var result = Expression.Lambda <Func <StrongBox <object>[], Delegate> >(lambda, closure); // 4. Compile it return(result.Compile()); }