2

How do I emit IL to call a DynamicMethod while creating a DynamicMethod?

When calling ILGenerator.Emit(OpCodes.Callvirt, myDynamicMethod); the IL that is produces results in a MissingMethodException when executed.

I reproduced the issue with this minimal code:

var dm1 = new DynamicMethod("Dm1", typeof(void), new Type[0]);
dm1.GetILGenerator().Emit(OpCodes.Ret);
var dm2 = new DynamicMethod("Dm2", typeof(void), new Type[0]);
var ilGenerator = dm2.GetILGenerator();
ilGenerator.Emit(OpCodes.Callvirt, dm1);
ilGenerator.Emit(OpCodes.Ret);

dm2.Invoke(null, new Type[0]); // exception raised here
Sellorio
  • 1,806
  • 1
  • 16
  • 32

1 Answers1

2

You can indeed call a DynamicMethod from another DynamicMethod.

var ilGenerator = dm2.GetILGenerator();
ilGenerator.Emit(OpCodes.Call, dm1);

OpCodes.Callvirt should be used when calling a virtual method on an object (e.g. ToString()). This does not apply to DynamicMethod.

OpCodes.Call should instead be used.

Mr Anderson
  • 2,200
  • 13
  • 23
  • Now getting Invalid Program exception. – Sellorio Jul 27 '19 at 06:37
  • From what I picked up from other errors, `DynamicMethod`s can only be statics, right? I am copying my IL from an existing method and replacing method calls with dynamics which means instead of calling `string.GetHashCode()` (for example) it's calling a dyamic `GetHashCode(string)`. From my basic understanding of IL, the 2 should be equivalent (i.e. just a matter of replacing the call instruction and the rest should just work). – Sellorio Jul 27 '19 at 06:46
  • @Sellorio I'm running the code in visual studio 2017 and not getting an error. try changing your ctor to `new DynamicMethod("Dm1", typeof(void), new Type[0], typeof(object), true);` – Mr Anderson Jul 27 '19 at 06:47
  • Yeah the basic code works but my main code isn't functional yet. (see previous comment) – Sellorio Jul 27 '19 at 06:47
  • Correct, they are only statics. The only way to add instance methods at runtime is with `System.Reflection.Emit.TypeBuilder` and not on existing types. You can get your `DynamicMethod` to mimic an instance method by having the first parameter be of that type. However, there's no way to call it virtually – Mr Anderson Jul 27 '19 at 06:52
  • Yeah I thought so. If I understand correctly, `string.GetHashCode()` can be trivially replaced with `GetHashCode(string)` (the dynamic) and the IL surrounding the call is identical (since param0 is used for `this` in instance calls and `param0` for static calls... right?) – Sellorio Jul 27 '19 at 06:55
  • Yes, that's exactly right. `Ldarg0` would be used for `this` instance and first parameter of static – Mr Anderson Jul 27 '19 at 06:56
  • Then I don't know what's going wrong. I've got a branch up with my changes if you want to check it out: https://github.com/Sellorio/JazSharp/tree/switch-to-reflection-emit – Sellorio Jul 27 '19 at 06:59
  • Are you pushing the argument(s) to the stack before the `Call` instruction? – Mr Anderson Jul 27 '19 at 07:07
  • Hey sorry, I've spent too many continuous hours on this. The call is working. The issue is the code inside the method so don't worry about it. Thanks for your help! – Sellorio Jul 27 '19 at 07:09
  • FYI, forgot to specify the type when using Newarr OpCode in the 2nd Dynamic Method. – Sellorio Jul 27 '19 at 07:16