Search code examples
c#reflectionreflection.emitintermediate-language

C# Call and return an object from a static method in IL


This is an extension to the solutions offered here. I've created a static method that returns me an object. My goal, is the write a dynamic method for a type I define at runtime to return me the object that this static method is returning. My code thus far:

 // type builder and other prep stuff removed for sake of space and reading

private void EmitReferenceMethodBody(Type returnType)
{
    MethodBuilder builder =
    typeBuilder.DefineMethod(
                    method.Name,
                    MethodAttributes.Virtual | MethodAttributes.Public,
                    method.CallingConvention,
                    method.ReturnType,
                    typeArray1);
    builder.InitLocals = true;
    ILGenerator gen = builder.GetILGenerator();
    MethodInfo getStoredObject = typeof(ObjectStore).GetMethod("GetStoredObject",                  BindingFlags.Public | BindingFlags.Static);        
    MethodInfo getTypeFromHandle = typeof(Type).GetMethod("GetTypeFromHandle");            

    gen.Emit(OpCodes.Ldtoken, returnType);
    gen.Emit(OpCodes.Call, getTypeFromHandle);
    gen.Emit(OpCodes.Call, getStoredObject);
    gen.Emit(OpCodes.Ret);   
}

The updated code now calls the method but appears to be passing the type of the dynamically created type rather then the variable returnType.


Solution

  • At least one problem is that you are pushing the "this" reference (OpCodes.Ldarg_0) onto the stack even though it is never popped (since you are invoking a static method). I'd try removing that line and see if it behaves better.

    Another problem is that you are passing in new Type[] { returnType } to the EmitCall method. That is intended for optional arguments (params) and I suspect your method actually doesn't have any parameters. Therefore, you should remove that argument as well.

    Edit:

    Based on comments, you are trying to pass in a System.Type object known statically to a method you are invoking dynamically. This is possible, but you need to jump through a couple of hoops.

    1. Get a reference to the MethodInfo for the method Type.GetTypeFromHandle:

      MethodInfo getTypeFromHandle = typeof(Type).GetMethod("GetTypeFromHandle");
      
    2. Use the following lines of IL to push your returnType onto the stack:

      gen.Emit(OpCodes.Ldtoken, returnType);
      gen.Emit(OpCodes.Call, getTypeFromHandle);
      

    To sum, your code should look like this:

    MethodInfo getTypeFromHandle = typeof(Type).GetMethod("GetTypeFromHandle");
    gen.Emit(OpCodes.Ldtoken, returnType);
    gen.Emit(OpCodes.Call, getTypeFromHandle);
    gen.EmitCall(OpCodes.Call, getStoredObject);                
    gen.Emit(OpCodes.Ret);
    

    The transitional stack behavior of this code is:

    1. Push the RuntimeTypeHandle corresponding to the specified Type reference onto the stack by using Opcodes.Ldtoken.

    2. Invoke getTypeFromHandle which pops the type handle off the stack, and pushes the actual System.Type onto the stack.

    3. Call your static method, which will pop the Type argument off of the stack and push the return value of your own method onto the stack.

    4. Return from the method.