您可以使用ModuleBuilder.DefineType
未指定父类型的重载,然后使用该TypeBuilder.SetParent
方法将父级设置为递归类型(使用类似于typeof(A<>).MakeGenericType(tb)
where tb
is your的参数TypeBuilder
,但我前面没有 C# 编译器我)。
编辑- 这是一个工作示例,假设你有一个ModuleBuilder mb
. 对于空的默认构造函数,你根本不需要使用该DefineConstructor
方法;或者你可以使用DefineDefaultConstructor
. 不过,我已经包含了一个显式调用基本构造函数的示例,以防您想在其中添加一些额外的逻辑。
TypeBuilder tb = mb.DefineType("B");
Type AB = typeof(A<>).MakeGenericType(tb);
tb.SetParent(AB);
ConstructorInfo ctor = TypeBuilder.GetConstructor(AB, typeof(A<>).GetConstructor(new Type[] { }));
ILGenerator ilg = tb.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { }).GetILGenerator();
ilg.Emit(OpCodes.Ldarg_0);
ilg.Emit(OpCodes.Call, ctor);
ilg.Emit(OpCodes.Ret);
Type t = tb.CreateType();