我正在运行时生成新的类型,生成默认构造函数后,我想再生成一个带参数的构造函数。我的做法是:
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
。