您可以使用ModuleBuilder.DefineType
超载不指定父类型,然后使用TypeBuilder.SetParent
方法父设置为递归式(使用参数类似typeof(A<>).MakeGenericType(tb)
其中tb
是你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();
来源
2009-09-14 15:58:42
kvb
看看[这个问题](http://stackoverflow.com/questions/1348268/how-do-i-create-a-class-that-inherits-from-another-and-passes -a型参数中)。我认为你有同样的问题。 – 2009-09-14 12:17:19