C# 創建自定義配置節點1


轉載:http://www.educity.cn/develop/495003.html

      在.Net應用程序中我們經常看到VS為我們生成的項目工程中都會含有app.config或者web.connfig這樣的文件.這個文件就是我們所說的應用程序配置文件.在這個文件里面記述着一些與我們的應用程序相關的信息如數據庫連接認證模式等我們在程序中可以利用ConfigurationManager的ConnectionStrings屬性方便的獲取配置文件中的數據庫連接字符串信息。

可是有時候我們需要對它進行一些擴展加入一些自定義的元素而不是僅僅使用默認的配置.例如我們可能需要在程序啟動時動態的加載某個類並對其進行初始化,而這個類或者初始化數據是我們在程序設計的時候所不知道的.相信大家都碰到過這樣的問題,這里就不做過多的解釋了.最好的辦法無非就是把這些可能會改變的東西寫進配置文件里面,到你能夠確定的時候你只需要修改配置文件就Ok了,而不是產品馬上上線的時候還用秒鍾打開VS去改代碼。

添加一些自定義的元素到配置文件中是很容易的只需要兩步就能搞定:

   1、 在<configSections>節點中注冊你所要定義的節點名稱及用於處理該節點的配置節處理程序代碼如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="dbFactory"  type ="DbFactory.Configuration.DbFactorySection,DbFactory.Configuration" />
  </configSections>
</configuration>

 

  2、 在適當的位置添加自定義的節點代碼如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="dbFactory" type ="DbFactory.Configuration.DbFactorySection,DbFactory.Configuration" />
  </configSections>
  <dbFactory >
    <default factory="sql"></default>
    <factorys>
      <add name="sql"  assembly="HelloData"  class="SqlDbFactory" />
      <add name="oracle" assembly="HelloData" class="OracleDbFactory" />        
    </factorys>      
  </dbFactory>
</configuration>
    
 自定義節點算是添加完了,可是我們怎么在程序里面獲取這些配置信息呢?相信大家很多都是玩XML的高手,用SystemXml下的一些類寫個XML的解析類把我們自定義的節點信息解析出來不就得到我們想要的東西了嗎?的確是這樣大牛果然是大牛小弟實在是佩服!可是如果用這種方式來實現的話小弟就實在沒有必要寫這篇博文了。

Net框架為我們實現了很多的配置API來簡化我們對配置文件的操作。在第一個步驟中我們提到了配置節處理程序這個程序就是用來讀寫我們自定義的節點信息的。那么我們又如何來實現一個配置節處理程序呢?首先我們來了解一下相關的類和概念。

ConfigurationSection:自定義節點都要繼承該類以提供對自定義配置節的自定義處理和編程訪問。

ConfigurationElement:它表示配置文件內的一個元素。

ConfigurationElementCollection:它表示包含一個子元素集合的配置元素。

有了這些類我們可以歸納起來配置文件中有兩種類型的配置元素。

第一:單一型配置元素即繼承於ConfigurationElement的元素它不包含任何子元素。

第二:集合型配置元素即繼承於ConfigurationElementCollection的元素它包含一個子元素集合。

概念往往都比較抽象,要搞清楚這些東西我們還是結合我們上面給的例子來具體說明一下。

在<dbFactory> 配置節中有兩個元素,即<default>和<factorys>而<factorys>元素又包含了兩個子元素。那么在這里<default>就是單一型配置元素,而<factorys>就是集合型配置元素我們需要分別實現與這些元素相對應的類及其屬性。

<default>元素它是一個單一型的元素,所以我們繼承ConfigurationElement該元素中有一個factory屬性那么我們在類中進行相應的定義代碼如下:

代碼

        public class DefaultElement : ConfigurationElement
        {
            [ConfigurationProperty("factory")]
            public string Factory
            {
                get { return this["factory"] as string; }
                set { this["factory"] = value; }
            }
        }

 

注意在屬性定義上面我們需要注冊該屬性的ConfigurationProperty特性。

<factorys>子元素

代碼:

public class FactoryElement : ConfigurationElement
        {
            [ConfigurationProperty("name")]
            public string Name
            {
                get
                {
                    return this["name"] as string;
                }
                set
                {
                    this["name"] = value;
                }
            }
            [ConfigurationProperty("assembly")]
            public string Assembly
            {
                get
                {
                    return this["assembly"] as string;
                }
                set
                {
                    this["assembly"] = value;
                }
            }
            [ConfigurationProperty("class")]
            public string Class
            {
                get
                {
                    return this["class"] as string;
                }
                set
                {
                    this["class"] = value;
                }
            }
        }
    }

<factorys>元素是集合型元素繼承ConfigurationElementCollection

代碼:

  public class FactoryElements : ConfigurationElementCollection
        {
            protected override ConfigurationElement CreateNewElement()
            {
                return new FactoryElement();
            }
            protected override object GetElementKey(ConfigurationElement element)
            {
                return ((FactoryElement)element).Name;
            }
            public FactoryElement this[string name]
            {
                get
                {
                    return BaseGet(name) as FactoryElement;
                }
            }
        }

 

    ConfigurationElementCollection類是個抽象類,你應該顯示的實現它的CreateNewElement方法和GetElementKey方法。

<dbFactory>節點繼承於ConfigurationSection

代碼

 

        public class DbFactorySection : ConfigurationSection
        {
            [ConfigurationProperty("default")]
            public DefaultElement DefaultFactory
            {
                get { return this["default"] as DefaultElement; }
                set { this["default"] = value; }
            }
            [ConfigurationProperty("factorys")]

            public FactoryElements Factorys
            {
                get
                {
                    return this["factorys"] as FactoryElements;
                }
                set
                {
                    this["factorys"] = value;
                }
            }


        }
     配置節處理程序終於寫完了。把這四個類放在同一個工程目錄下,編譯成一個DLL,在你需要獲取配置信息的地方引用這個DLL,用DbFactorySection section = ConfigurationManager.GetSection( “dbFactory”) as DbFactorySection;試試section是不是你想要的東西呢?


免責聲明!

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



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