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

后端 未结 6 998
遥遥无期
遥遥无期 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:32

    Does each customer get their own exe and config file, and there is a shared dll? Or is there a shared exe, and each customer has their own dll?

    You can put the full type name in the configuration like this:

    Shared.exe.config:

    
      
    
    

    And put AssemblyForJohn.dll in the same folder as your Shared.exe.

    Then you can load it dynamically in code like this:

    Shared.exe:

    var typeString = ConfigurationManager.AppSettings["CustomerType"];
    var parts = typeString.Split(',');
    var typeName = parts[0];
    var assemblyName = parts[1];
    var instance = (BaseClass)Activator.CreateInstance(assemblyName, typeName).Unwrap();
    

提交回复
热议问题