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

来源:https://www.cnblogs.com/GongQun/archive/2019/12/09/12010794.html
-Advertisement-
Play Games

1.1、自定義config結構(參考對應顏色標註),放到configuration根節點下: <test> <testInfos> <testInfo aa="aaKeyStr1" bb="111111" /> <testInfo aa="aaKeyStr2" bb="222222" /> </te ...


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/

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

 

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...