I'm using Sigil to create a DynamicMethod
and would like to see the generated IL.
I've never worked with DynamicMethods
before so maybe there's a very obvious answer, but I haven't found anything so far.
Here's a similar question, but it is pretty old and I don't know whether the linked tool works in VS2013 – I thought maybe there was something newer available. Storing the generated method in an assembly and writing it to disk likely works, but that's pretty cumbersome during development.
By the way I'm aware of the out string instructions
parameter of Sigil's CreateDelegate
method, but this doesn't seem to be "real" IL code (contains e.g. named labels) and I'm also not sure whether this is before or after Sigil's optimization.
Edit: I ended up creating a dynamic assembly and writing it to disk, as @svick suggested. The IL of the emitted methods can then be viewed with ildasm
. In case someone wants to do the same, here's the code I used:
var asmName = new AssemblyName("MyAssembly");
var asm = AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Save);
var mod = asm.DefineDynamicModule(asmName.Name, asmName.Name + ".dll");
var typeBuilder = mod.DefineType("MyType", TypeAttributes.Public | TypeAttributes.Abstract);
// NOTE: this is Sigil's Emit
var emitter = Emit<MyDelegate>.BuildMethod(typeBuilder, "MyMethod", MethodAttributes.Static | MethodAttributes.Public, CallingConventions.Standard);
// [...] emit calls
emitter.CreateMethod();
asm.Save(asmName.Name + ".dll");
ILGenerator
, so when it actually does the write - it isn't directly in charge anyway. I wonder ifdelegateInstance.Method.GetMethodBody().GetILAsByteArray()
would suffice for what you want?GetILAsByteArray
method for now.GetMethodBody
operation throws an exception, apparently this method doesn't work for DynamicMethods, but thanks for the tip.