1

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");
Community
  • 1
  • 1
enzi
  • 4,057
  • 3
  • 35
  • 53
  • what kind out output are you expecting? raw bytes? text? or...? IIRC, Sigil acts as a layer on top of `ILGenerator`, so when it *actually* does the write - it isn't directly in charge *anyway*. I wonder if `delegateInstance.Method.GetMethodBody().GetILAsByteArray()` would suffice for what you want? – Marc Gravell Mar 13 '15 at 17:04
  • Text, I guess. I only wish to view the IL code for debugging purposes. I cannot use Ildasm (I suppose) because the code is generated at runtime. I realize that Sigil is only an abstraction, I just figured I mention it in case it has built-in support for this too. I'll have a look at the `GetILAsByteArray` method for now. – enzi Mar 13 '15 at 17:33
  • The `GetMethodBody` operation throws an exception, apparently this method [doesn't work for DynamicMethods](http://stackoverflow.com/questions/4146877/how-do-i-get-an-il-bytearray-from-a-dynamicmethod), but thanks for the tip. – enzi Mar 14 '15 at 16:28

1 Answers1

1

I think the simplest way would be to actually create an assembly containing your method and then use ildasm on that. That way, you can keep most of your code and only replace some plumbing (probably replacing Emit<T>.NewDynamicMethod() with Emit<T>.BuildMethod()).

svick
  • 236,525
  • 50
  • 385
  • 514
  • It's not the most convenient solution because, as you say, some code changes are necessary, but it works. I ended up creating a unit test that writes the assembly to disk (so that I can easily re-create it during development) and copy-pasted the Emit code in. – enzi Mar 14 '15 at 16:25