Custom Configuration for .NET (Part 4): Backgrounder on Configuration Sections
Updated 2010-06-22: Added link to part 5.
There are two remaining approaches to providing structure that I have not yet covered: collections of elements and section groups. Element collections will be a topic for several future posts as there are a number of different approaches to creating element collections, and two of them make little sense without first covering configuration inheritance (see part 5).
Section groups are much simpler, and mostly a matter of declaring them.
Creating Section Groups
When custom sections are declared using the
element of a configuration file, one can declare both sections (with the
<sections> element, using attributes to define the associated
custom type and its name) and section groups.
To define a section group use the—unsurprisingly—
element. This needs a name and a type. Often the type will be
System.Configuration.ConfigurationSectionGroup which adds no behaviour
(but one can customise this, more below) and just allows programmatic access to
its child sections and section groups (there is no specified limit to
the depth of structure you can define—but don’t go overboard with
deep structures which will become increasingly hard to understand and maintain).
<configuration> <configSections> <sectionGroup name="userSettings" type="System.Configuration.ConfigurationSectionGroup, System.Configuration, Version=126.96.36.199, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> <section name="recentFiles" type="CustomConfiguration.RecentFilesConfigurationSection, MyAssembly"/> </sectionGroup> </configSections> <userSettings> <recentFiles showFiles="5" removeNoneExistant="true"/> </userSettings> </configuration>
Which defines a
userSettings element to act as a container for (presumably)
multiple sections describing many aspects of user behaviour.
Note, replace the strong name for
Version=188.8.131.52, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”
for .NET versions 2, 3 and 3.5.
Reading Section Groups and Contained Sections
In previous parts of this series the application code used
Configuration.GetSection(‹name›) to get
to its custom section (and the instance of
Configuration was obtained
This works for configuration sections that are immediate children of the root
var config = ConfigurationManager.OpenExeConfiguration(…); var userSettingsGroup = config.GetSectionGroup("userSettings"); var recentFiles = (RecentFilesConfigurationSection) userSettingsGroup.GetSection("recentFiles");
GetSectionGroup returns a
GetSectionGroup methods as
Configuration Section and Group Collection Properties
You might notice that the
ConfigurationSectionGroup also have properties
can be indexed by name. These give another approach to getting
a section or section group:
var myConfig = (MyConfigurationSection)config.GetSection("mine"); var myConfig2 = (MyConfigurationSection)config.Sections["mine"];
are completely equivalent.
Custom Section Group Types
It is also possible to create your own custom configuration section
group by deriving from
a number of .NET Framework types do this. E.g.
defined in the
System.Xml assembly, which adds properties
to type safely access contained sections (i.e. the properties
contain the access into the
and cast the results to the correct type).