Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create a Configuration Section That Contains a Collection

There is a great question and answer here that illustrates how to create a custom configuration section that is able to parse configuration of the following form into .Net objects:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="CustomConfigSection" type="ConfigTest.CustomConfigSection,ConfigTest" />
  </configSections>

  <CustomConfigSection>
    <ConfigElements>
      <ConfigElement key="Test1" />
      <ConfigElement key="Test2" />
    </ConfigElements>
  </CustomConfigSection>

</configuration>

My question is, does anyone know how to create the same custom configuration section without the ConfigElements element? For example, one that would parse the following CustomConfigSection element in place of the one shown above:

  <CustomConfigSection>
    <ConfigElement key="Test1" />
    <ConfigElement key="Test2" />
  </CustomConfigSection>

The issue that I have is that it appears that the type CustomConfigSection needs to inherit from both ConfigurationSection and ConfigurationElementCollection, which of course is not possible in C#. The other approach I have found requires me to implement IConfigurationSectionHandler, which is deprecated as of .Net v2. Does anyone know how to accomplish the desired result? Thanks.

like image 471
Jeff G Avatar asked Jun 01 '15 19:06

Jeff G


People also ask

How do I create a custom section in app config?

Open the App. config file and add the configSections, sectionGroup and section to it. We need to specify the name and fully qualified type of all the section and section group.

What is configuration section?

The configuration section is an optional section for programs and classes, and can describe the computer environment on which the program or class is compiled and executed.


1 Answers

You don't need to inherit from both ConfigurationSection and ConfigurationElementCollection. Instead, define your config section like this:

public class CustomConfigSection : ConfigurationSection
{
    [ConfigurationProperty("", IsDefaultCollection = true)]
    public MyConfigElementCollection ConfigElementCollection
    {
        get
        {
            return (MyConfigElementCollection)base[""];
        }
    }
}

And your config element collection:

[ConfigurationCollection(typeof(MyConfigElement), AddItemName = "ConfigElement"]
public class MyConfigElementCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new MyConfigElement();
    }

    protected override object GetElementKey(ConfigurationElement element)
    {
        if (element == null)
            throw new ArgumentNullException("element");

        return ((MyConfigElement)element).key;
    }
}

And config element itself:

public class MyConfigElement: ConfigurationElement
{
    [ConfigurationProperty("key", IsRequired = true, IsKey = true)]
    public string Key
    {
        get
        {
            return (string)base["key"];
        }
    }   
}
like image 174
vmg Avatar answered Oct 12 '22 22:10

vmg