關於ConfigurationSection自定義config的簡單使用


1.1、自定義config結構(參考對應顏色標注),放到configuration根節點下:

<test>
  <testInfos>
    <testInfo aa="aaKeyStr1" bb="111111" />
    <testInfo aa="aaKeyStr2" bb="222222" />
  </testInfos>
  <testC cc="ccStr" />
</test>

推薦獨立文件引用:

將1.1中自定義config新建為xml文件,命名:test.config

configuration根節點下添加:

<test configSource="test.config" />

1.2、config文件下需添加對應配置:

configSections節點下添加,name為自定義config的根節點,type為根節點類的命名空間.類名, 命名空間

<section name="test" type="CMDTest.TestConfigurationSection, CMDTest" />

2、創建根節點類TestConfigurationSection,繼承ConfigurationSection,對應自定義config中test節點:

    public class TestConfigurationSection : ConfigurationSection
    {
        [ConfigurationProperty("testInfos", IsDefaultCollection = true)]
        public TestInfoElementCollection ContractInfos
        {
            get
            {
                return (TestInfoElementCollection)base["testInfos"]; // 子列表節點
            }
        }
        [ConfigurationProperty("testC", IsDefaultCollection = true)]
        public TestCElement TestC
        {
            get
            {
                return (TestCElement)base["testC"]; // 單個子節點
            }
        }
    }

3.1、(子節點為集合時使用)創建子節點Collection類,繼承ConfigurationElementCollection,對應自定義config中testInfos節點:

    public class TestInfoElementCollection : ConfigurationElementCollection
    {

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

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((TestInfoElement)element).AA; // 指定AA屬性為唯一索引
        }
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMap;
            }
        }
        protected override string ElementName
        {
            get
            {
                return "testInfo"; // 子節點名稱
            }
        }
    }

3.2、創建列表子元素類,繼承ConfigurationElement(單個子節點均可繼承此類),對應自定義config中testInfo節點:

    public class TestInfoElement : ConfigurationElement
    {
        [ConfigurationProperty("aa", IsRequired = true)] // 是否必填
        public string AA
        {
            get
            {
                return (string)base["aa"]; // 節點屬性名稱
            }
        }

        [ConfigurationProperty("bb")]
        public string BB
        {
            get
            {
                return (string)base["bb"];
            }
        }
    }

4、(子節點為單個節點時使用)同3.2,對應自定義config中testC節點:

    public class TestCElement : ConfigurationElement
    {
        [ConfigurationProperty("cc", IsRequired = true)]
        public string CC
        {
            get
            {
                return (string)base["cc"];
            }
        }
    }

5、調用代碼Demo:

var tcs = (TestConfigurationSection)ConfigurationManager.GetSection("test");
// 讀取單個子節點
var testC = tcs.TestC;
// 讀取list節點
Dictionary<string, string> list = new Dictionary<string, string>();
foreach (TestInfoElement item in tcs.ContractInfos)
{
    list.Add(item.AA, item.BB);
}
var aa = list["aaKeyStr1"];

運行效果:

 

心得:我理解的自定義config無非就是將節點抽象成對象屬性,對應的屬性需繼承相關父類進行讀取,對象類的結構需與config結構對應;編寫時遇到復雜的config需注意樹的深度以及節點、屬性對應名稱,容易寫錯,需細心

附上示例源碼地址:https://gitee.com/GongQun/TestRun/tree/develop/

如有錯誤,請指正,謝謝!

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM