3

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.

Community
  • 1
  • 1
OnResolve
  • 4,016
  • 3
  • 28
  • 50

2 Answers2

4

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.

Kirk Woll
  • 76,112
  • 22
  • 180
  • 195
  • So removing the load arg 0 emit allows this to call the static method, however I do want to pass the returnType variable to the static method, but it seems to be passing 'this'. When I debug the static method, the param which is of type 'Type' is not the value of returnType, but rather the type of the dynamic class I created. – OnResolve Mar 08 '12 at 20:26
  • Please confirm that your method signature looks like this: `object GetStoredObject(Type returnType);`. If so, then you do indeed need to pass in an argument. However, what `Type` are you actually trying to pass in? – Kirk Woll Mar 08 '12 at 20:44
  • "public static Object GetStoredObject(Type contract)" My method above called EmitReferenceMethodBody(Type returnType) gets passed the type i want to pass to my static method. – OnResolve Mar 08 '12 at 20:53
  • Works! Thank you so much. Your explanation was very intuitive! – OnResolve Mar 08 '12 at 21:45
1

Expression trees might be a better solution here. It's pretty easy to create a Func<Type, object> using dynamic typing through expressions.

ParameterExpression paramEx = Expression.Parameter(typeof(Type), "paramObject");
Expression callExpression = Expression.Call(typeof(ObjectStore), "GetStoredObject", null, paramEx);
Expression<Func<Type, object>> funcExpression = Expression.Lambda<Func<Type, object>>(callExpression, paramEx);
Func<Type, object> actualFunc = funcExpression.Compile();

Now if the ObjectStore requires generic parameters, you would replace typeof(ObjectStore) with typeof(ObjectStore).MakeGenericType(returnType). If the GetStoredObject function itself requires generic parameters, then you would change the null in the Expression.Call statement to new Type[] { returnType }. If this is generated once for each type you pass in and you plan on using this a lot, then it might be a good idea to cache these Funcs into a Dictionary<Type, Func<Type, object>> and only build it once (as compiling Expressions repeatedly is a waste of system resources).

SPFiredrake
  • 3,852
  • 18
  • 26