Can I use Activator.CreateInstance with an Interface?

前端 未结 6 1515
故里飘歌
故里飘歌 2021-01-04 00:21

I have an example:

        Assembly asm = Assembly.Load(\"ClassLibrary1\");
        Type ob = asm.GetType(\"ClassLibrary1.UserControl1\");
        UserContro         


        
6条回答
  •  清歌不尽
    2021-01-04 00:53

    The only problem with the accepted answer is that you must have a concrete class on your assembly that implements the interface.

    To avoid that I have created my CustomActivator that is able to create a dynamic object at runtime and make it implements the desired interface.

    I put it on the github: https://github.com/fabriciorissetto/CustomActivator

    The call is simple:

    CustomActivator.CreateInstance();
    

提交回复
热议问题