Reputation: 9009
In .NET configuration, you can write your own configuration section and have quite a lot of freedom in how to structure it (as XML). So in most cases we do this:
<?xml version="1.0"?>
<configuration>
<configSections>
<section name="MySection" type="MySectionType, MySectionAssembly" ... />
</configSections>
<MySection>
...
</MySection>
</configuration>
Then, to access our section in code, we do the following:
MySection mySection = (MySection) ConfigurationManager.GetSection("MySection");
The example above is the only way I've seen to have a custom section obtained in code.
Imagine now that we have a library or framework, that defines the MySection
classes and the classes for the structure inside it. We want to provide these classes and structure to a third party developers that will define in their applications the configuration files. At the same time, our framework will need to obtain that section programmatically in order to function properly.
Clearly, the end user of our framework has the absolute freedom to name MySection
to anything else. Therefore the code above will fail to load the section. Is there a nice way to overcome this issue? Deciding to force the consumers of the library to name their section only in the way we desire is something I would like to avoid doing.
EDIT: An additional complication to the scenario is putting the section in a section group:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="MyGroup">
<section name="MySection" type="MySectionType, MySectionAssembly" ... />
</sectionGroup>
</configSections>
<MyGroup>
<MySection>
...
</MySection>
</MyGroup>
</configuration>
Would it be possible to handle this situation too?
Thanks in advance.
Upvotes: 1
Views: 166
Reputation: 28839
Should not be a problem. See this:
http://msdn.microsoft.com/en-us/library/system.configuration.configurationsectiongroup.aspx
Upvotes: 1