I have this code which produce a delegate which multiply myNumber by 5
ParameterExpression numParam = Expression.Parameter(typeof(int), "num");
ConstantExpression five = Expression.Constant(5, typeof(int));
BinaryExpression numMultiply = Expression.Multiply(numParam,five);
Lets create the delegate :
Expression<Func<int, int>> lambda1 =
Expression.Lambda<Func<int, int>>(
numMultiply,
new ParameterExpression[] { numParam });
Console.Write(lambda1.Compile()(4));
now let’s say I want to change this expression tree to Add instead of Multiply
here is the new line :
BinaryExpression numAdd = Expression.Add(numParam,five);
But how can I change the lambda1 so that it will now will use numAdd instead of multiply ?
You just build a new one, and compile it.
From the MSDN page:
The “should be” phrase is a little odd but when you look at the API you will see that all relevant properties (Body, Left, Right) are read-only.