Recursive Custom Configuration in c#

1k views Asked by At

I am trying to create a custom configuration section that follows the following recursive structure:

<monitorSettings>
    <monitor description="description1" />
    <monitor description="description2" />
    <monitor description="description3">
        <monitor description="description3.1" />
        <monitor description="description3.2" />
    </monitor>
</monitorSettings>

Is this possible? I am not sure how I would lay out the configuration classes.

I have the following for the monitor:

public class Monitor : ConfigurationElement
{
    [ConfigurationProperty("description", IsRequired = true)]
    public String Description
    {
        get
        {
            return (String)this["description"];
        }  
        set
        {
            this["description"] = value;
        }
    }
}

What would I need to add to make it recursive?

2

There are 2 answers

0
Bonshington On

try configuration group ConfigurationSectionGroup Class

1
Stuart Whiteford On

You can do this with a config file like the following (slight change to the one above):

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="monitorSettings" type="RecursiveCustomConfiguration.MonitorConfig, RecursiveCustomConfiguration"/>
  </configSections>
  <monitorSettings>
    <monitors>
      <monitor description="description1" />
      <monitor description="description2" />
      <monitor description="description3">
        <monitors>
          <monitor description="description3.1" />
          <monitor description="description3.2" />
        </monitors>
      </monitor>
    </monitors>
  </monitorSettings>
</configuration>

and configuration classes like:

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace RecursiveCustomConfiguration
{

    public class MonitorConfig : ConfigurationSection
    {
        public static MonitorConfig GetConfig()
        {
            return ConfigurationManager.GetSection("monitorSettings") as MonitorConfig;
        }

        [ConfigurationProperty("monitors", IsDefaultCollection = true, IsRequired = true)]
        [ConfigurationCollection(typeof(MonitorCollection), AddItemName = "monitor")]
        public MonitorCollection Monitors
        {
            get
            {
                return this["monitors"] as MonitorCollection;
            }
        }

    }

    public class MonitorCollection : ConfigurationElementCollection
    {

        public Monitor this[int index]
        {
            get
            {
                return base.BaseGet(index) as Monitor;
            }
            set
            {
                if (base.BaseGet(index) != null)
                {
                    base.BaseRemoveAt(index);
                }
                this.BaseAdd(index, value);
            }
        }

        public Monitor this[object description]
        {
            get
            {
                return base.BaseGet(description) as Monitor;
            }
        }

        protected override ConfigurationElement CreateNewElement()
        {
            return new Monitor();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((Monitor)element).Description;
        }

        public override ConfigurationElementCollectionType CollectionType
        {
            get { return ConfigurationElementCollectionType.BasicMap; }
        }

        protected override string ElementName
        {
            get { return "monitor"; }
        }

    }

    public class Monitor : ConfigurationElement
    {

        [ConfigurationProperty("description", IsRequired = true)]
        public string Description
        {
            get
            {
                return this["description"] as string;
            }
        }

        [ConfigurationProperty("monitors", IsRequired = false)]
        public MonitorCollection Monitors
        {
            get
            {
                return this["monitors"] as MonitorCollection;
            }
        }

    }

}

Then to use in a Windows Console app for instance:

MonitorCollection monitors = MonitorConfig.GetConfig().Monitors;

foreach (Monitor m in monitors)
{
    Console.WriteLine(m.Description);
    if (m.Monitors != null && m.Monitors.Count > 0)
    {
        foreach (Monitor m1 in m.Monitors)
        {
            Console.WriteLine(m1.Description);
        }
    }
}