c#中的递归自定义配置

本文关键字:自定义 配置 递归 | 更新日期: 2023-09-27 18:21:26

我正在尝试创建一个自定义配置部分,该部分遵循以下递归结构:

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

这可能吗?我不确定该如何布置配置类。

我有以下显示器:

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

我需要添加什么才能使其递归?

c#中的递归自定义配置

您可以使用如下配置文件(对上面的配置文件稍作更改)来完成此操作:

<?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>

和配置类,如:

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

然后在Windows控制台应用程序中使用,例如:

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配置组ConfigurationSectionGroup类