Using C# OpCodes to emit method that returns an object

纵饮孤独 提交于 2019-12-12 03:08:25

问题


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;
}

回答1:


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.



来源:https://stackoverflow.com/questions/9606104/using-c-sharp-opcodes-to-emit-method-that-returns-an-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!