user2155362
user2155362

Reputation: 1703

How can I get expression result?

I build an expression like below:

Expression left = Expression.Constant(5, typeof(int));
Expression right = Expression.Constant(6, typeof(int));
Expression result = Expression.Add(left,right);

So, can I get the real result about "5+6" via the expression result?

Upvotes: 2

Views: 110

Answers (1)

ProgrammingLlama
ProgrammingLlama

Reputation: 38767

I think you need to compile it as a Func:

Expression left = Expression.Constant(5, typeof(int));
Expression right = Expression.Constant(6, typeof(int));
Expression result = Expression.Add(left,right);

var compiled = Expression.Lambda<Func<int>>(result).Compile();
Console.WriteLine(compiled.Invoke());

Expression tree docs

Try it online

Upvotes: 2

Related Questions