That library targets .NET Framework 4 and reflects over private fields to extract the IL from the DynamicMethod's ILGenerator. I'm not sure how robust this strategy is. You can do something similar yourself:
Expression<Func<int, int, int>> foo = (x, y) => x + y;
var m = (DynamicMethod) foo.Compile().GetMethodInfo();
var gen = m.GetILGenerator().Dump();
byte[] il = gen.Uncapsulate().m_ILStream;
il.Dump();
Comments
I believe this is being implemented in .NET 9 (although we are still lacking CompileToMethod):
https://github.com/dotnet/runtime/issues/15704
It's possible that you might find a third-party library that accomplishes this.
would it be possible for the LinqPad's Disassemble extension method on MethodBase to be used for that and use this il-visualizer libary?
That library targets .NET Framework 4 and reflects over private fields to extract the IL from the DynamicMethod's ILGenerator. I'm not sure how robust this strategy is. You can do something similar yourself:
From there, you could invoke a disassembler on the IL, or use the sample I wrote for C# in a Nutshell:
https://www.albahari.com/nutshell/E12-CH18.aspx
Here's a complete example.
https://share.linqpad.net/re5lasah.linq
Thanks a lot!
I am getting "InvalidCastException: Unable to cast object of type 'RTDynamicMethod' to type 'System.Reflection.Emit.DynamicMethod'."
for both .NET 6 and .NET7 (saw that the query you had was targetting .NET9)
is this a valid way to go around the error ?
var method = (DynamicMethod)expr.Compile().GetMethodInfo().Uncapsulate().m_owner;
It works, the only missing thing is of course the tokens are not represented as strings.