3

I have an expression that contains Func<,> delegate. I'm trying to invoke it and to get the integer value as result

class Program
{
    static Expression TestMethod(Expression<Func<int, int>> expression)
    {
        return expression;
    }

    static void Main(string[] args)
    {
        var expr = TestMethod(i => i + 1);

        var result = Expression.Lambda<Func<int, int>> (expr).Compile().Invoke(1);

        Console.WriteLine(result);
    }
}

When I Invoke expression, I get next error:

Unhandled Exception: System.ArgumentException: Incorrect number of parameters supplied for lambda declaration at System.Linq.Expressions.Expression.ValidateLambdaArgs(Type delegateType, Expression& body, ReadOnlyCollection1 parameters, String paramName) at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, String name, Boolean tailCall, IEnumerable1 parameters) at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, Boolean tailCall, IEnumerable`1 parameters) at System.Linq.Expressions.Expression.Lambda[TDelegate](Expression body, ParameterExpression[] parameters) at TestConsoleApp.Program.Main(String[] args) in E:\programming\portfolio\TestConsoleApp\TestConsoleApp\Program.cs:line 22

How can I get the result of the Func<,> delegate invocation?

1 Answer 1

3

You're wrapping a lambda in a lambda... this is not what you want. Try this:

class Program
{
    static void Main(string[] args)
    {
        Expression<Func<int, int>> expr = i => i + 1;

        var result = expr.Compile().Invoke(1);

        Console.WriteLine(result);
    }
}

If you want to use the "passthrough" TestMethod, you can do this:

class Program
{
    static Expression TestMethod(Expression<Func<int, int>> expression)
    {
        return expression;
    }

    static void Main(string[] args)
    {
        var expr = TestMethod(i => i + 1);

        var result = ((Expression<Func<int, int>>)expr).Compile().Invoke(1);

        Console.WriteLine(result);
    }
}

Or if you want to rebuild the lambda from a non-generic expression:

class Program
{
    static LambdaExpression TestMethod(Expression<Func<int, int>> expression)
    {
        return expression;
    }

    static void Main(string[] args)
    {
        var expr = TestMethod(i => i + 1);

        var result = Expression.Lambda<Func<int, int>>(expr.Body, expr.Parameters).Compile().Invoke(1);

        Console.WriteLine(result);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

For same reason, I wanted to use Expression instance to store Expression<Func<int, int>>. I see that your decision will work. Don't you mind if i will ask you to help me to invoke my delegate from - Expression expr: Expression expr = TestMethod(i => i + 1);
@EvgeniyMiroshnichenko I added some code which hopefully is what you're looking for.
That was exactly what I was looking for!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.