简体   繁体   English

如何在 c#.net 中为嵌套列表编写自定义配置部分

[英]How to write the custom config section for nested list in c#.net

Hi can any one suggest the way to write the custom configuration class for writing the configuration as follows in my app.config file.嗨,任何人都可以建议编写自定义配置类的方法,以便在我的 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>

Finally I figured it out how to write the custom configuration file.最后我想出了如何编写自定义配置文件。

C# Code 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM