Execute LambdaExpression and get returned value as object

Sure… you just need to compile your lambda and then invoke it…

object input = 4;
var compiledLambda = lambda.Compile();
var result = compiledLambda.DynamicInvoke(input);

Styxxy brings up an excellent point… You would be better served by letting the compiler help you out. Note with a compiled expression as in the code below input and result are both strongly typed.

var input = 4;
var compiledExpression = exTyped.Compile();
var result = compiledExpression(input);

Leave a Comment