title: Merging custom configuration sections at runtime in .NET
body: <p>My question is about working with standard .NET configuration objects and custom configuration elements as well, (which one can define by extending the <a href="https://msdn.microsoft.com/en-us/library/system.configuration.configurationsection%28v=vs.110%29.aspx"><code>System.Configuration.ConfigurationSection</code></a> class). We usually obtain these from the <a href="https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager%28v=vs.110%29.aspx"><code>System.Configuration.ConfigurationManager</code></a> class's methods, <a href="https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.getsection%28v=vs.110%29.aspx"><code>GetSection(...)</code></a> being an example.</p>
  
  <p>The loaded configuration object appears to be a <em>merged</em> configuration object containing the setup present in the application config file (the the <strong>app.config</strong> or <strong>web.config</strong> file which a developer may have created) and what is defined in the <strong>machine.config</strong> file (the latter coming with the .NET Framework installation).</p>
  
  <p>So, we can assume that the configuration is loaded in hierarchical manner with the <strong>machine.config</strong> first, and any user-defined configuration overlaying that default setup, and could be looked at like this:</p>
  
  <ul>
  <li><strong>machine.config</strong>
  <ul>
  <li><strong>app.config</strong> (overrides / merges with respective elements found in machine.config )</li>
  </ul></li>
  </ul>
  
  <p>My goal is to create multiple layers of configuration, so that there might be other config files between (and if possible, after) the <strong>machine.config</strong> and the <strong>app.config</strong> file:</p>
  
  <ul>
  <li><strong>machine.config</strong>
  <ul>
  <li><strong><em>custom.config</em></strong> (a custom config interfering between the machine.config and the app.config file)
  <ul>
  <li><strong>app.config</strong> - now <strong>app.config</strong> is merged with both <strong>machine.config</strong> and <strong>custom.config</strong></li>
  </ul></li>
  </ul></li>
  </ul>
  
  <hr>
  
  <p><strong>Update</strong>  </p>
  
  <p>The point with that is - if I have defined the configuration section in both <strong>custom.config</strong> and <strong>app.config</strong>, I need to get a merged version of both configs when I call <code>ConfigurationManager.GetSection("MyCustomSection")</code>. Ideally, it will be great if I am able to perform merging as describe in <a href="http://blogs.msdn.com/b/markgabarra/archive/2006/06/27/648742.aspx">this MSDN article</a>.</p>
  
  <hr>
  
  <p>Normally, I would write my own configuration manager class and will try to get as far as I could to get the desired result, but assuming the .NET framework has this working for <strong>machine.config</strong> and <strong>app.config</strong>, I thought I might benefit from the built-in functionality of the framework. In addition, I do not know how to manually trigger such merging, if I am indeed supposed to resort to a config manager implementation of my own.</p>
  
  <p>So, is it possible to leverage the built-in mechanisms of configuration section/element merging with custom configuration files? I am especially interested in developing and supporting this for custom configuration sections. The <code>System.Configuration</code> namespace contains base objects to build configuration section and elements, and those allow some settings related to the merging (like setting the appropriate <a href="https://msdn.microsoft.com/en-us/library/system.configuration.configurationelementcollectiontype%28v=vs.110%29.aspx"><code>ConfigurationElementCollectionType</code></a> for example). Are these related to only merging with <strong>machine.config</strong> (or multiple layers of <strong>web.config</strong> files in a web application), or is it possible to manually trigger the merging of pre-loaded config files? I am trying to avoid having to support custom merging in any of my custom configuration objects and probably forget about supporting the existing settings from the System.Configuration...</p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>There is an important clarification to I'd like to make, in response to the existing answers and comments. I am capable of loading the <code>ConfigurationSection</code> objects from the current application setup (<strong>app.config</strong> / <strong>web.config</strong>) and from a physical file that is my <strong>custom.config</strong>. I need to know if there is a chance to merge these objects without resorting to reflection and property-by-property comparison, by some built-in means in the framework.</p>
  
  <hr>
  
  <p><sup>
  <strong>Note:</strong> I would appreciate better a solution that would work on .NET 3.0+. Please add a note if your answer targets a higher version of the framework.</sup></p>