【问题标题】:How to write the custom config section for nested list in c#.net如何在 c#.net 中为嵌套列表编写自定义配置部分
【发布时间】:2025-12-23 18:30:11
【问题描述】:

您好,任何人都可以建议编写自定义配置类的方法,以便在我的 app.config 文件中编写如下配置。

<configuration>
    <configSections>
        <section name="oneP2CReportConfiguration" type="MyDll.Config.CustomConfig.OnePage2ColReportConfigInfo, MyDll.Config"/>          
    </configSections>

        <oneP2CReportConfiguration>
                <countryGroup>
                        <country name="India">
                        <country name="Japan">      
                </countryGroup>
                <countryGroup>  
                        <country name="China">
                        <country name="Nepal">  
                </countryGroup>
        </oneP2CReportConfiguration> 
</configuration>

【问题讨论】:

  • 为什么投反对票.. 我的问题有什么问题
  • 我不确定为什么这个问题被否决了——但从我的角度来看,我认为这个问题是在研究太少之后提出的。有效地要求某人为你做你的工作。我不会担心 - 堆栈溢出的人和互联网上的其他人一样烦人/傲慢/自以为是 :) 你的答案得到 +1,但我不能回答这个问题 ^^
  • 感谢@DanRayson,我有点着急,这就是为什么我不能提供我正在尝试的代码,我想人们可能会给我一些类似自定义配置的链接,以便我可以实现它。

标签: c# custom-configuration


【解决方案1】:

终于搞明白了如何编写自定义配置文件。

C#代码

namespace Custom.Configs
{
    public class OnePage2CountryReportConfig : ConfigurationSection
    {
        [ConfigurationProperty("", IsDefaultCollection = true)]
        public CountryGroupCollection Members
        {
            get
            {
                CountryGroupCollection countryGroupCollection = (CountryGroupCollection)base[""];
                return countryGroupCollection;
            }
        }
    }

    public class CountryGroupCollection : ConfigurationElementCollection
    {
        public CountryGroupCollection()
        {
            CountryGroupElement details = (CountryGroupElement)CreateNewElement();
            if (details.Name != "")
            {
                Add(details);
            }
        }

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

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

        protected override Object GetElementKey(ConfigurationElement element)
        {
            return ((CountryGroupElement)element).Name;
        }

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

        new public CountryGroupElement this[string name]
        {
            get
            {
                return (CountryGroupElement)BaseGet(name);
            }
        }

        public int IndexOf(CountryGroupElement details)
        {
            return BaseIndexOf(details);
        }

        public void Add(CountryGroupElement details)
        {
            BaseAdd(details);
        }
        protected override void BaseAdd(ConfigurationElement element)
        {
            BaseAdd(element, false);
        }

        public void Remove(CountryGroupElement details)
        {
            if (BaseIndexOf(details) >= 0)
                BaseRemove(details.Name);
        }

        public void RemoveAt(int index)
        {
            BaseRemoveAt(index);
        }

        public void Remove(string name)
        {
            BaseRemove(name);
        }

        public void Clear()
        {
            BaseClear();
        }

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


    public class CountryCollection : ConfigurationElementCollection
    {

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

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((Country)element).CountryCode;
        }

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

        protected override bool IsElementName(string elementName)
        {
            return !String.IsNullOrEmpty(elementName) && elementName == "country";
        }

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

        public Country this[int index]
        {
            get
            {
                return base.BaseGet(index) as Country;
            }
        }

        public new Country this[string key]
        {
            get
            {
                return base.BaseGet(key) as Country;
            }
        }
    }

    public class CountryGroupElement : ConfigurationElement
    {

        [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
        [StringValidator(InvalidCharacters = "  ~!@#$%^&*()[]{}/;’\"|\\")]
        public string Name
        {
            get { return (string)this["name"]; }
            set { this["name"] = value; }
        }

        [ConfigurationProperty("", IsDefaultCollection = true, IsKey = false, IsRequired = true)]
        public CountryCollection Countries
        {
            get { return base[""] as CountryCollection; }
            set
            {
                base[""] = value;
            }
        }

    }

    public class Country : ConfigurationElement
    {
        [ConfigurationProperty("code", IsRequired = true, IsKey = true)]
        [StringValidator(InvalidCharacters = "  ~!@#$%^&*()[]{}/;’\"|\\")]
        public string CountryCode { get { return (string)this["code"]; } }
    }

}

app.config

   <configSections>
   <section name="onePage2CountryReportConfigSection" type="Custom.Configs.OnePage2CountryReportConfig, Custom.Config"/>
  </configSections>

  <onePage2CountryReportConfigSection>
    <countryGroup name="group1">
      <country code="JAP"/>
      <country code="NEP"/>
    </countryGroup>
    <countryGroup name="group2">
      <country code="IND"/>
      <country code="BAN"/>
      <country code="SRI"/>
      <country code="PHI"/>
    </countryGroup>
  </onePage2CountryReportConfigSection>

【讨论】: