I have the following classes:
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) { }
}
I am trying to dynamically create class MyEntity with the constructor specified. So far I have the following code:
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();
}
}
The code failed @ ConstructorInfo c = baseType.GetConstructor(new Type[] { factoryType })
. I got a NotSupportedException.
Is there any way to achieve this? I have been stonewalled by this for three days. Any help would be appreciated.
Thanks!
You need to use the static TypeBuilder.GetConstructor
method. I think this should work (untested):
ConstructorInfo genCtor = typeof(Entity<>).GetConstructor(new Type[] { typeof(Factory<>).MakeGenericType(typeof(Entity<>).GetGenericArguments()) });
ConstructorInfo c = TypeBuilder.GetConstructor(baseType, genCtor);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With