Is it possible to view the IL code generated when you call Compile() on an Expression tree? Consider this very simple example:
class Program
{
public int Value { get; set; }
static void Main(string[] args)
{
var param = Expression.Parameter(typeof(Program));
var con = Expression.Constant(5);
var prop = Expression.Property(param, typeof(Program).GetProperty("Value"));
var assign = Expression.Assign(prop, con);
Action<Program> lambda = Expression.Lambda<Action<Program>>(assign, param).Compile();
Program p = new Program();
lambda(p);
//p.Value = 5;
}
}
Now, the expression tree does what the last line of Main
says. Compile the application, then open it in Reflector. You can see the IL code of p.Value = 5;
that does the assignment. But the expression tree was made and compiled at runtime. Is it possible to view the resulting IL code from the compile?
Yes! Use this tool:
https://github.com/drewnoakes/il-visualizer
This was incredibly useful when I was implementing and debugging Compile, as I'm sure you can imagine.