【发布时间】:2011-10-14 06:21:56
【问题描述】:
我在运行时生成新类型, 在我生成默认构造函数后,我想生成另一个带有参数的构造函数。我正在这样做:
cb = tb.DefineConstructor(MethodAttributes.Public | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName,
CallingConventions.Standard, new Type[] { typeof(bool) });
GenConstructorWithParameters(cb, fields, genFields);
问题是,我无法从方法 GenConstructorWithParameters 调用默认构造函数,因为 CLR 不允许我写这样的东西:
gen.Emit(OpCodes.Ldarg_0);
gen.Emit(OpCodes.Call, cb.DeclaringType.GetConstructor(Type.EmptyTypes));//Not allowed to call .GetConstructor() on not created type!
如何调用默认构造函数?有可能吗?
tb - TypeBuilder 的实例,cb - ConstructorBuilder
【问题讨论】:
-
糟糕,不是重复的,抱歉 :)
标签: c# .net code-generation il