Change default app.config at runtime

后端 未结 8 1976
伪装坚强ぢ
伪装坚强ぢ 2020-11-22 05:50

I have the following problem:
We have an application that loads modules (add ons). These modules might need entries in the app.config (e.g. WCF configuration). Because t

8条回答
  •  伪装坚强ぢ
    2020-11-22 06:43

    If anybody is interested, here is a method that works on Mono.

    string configFilePath = ".../App";
    System.Configuration.Configuration newConfiguration = ConfigurationManager.OpenExeConfiguration(configFilePath);
    FieldInfo configSystemField = typeof(ConfigurationManager).GetField("configSystem", BindingFlags.NonPublic | BindingFlags.Static);
    object configSystem = configSystemField.GetValue(null);
    FieldInfo cfgField = configSystem.GetType().GetField("cfg", BindingFlags.Instance | BindingFlags.NonPublic);
    cfgField.SetValue(configSystem, newConfiguration);
    

提交回复
热议问题