C# 将泛型对象作为参数加载的操作码
我正在调用一个方法,其中一个参数的类型为object。如何将其加载到堆栈上?我知道枚举有整数等价物,但我也希望使用它将使用Type.CreateInstance创建的实际对象作为参数放入方法addoutputC# 将泛型对象作为参数加载的操作码,c#,reflection,typebuilder,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
//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);
}
你能提供一个吗?好的,请再次回顾OP,谢谢用C写代码,然后看看ILDASM生成了什么@用户1296193,你能提供你尝试得到的c#等价物吗?addoutput(Enum.EnumValue1);addoutput(Enum.EnumValue2)。。。。