Recursive Custom Configuration in c#

六月ゝ 毕业季﹏ 提交于 2019-12-05 15:27:40

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

try configuration group ConfigurationSectionGroup Class

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