I’m trying to generate IL to mimic the following method MyMethod:
public void DoSomething(object a, object b, string c, string d){...};
public virtual void MyMethod(object a, object b)
{
DoSomething(a, b, "hello", this.SomeInstanceString);
}
This is what I’ve got so far, but I’m unable to understand the proper way to load the 3rd and 4th arguments onto the stack to call DoSomething(a, b, "hello", this.SomeInstanceString)):
MethodBuilder myMethod = typeBuilder.DefineMethod("MyMethod",
MethodAttributes.Public | MethodAttributes.Virtual, typeof(void), new[]
{ typeof(object), typeof(object) });
ILGenerator myMethodILGen = mbFromCustomObject.GetILGenerator();
myMethodILGen.Emit(OpCodes.Ldarg_0);
myMethodILGen.Emit(OpCodes.Ldarg_1);
// How do I load the string "hello" and the local instance
// variable this.SomeInstanceString onto the stack?
myMethodILGen.Emit(OpCodes.Call, GetMethodInfoForDoSomething());
myMethodILGen.Emit(OpCodes.Ret);
So, how do I load the string "hello" and the local instance variable this.SomeInstanceString onto the stack to call DoSomething?
Loading a string literal is fairly easy.
Loading a field from the object instance requires that you first load the object instance onto the stack and then use the Ldfld opcode. You should presumably have a FieldBuilder for your SomeInstanceString field already that you can use for this.
Also, keep in mind that Ldarg_0 does not do what you think it does. Instance methods have an implicit argument that is in the zero slot which contains the instance that the method is currently operating within. That’s why we’re using Ldarg_0 to dereference the field, because presumably you want the instance that the method is in. This does not apply to static methods, though.