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

后端 未结 6 1074
栀梦
栀梦 2021-02-10 14:49

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:14

    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.

提交回复
热议问题