C#如何使用和開發自定義配置節


在日常的程序設計中,如何靈活和巧妙地運用配置信息是一個成功的設計師的首要選擇。這不僅是為了程序設計得更靈活性和可擴展性,也是為了讓你的代碼給人以清新的感覺。程序中的配置信息一般放在應用程序的app.config或web.config文件中,當然也可以自定義自己的配置文件。這些配置文件是以XML格式進行存儲和讀取的。微軟也封裝一些對這些配置文件操作的類,這些類存在於名字空間System.Configuration下,這個命名空間包含提供用於處理配置數據的編程模型的類型,當然為了使用還添加System.Configuration.dll程序集。

   現在我們先看一下這個名字空間下的幾個重要的類:

   1、ConfigurationManager,這個提供用於打開客戶端應用程序集的Configuration對象。

   2、WebConfigurationMaManager,這個提供用於打開web應用程序集的Configuration對象。

   3、ConfigurationSection ,表示配置文件中的節。

   4、ConfigurationSectionCollection ,表示配置文件中相關節的集合。

   5、ConfigurationSectionGroup ,表示配置文件中的一組相關節。

   6、ConfigurationSectionGroupCollection ,表示 ConfigurationSectionGroup 對象的集合。

   7、ConfigurationProperty ,表示屬性或配置元素的子元素。

   8、ConfigurationPropertyAttribute ,以聲明方式指示 .NET Framework,以實例化配置屬性。

   9、ConfigurationElement ,表示配置文件中的配置元素。

   10、ConfigurationElementCollection ,表示包含一個子元素集合的配置元素。

  當然這里面這常用的是ConfigurationManager類,這個類提供了兩個靜態常用的靜態方法:

       object GetSection(string sectionName)用於讀取當前應用程序默認配置的指定配置信息;

        Configuration OpenExeConfiguration(ConfigurationUserLevel userLevel)將當前應用程序配置文件打開以得到一個Configuration對象。

     以及兩個靜態屬性:AppSettins獲取當前應用程序默認配置的AppSettingsSection數據;ConnectionStrings獲取當前應用程序默認配置的ConnectionStringSection數據。

     當然由於AppSettings是一個NameValueCollection對象,因此對它的讀取和設置可以直接以AppSettings[“mySet”]的形式得到,相應的在*.config文件中的<appsettings>節下添加<add name=”mySet” value=””/>即可。

    要自定義配置節配置元素,就必須要使我們的類分別繼承自ConfigurationSection和ConfigurationElement類。那么實現這個兩個類的對象就可以加入到配置文件的<configSection>和其他元素節點中。

    這兒介紹在配置節點中加入自定義配置信息的能力(元素節點)。

    1、實現一個繼承自ConfigurationElement和ConfigurationSection的類,並添加的配置屬性加上ConfigurationPropertyAttribute特性。

    2、添加配置信息。

    3、在程序中讀取配置信息。

第一步:實現派生自ConfigurationSection和ConfigurationElement的類 

using System;
using System.Collections;
using System.Text;
using System.Configuration;
using System.Xml;
 
 
namespace MyCustomConfiguration
{
   //自定義配置節
   public class CustomSectionConfiguration : ConfigurationSection
   { 
      public CustomSectionConfiguration() { }
      public CustomSectionConfiguration(string value) { }
      //添加特性ConfigurationPropertyAttribute
      //'customAttribute'是配置文件中的元素
      //'CustomAttribute'是程序中要的屬性;
      [ConfigurationProperty("customAttribute", DefaultValue = "", IsRequired = true)]
      public string CustomAttribute
      {
          get { return (string)base["customAttribute"]; }
          set { base["customAttribute"] = value; }
         }
       //添加特性ConfigurationPropertyAttribute
       [ConfigurationProperty("customElement", DefaultValue = "", IsRequired = true)]
       public CustomElementConfiguration CustomElement
        {
           get { return (CustomElementConfiguration)base["customElement"]; }
           set { base["customElement"] = value; }
           }
    }
    
     //自定義配置元素
     public class CustomElementConfiguration : ConfigurationElement
     {
        public CustomElementConfiguration() { }
        public CustomElementConfiguration(string value1, string value2)
        {
            Value1 = value1;
            Value2 = value2;
         }
        [ConfigurationProperty("value1", DefaultValue = "", IsRequired = true)]
        public string Value1
       {
          get { return (string)base["value1"]; }
          set { base["value1"] = value; }
         }
          [ConfigurationProperty("value2", DefaultValue = "", IsRequired = true)]
          public string Value2
          {
             get { return (string)base["value2"]; }
              set { base["value2"] = value; }
             }
       
 }

第二步:在*.config中設置自定的元素 

 
 
 
         <configuration> <!-- 配置節--> <configSections> <sectionGroup name="customGroup"> <section name="customSection" type=" MyCustomConfiguration.CustomSectionConfiguration, MyCustomConfiguration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowLocation="true" allowDefinition="everywhere" /> </sectionGroup> …… <!-- 配置節設置信息 --> <customGroup> <customSection customAttribute="Custom"> <customElement Value1=”best" Vaule2=”better”/> </customSection> </customGroup> …… </configuration>

第三步:在程序中使用配置信息。 

MyCustomConfiguration.CustomSectionConfiguration config = (MyCustomConfiguration.CustomSectionConfiguration)System.Configuration.ConfigurationManager.GetSection( "customGroup/customSection");

        首先得到配置節對象:         接着就可以使用強名稱的對象和屬性了。
 

上面介紹的是單一屬性的配置,如果要配置多個對象,那么就得使用System.Configuration.ConfigurationElementCollection,這類的作用是生成多個子對象,也就是在一個標簽下可以放置多個System.Configuration.ConfigurationElement對象。

方法同上,我們必須要重寫幾方法:

ConfigurationElement CreateNewElement()//這個方法的作用是返回子對象實例;

object GetElementKey(ConfigurationElement element);//這個方法的得到對象中的鍵名;

ConfigurationElementCollectionType CollectionType{get;}//這個屬性是定義映射方式;

string ElementName{get;}//這個屬性是定義XML元素的名字。

protected override ConfigurationElement CreateNewElement()
{
       return new CustomElementConfiguration ();
}
protected override object GetElementKey(ConfigurationElement element)
{
     return ((CustomElementConfiguration )element).Name;
  }
public override ConfigurationElementCollectionType CollectionType
  {
            get { return ConfigurationElementCollectionType.BasicMap; }
    }
protected override string ElementName
{
     get { return "collection"; }
}


免責聲明!

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



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