Dynamically loading classes (with custom behavior) from different assemblies?

后端 未结 6 1000
遥遥无期
遥遥无期 2021-02-10 14:36

We are building an app for few customers, each has its own requirements along with the similar ones. We also want to keep all the code in the same app, not branch it, and the IF

6条回答
  •  北荒
    北荒 (楼主)
    2021-02-10 15:25

    Here's one way to handle it with DI using Unity.

    IUnityContainer container = new UnityContainer();
    string customerNamespace = ConfigurationManager.AppSettings["CustomerNamespace"];
    container.RegisterType(typeof(ISomeInterface), 
                           Type.GetType(customerNamespace+".SomeImplementation"));
    
    
    // ...
    
    ISomeInterface instance = conainer.Resolve();
    

    Where each customer has their own implementation of ISomeInterface in a customer specific namespace.

提交回复
热议问题