Question

I am creating a dynamic type which has a method that I'd like to return an object. I am failing to understand how to achieve this. Here's what I have so far:

// .. stuff to create type builder



MethodBuilder builder =
                typeBuilder.DefineMethod(
                    method.Name,
                    MethodAttributes.Virtual | MethodAttributes.Public,
                    method.CallingConvention,
                    method.ReturnType,
                    typeArray1);
            builder.InitLocals = true;
ILGenerator gen = builder.GetILGenerator();
Object myObjectIdLikeToReturn = someMethodCall();
//gen.??(??????????) // here's where I'm lost
gen.Emit(OpCodes.Ldloc_0);
gen.Emit(OpCodes.Ret);

I believe, if I am reading the msdn correct, I need to get the reference of myObjectIdLikeToReturn on the stack--however I've not had luck. Can anyone point me in the right direction?

Edit: To make it more clear. I'm attempting to write the equivalent in IL:

public virtual Object MyNewMethod() {
   return myObjectIdLikeToReturn;
}
Was it helpful?

Solution

You have this line:

Object myObjectIdLikeToReturn = someMethodCall();

You say you "want to get the reference to myObjectIdLikeToReturn onto the stack." But that is impossible. I think you are confusing the timing of when these things are happening. The IL generation code is analogous to compiling -- everything needs to be known statically. Whereas myObjectIdLikeToReturn is a variable that happens to exist while you are generating the code, but would not have any meaning to the class that is actually generated. You can't "bake in" a reference to some arbitrary runtime object, the idea simply doesn't make any sense.

Your only solution is to somehow make your IL perform the invocation to someMethodCall. But without knowing more about where that method lives and its connection to your generated type, it's hard to elaborate on precisely how you'll accomplish that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top