Now I'm going to compile the expression tree into a delegate to generate code dynamically, but I have a problem. I have to call a method in the expression tree, which is exactly the expression tree delegate that has not been dynamically compiled yet. What should I do?
I want to generate the following code from the expression tree:
int i = 0;
Action ac = null;
ac = () =>
{
//if (i-- > 0) condition
ac();
};
The following code does not work and will prompt ac is null
static Action ac = Build();
static Action Build()
{
return Expression.Lambda<Action>(
Expression.Call(
Expression.Constant(ac), //throw ac is null
typeof(Action).GetType().GetMethod("Invoke")
)
).Compile();
}