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.
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 theEmitCallmethod. 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.Typeobject known statically to a method you are invoking dynamically. This is possible, but you need to jump through a couple of hoops.Get a reference to the
MethodInfofor the methodType.GetTypeFromHandle:Use the following lines of IL to push your
returnTypeonto the stack:To sum, your code should look like this:
The transitional stack behavior of this code is:
Push the
RuntimeTypeHandlecorresponding to the specifiedTypereference onto the stack by usingOpcodes.Ldtoken.Invoke
getTypeFromHandlewhich pops the type handle off the stack, and pushes the actualSystem.Typeonto the stack.Call your static method, which will pop the
Typeargument off of the stack and push the return value of your own method onto the stack.Return from the method.