将通用对象加载为参数的操作码

时间:2015-10-16 14:01:55

标签: c# reflection typebuilder

我正在调用一个方法,其中一个参数是object类型。如何在堆栈上加载?我知道枚举有等价的整数,但我也想用它来将用Type.CreateInstance创建的实际对象作为方法addoutput的参数

//method addoutput of type overriden by the typebuilder is of form    addoutput(object o)
//enumType is a Type that is an enumeration
TypeBuilder tb;
//tb is initialized here
//override constructor
ConstructorBuilder ctor1 = tb.DefineConstructor(MethodAttributes.Public,    CallingConventions.Standard, null);
ILGenerator ctor1IL = ctor1.GetILGenerator();
FieldInfo[] infos;
infos = enumType.GetFields(BindingFlags.Public | BindingFlags.Static);
//basically for each value in the enumeration an output is created
foreach (FieldInfo fi in infos)
{
 MethodInfo addoutputinfo = tb.BaseType.GetMethod("addoutput",     BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance |     BindingFlags.Static);
 ctor1IL.Emit(OpCodes.Ldarg_0);
 //something like this ? seems there's a syntax error 
 ctor1IL.Emit(OpCodes.Ldobj,fi.GetValue(null))  
 ctor1IL.Emit(OpCodes.Call, addoutputinfo);
}

0 个答案:

没有答案