我有以下班子:
public class Entity<T> where T : Entity<T> {
public Factory<T> Factory { get; private set; }
public Entity(Factory<T> factory) {
Factory = factory;
}
}
public class Factory<T> { }
public class MyEntity : Entity<MyEntity> {
public MyEntity(Factory<MyEntity> factory) : base(factory) { }
}
我正试图与具体建筑商一道,积极创造我的活力。 到目前为止,我有以下法典:
class Program {
static ModuleBuilder _moduleBuilder;
public static ModuleBuilder ModuleBuilder {
get {
if (_moduleBuilder == null) {
AssemblyBuilder asmBuilder = System.Threading.Thread.GetDomain().DefineDynamicAssembly(new AssemblyName("Dynamic"), AssemblyBuilderAccess.Run);
_moduleBuilder = asmBuilder.DefineDynamicModule("MainModule");
}
return _moduleBuilder;
}
}
static void Main(string[] args) {
TypeBuilder typeBuilder = ModuleBuilder.DefineType("MyEntity", TypeAttributes.Public);
Type baseType = typeof(Entity<>).MakeGenericType(typeBuilder);
typeBuilder.SetParent(baseType);
Type factoryType = typeof(Factory<>).MakeGenericType(typeBuilder);
ConstructorBuilder cBuilder = typeBuilder.DefineConstructor(MethodAttributes.Public, CallingConventions.Standard, new Type[] { factoryType });
ILGenerator ctorIL = cBuilder.GetILGenerator();
ctorIL.Emit(OpCodes.Ldarg_0);
ctorIL.Emit(OpCodes.Ldarg_1);
ConstructorInfo c = baseType.GetConstructor(new Type[] { factoryType });
ctorIL.Emit(OpCodes.Call, c);
ctorIL.Emit(OpCodes.Ret);
Type syType = typeBuilder.CreateType();
Console.ReadLine();
}
}
该守则没有成功ConstructorInfo c = baseType.GetConstructor(new打字[]{工厂Type }
。 我不赞成。
是否有办法实现这一目标? 我用了三天时间。 希望得到任何帮助。
感谢!