In-lining InvocationExpressions This is regards to one of my answers: Linq Expression Calling Combines. The question wasn't very well received, but it proved to be an interesting task that I enjoyed working on.

I ended up with the following:

public static class ExpressionHelpers
{
    public static TExpressionType InlineInvokes&lt;TExpressionType&gt;(this TExpressionType expression)
        where TExpressionType : Expression
    {
        return (TExpressionType)new InvokeInliner().Inline(expression);
    }

    public static Expression InlineInvokes(this InvocationExpression expression)
    {
        return new InvokeInliner().Inline(expression);
    }

    public class InvokeInliner : ExpressionVisitor
    {
        private Stack&lt;Dictionary&lt;ParameterExpression, Expression&gt;&gt; _context = new Stack&lt;Dictionary&lt;ParameterExpression, Expression&gt;&gt;();
        public Expression Inline(Expression expression)
        {
            return Visit(expression);
        }

        protected override Expression VisitInvocation(InvocationExpression e)
        {
            var callingLambda = e.Expression as LambdaExpression;
            if (callingLambda == null) //Fix as per comment
                return base.VisitInvocation(e);
            var currentMapping = new Dictionary&lt;ParameterExpression, Expression&gt;();
            for (var i = 0; i &lt; e.Arguments.Count; i++)
            {
                var argument = Visit(e.Arguments[i]);
                var parameter = callingLambda.Parameters[i];
                if (parameter != argument)
                    currentMapping.Add(parameter, argument);
            }
            _context.Push(currentMapping);
            var result = Visit(callingLambda.Body);
            _context.Pop();
            return result;
        }

        protected override Expression VisitParameter(ParameterExpression e)
        {
            if (_context.Count &gt; 0)
            {
                var currentMapping = _context.Peek();
                if (currentMapping.ContainsKey(e))
                    return currentMapping[e];
            }
            return e;
        }
    }
}


At its core, it simply in-lines Invoke() calls in the expression tree, replacing them with the called expression's code verbatim. 

Examples:

INPUT:
Invoke(i =&gt; (i + 1), 3)

OUTPUT:
(3 + 1)  

INPUT: 
i =&gt; Invoke((i, j) =&gt; (i * j), Invoke(i =&gt; (i + 1), i), Invoke(i =&gt; (i + 2), i))

OUTPUT:
i =&gt; ((i + 1) * (i + 2))

INPUT:  
b =&gt; Invoke((d, e) =&gt; (d * e), Invoke(b =&gt; (50 + Invoke(z =&gt; (25 + Invoke(h =&gt; (h * 8), z)), b)), b), Invoke(c =&gt; (c + 2), b))  

OUTPUT:   
b =&gt; ((50 + (25 + (b * 8))) * (b + 2))  


Example usage:

Expression&lt;Func&lt;int, int&gt;&gt; f1 = i =&gt; i + 1;
Expression&lt;Func&lt;int, int&gt;&gt; f2 = i =&gt; i + 2;
Expression&lt;Func&lt;int, int, int&gt;&gt; f3 = (i, j) =&gt; i * j;

var p = Expression.Parameter(typeof(int), "i");
var r = Expression
    .Invoke(f3, new[] { 
        Expression.Invoke(f1, p), 
        Expression.Invoke(f2, p) }) 
    .InlineInvokes();

Expression&lt;Func&lt;int, int&gt;&gt; lam = Expression.Lambda&lt;Func&lt;int, int&gt;&gt;(r, p);


The only 'issue' I can come up with is that if one were to modify f1 after InlineInvokes() was called, they might be surprised if they were expecting f1 to be captured and lazily executed, but in my opinion, this is probably to be expected (any other opinions?).

In summary, my questions are:


Are there any pitfalls in in-lining invocations (that is, is my idea fundamentally flawed, and cannot be solved 'properly')?
Are there any issues with the code I've written itself?
I ended up using this code a few times, and recently was thinking through a scenario where the above will fail:

var iParam = Expression.Parameter(typeof(int));
var lParam = Expression.Parameter(typeof(int));

var original = Expression.Invoke(
    Expression.Lambda(
        Expression.Invoke(
                Expression.Lambda(
                    Expression.Add(iParam, lParam),
                    iParam),
                Expression.Constant(1)
        ),
        lParam),
    Expression.Constant(2)
);
var transformed = original.InlineInvokes();


The original can be visualized as:

Invoke(Param_0 =&gt; Invoke(Param_1 =&gt; (Param_1 + Param_0), 1), 2)


Here, the inner invocation is using a parameter from its parent. Since we're using a stack to represent the mappings between parameters, we're not taking into consideration Param_0. Here's the current output:

(1 + Param_0)


Here's the fixed code:

protected override Expression VisitInvocation(InvocationExpression e)
{
    var callingLambda = e.Expression as LambdaExpression;
    if (callingLambda == null)
        return base.VisitInvocation(e);
    var currentMapping = new Dictionary&lt;ParameterExpression, Expression&gt;();
    for (var i = 0; i &lt; e.Arguments.Count; i++)
    {
        var argument = Visit(e.Arguments[i]);
        var parameter = callingLambda.Parameters[i];
        if (parameter != argument)
            currentMapping.Add(parameter, argument);
    }
    if (_context.Count &gt; 0)
    {
        var existingContext = _context.Peek();
        foreach (var kvp in existingContext)
        {
            if (!currentMapping.ContainsKey(kvp.Key))
                currentMapping[kvp.Key] = kvp.Value;
        }
    }

    _context.Push(currentMapping);
    var result = Visit(callingLambda.Body);
    _context.Pop();
    return result;
}


Note that we only propagate the parameters if they don't already exist. This is required in situations as follows:

var iParam = Expression.Parameter(typeof(int));
var lParam = Expression.Parameter(typeof(int));

var original = Expression.Invoke(
    Expression.Lambda(
        Expression.Invoke(
                Expression.Lambda(
                    Expression.Add(iParam, lParam),
                    iParam, lParam),
                Expression.Constant(1), Expression.Constant(3)
        ),
        lParam),
    Expression.Constant(2)
);




Invoke(Param_0 =&gt; Invoke((Param_1, Param_0) =&gt; (Param_1 + Param_0), 1, 3), 2)


Here, the inner invoke is providing a value for Param_0 which takes precedence over what the outer invoke is providing. If we compile that expression (without inlining) - we see the result is 4.

Thus, we must ensure that we only add parameters from our parent context if we haven't already been provided them.