For the very first time I am exploring expression trees. I have a few basic doubts.
So essentially , an expression takes only a lambda expression . Ans then we can Compile() the lambda expression to MSIL code which in turn returns a generic delegate. We can invoke the returned delegate as it is . Is my understanding correct ?
If it is here is what I am trying to achieve: ((10*5)+(9/4))
BinaryExpression b1 = Expression.MakeBinary(ExpressionType.Multiply, Expression.Constant(10), Expression.Constant(5));//(10*5)
BinaryExpression b2 = Expression.MakeBinary(ExpressionType.Divide, Expression.Constant(9), Expression.Constant(4));//(9/4)
BinaryExpression b4 = Expression.MakeBinary(ExpressionType.Add, b1, b2);//((10*5)+(9/4))
So at this point we have made the lambda expression body
. Now to turn it to a full lambda expression
we need to call
Console.WriteLine(Expression.Lambda<Func<int, int>>(b4).Compile());
I am not getting this part . And this does not work also .
Why this Func<int,int>
?
Is it like the inner expressions will take only int as param and the entire expression will return an int?
Obviously this does not work. How the generated lambda looks like ?
I am getting the entire picture? How to make this work?
Func<int,int>
is a signature for lambdas that take a singleint
parameter, and return anint
. Your lambda has a different signature.Your lambda does not take any parameters, so you need
Func<int>
instead.Generated lambda is a callable object. If you would like to evaluate the expression that you get back, cast and call it, like this:
The above prints
52
, as expected.Demo 1.
If you would like to make a
Func<int,int>
, add a parameter to your expression, e.g. make it(p*5)+(9/4)
wherep
is anint
parameter:Demo 2.