Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Type Creation with a Constructor that reference its dependencies

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!

like image 207
Rushui Guan Avatar asked Apr 09 '12 17:04

Rushui Guan


1 Answers

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);
like image 130
kvb Avatar answered Oct 01 '22 10:10

kvb