実行時に新しい型を生成しています。デフォルトのコンストラクターを生成した後、パラメーターを使用して別のコンストラクターを生成したいと思います。次のようにしています。
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