Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you don't mind hacking around the framework and you can reasonably assume the .net framework version the application is running on (i.e. it's a web application or an intranet application) then you could try something like this:</p> <pre><code>using System; using System.Collections.Specialized; using System.Configuration; using System.Configuration.Internal; using System.Reflection; static class ConfigOverrideTest { sealed class ConfigProxy:IInternalConfigSystem { readonly IInternalConfigSystem baseconf; public ConfigProxy(IInternalConfigSystem baseconf) { this.baseconf = baseconf; } object appsettings; public object GetSection(string configKey) { if(configKey == "appSettings" &amp;&amp; this.appsettings != null) return this.appsettings; object o = baseconf.GetSection(configKey); if(configKey == "appSettings" &amp;&amp; o is NameValueCollection) { // create a new collection because the underlying collection is read-only var cfg = new NameValueCollection((NameValueCollection)o); // add or replace your settings cfg["test"] = "Hello world"; o = this.appsettings = cfg; } return o; } public void RefreshConfig(string sectionName) { if(sectionName == "appSettings") appsettings = null; baseconf.RefreshConfig(sectionName); } public bool SupportsUserConfig { get { return baseconf.SupportsUserConfig; } } } static void Main() { // initialize the ConfigurationManager object o = ConfigurationManager.AppSettings; // hack your proxy IInternalConfigSystem into the ConfigurationManager FieldInfo s_configSystem = typeof(ConfigurationManager).GetField("s_configSystem", BindingFlags.Static | BindingFlags.NonPublic); s_configSystem.SetValue(null, new ConfigProxy((IInternalConfigSystem)s_configSystem.GetValue(null))); // test it Console.WriteLine(ConfigurationManager.AppSettings["test"] == "Hello world" ? "Success!" : "Failure!"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload