Recursive Custom Configuration in c#

梦想的初衷 提交于 2020-01-02 06:21:04

问题


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?


回答1:


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);
        }
    }
}



回答2:


try configuration group ConfigurationSectionGroup Class



来源:https://stackoverflow.com/questions/8983921/recursive-custom-configuration-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!