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