Search code examples
c#.netconfigurationattributescustom-configuration

Implicit ConfigurationElementCollection sections


The System.Configuration connection string configuration classes define:

ConnectionStringsSection : ConfigurationSection

ConnectionStringSettingsCollection : ConfigurationElementCollection

ConnectionStringSettings : ConfigurationElement

But the section in the config file looks like:

 <connectionStrings>
    <add name="Foo" connectionString="Whatever" />
 </connectionStrings>

so it appears that the ConnectionStringSettingsCollection is implicit.

I would like to have this same behaviour in my own configuration section to remove a redundant level of nesting, but I couldn't find how to do it.


Solution

  • You need to decorate your default collection with the appropriate ConfigurationProperty attribute:

    [ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]