English 中文(简体)
A. 动态型号的设定,由一位参照其属地的核查员进行
原标题:Dynamic Type Creation with a Constructor that reference its dependencies

我有以下班子:

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 }。 我不赞成。

是否有办法实现这一目标? 我用了三天时间。 希望得到任何帮助。

感谢!

最佳回答
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签