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, ReadOnlyCollection
1 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?