亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

NET 自定義配置文件 Configuration

系統 2427 0

說起.NET的配置文件,.NET的開發人員無人不知,無人不用,如下面的配置節點,基本上每個.NET開發的項目都會出現像下面的配置,出現在App.config或者Web.config中

      <connectionStrings> 

    <add name=
      
        "
      
      
        DbConnectionString
      
      
        "
      
       connectionString=
      
        "
      
      
        ....
      
      
        "
      
      />

</connectionStrings>

<appSettings>

    <add key=
      
        "
      
      
        LogFilePath
      
      
        "
      
       value=
      
        "
      
      
        c:/Logs/
      
      
        "
      
      />

</appSettings>
    

一般的項目用NET提供的配置節點就已經夠用了,但是如果項目的配置文件很多很多,appSettings就會出現大量的配置,基本上都是key和value的組合,如果再加上命名不易讀,維護就會很麻煩,又或者你自己寫了一個框架給別人用,需要定義符合自己的配置節點,所以有些時候我們需要自定義一些配置.

其實NET已經提供了自定義配置的基類和一些接口,對創建自定義的配置已經非常方便了,下面就開始做幾個簡單的實例吧

1. 創建一個控制臺的項目?CustomConfigurationDemo,添加App.Config并且引用System.configuration dll

NET 自定義配置文件 Configuration

2. 創建?CustomConfigurationFirst類繼承ConfigurationSection,添加屬性long Id, string Name,string FirstProperty,并且通過ConfigurationPropertyAttribute標記屬性,第一個字符串為 配置文件中配置的屬性名,DefaultValue為默認值,其他屬性就不一一介紹了,可以參考ConfigurationPropertyAttribute的注釋信息。

      
        public
      
      
        class
      
      
         CustomConfigurationFirst : ConfigurationSection

    {

        
      
      
        private
      
      
        static
      
      
         CustomConfigurationFirst setting;

        
      
      
        public
      
      
        static
      
      
         CustomConfigurationFirst Setting

        {

            
      
      
        get
      
      
         

            {

                
      
      
        if
      
      (setting == 
      
        null
      
      
        )

                    setting 
      
      = (CustomConfigurationFirst)ConfigurationManager.GetSection(
      
        "
      
      
        firstCustomConfiguration
      
      
        "
      
      
        );

                
      
      
        return
      
      
         setting;

            }

        }



        [ConfigurationProperty(
      
      
        "
      
      
        id
      
      
        "
      
      , DefaultValue = 
      
        "
      
      
        1
      
      
        "
      
      , IsRequired = 
      
        true
      
      
        )]

        
      
      
        public
      
      
        long
      
      
         Id

        {

            
      
      
        get
      
       { 
      
        return
      
       (
      
        long
      
      )
      
        this
      
      [
      
        "
      
      
        id
      
      
        "
      
      
        ]; }

            
      
      
        set
      
       { 
      
        this
      
      [
      
        "
      
      
        id
      
      
        "
      
      ] =
      
         value; }

        }



        [ConfigurationProperty(
      
      
        "
      
      
        name
      
      
        "
      
      , DefaultValue = 
      
        "
      
      
        Lily
      
      
        "
      
      , IsRequired = 
      
        true
      
      
        )]

        
      
      
        public
      
      
        string
      
      
         Name 

        {

            
      
      
        get
      
       { 
      
        return
      
       (
      
        string
      
      )
      
        this
      
      [
      
        "
      
      
        name
      
      
        "
      
      
        ]; }

            
      
      
        set
      
       { 
      
        this
      
      [
      
        "
      
      
        name
      
      
        "
      
      ] =
      
         value; }

        }



        [ConfigurationProperty(
      
      
        "
      
      
        firstProperty
      
      
        "
      
      , DefaultValue = 
      
        "
      
      
        Property1
      
      
        "
      
      , IsRequired = 
      
        true
      
      
        )]

        
      
      
        public
      
      
        string
      
      
         FirstProperty

        {

            
      
      
        get
      
       { 
      
        return
      
       (
      
        string
      
      )
      
        this
      
      [
      
        "
      
      
        firstProperty
      
      
        "
      
      
        ]; }

            
      
      
        set
      
       { 
      
        this
      
      [
      
        "
      
      
        firstProperty
      
      
        "
      
      ] =
      
         value; }

        }

    }
      
    

我們自定義的配置創建好了,現在需要添加配置到App.config文件中,如下圖所示,首先需要創建configSections,把自定義的節點加進去,name隨便填寫(填寫的值將會與代碼中的ConfigurationManager.GetSection("firstCustomConfiguration")名稱對應),type需要填寫自定義配置節點類的全名稱和程序集

      <?xml version=
      
        "
      
      
        1.0
      
      
        "
      
       encoding=
      
        "
      
      
        utf-8
      
      
        "
      
       ?>

<configuration>

  <configSections>

    <section name=
      
        "
      
      
        firstCustomConfiguration
      
      
        "
      
       type=
      
        "
      
      
        CustomConfigurationDemo.CustomConfigurationFirst,CustomConfigurationDemo
      
      
        "
      
      />

  </configSections>



  <firstCustomConfiguration id=
      
        "
      
      
        12
      
      
        "
      
       name=
      
        "
      
      
        name
      
      
        "
      
       firstProperty=
      
        "
      
      
        property2
      
      
        "
      
      />

  

</configuration>
    

一切準備就緒,用控制臺程序打印出我們剛剛配置的屬性看看吧!

       Console.WriteLine("----CustomConfigurationFirst---------------------");

 CustomConfigurationFirst settingFirst = CustomConfigurationFirst.Setting;

 Console.WriteLine("settingFirst.Id:" + settingFirst.Id);

 Console.WriteLine("settingFirst.Name:" + settingFirst.Name);

 Console.WriteLine("settingFirst.FirstProperty"+ settingFirst.FirstProperty);

 Console.WriteLine("--------------------------------------------------");


    

運行結果如下,和我們配置文件中設置的值一樣,是不是感覺很簡單。

NET 自定義配置文件 Configuration

3. 有時候我們需要在配置文件中加一些子節點,應該怎么做呢?

先創建一個?UrlConfigurationElement:ConfigurationElement,在ConfigurationElement里面添加屬性和在Section里面添加是一樣的,然后我們創建一個CustomConfigurationSecond : ConfigurationSection,并創建一個屬性的類型是UrlConfigurationElement的,如下圖所示:

      [ConfigurationProperty(
      
        "
      
      
        url
      
      
        "
      
      
        )]


      
      
        public
      
      
         UrlConfigurationElement UrlElement

{

      
      
      
        get
      
       { 
      
        return
      
       (UrlConfigurationElement)
      
        this
      
      [
      
        "
      
      
        url
      
      
        "
      
      
        ]; }

      
      
      
        set
      
       { 
      
        this
      
      [
      
        "
      
      
        url
      
      
        "
      
      ] =
      
         value; }

}
      
    

?此時配置文件添加的配置為:

      <secondCustomConfiguration>

    <url name=
      
        "
      
      
        baidu
      
      
        "
      
       url=
      
        "
      
      
        http://www.baidu.com
      
      
        "
      
       />

</secondCustomConfiguration>
    

?

?然后通過代碼獲取配置屬性:

       Console.WriteLine(
      
        "
      
      
        ----CustomConfigurationSecond---------------------
      
      
        "
      
      
        );

 CustomConfigurationSecond settingSecond 
      
      =
      
         CustomConfigurationSecond.Setting;

 Console.WriteLine(
      
      
        "
      
      
        settingSecond.UrlElement.Name:
      
      
        "
      
       +
      
         settingSecond.UrlElement.Name);

 Console.WriteLine(
      
      
        "
      
      
        settingSecond.UrlElement.Url:
      
      
        "
      
       +
      
         settingSecond.UrlElement.Url);

 Console.WriteLine(
      
      
        "
      
      
        --------------------------------------------------
      
      
        "
      
      );
    

?

輸出結果為:與配置文件一樣

4. 以上是創建一個配置節點的情況,假如我們修改配置為

      <secondCustomConfiguration>

    <url name=
      
        "
      
      
        baidu
      
      
        "
      
       url=
      
        "
      
      
        http://www.baidu.com
      
      
        "
      
       />

    <url name=
      
        "
      
      
        google
      
      
        "
      
       url=
      
        "
      
      
        http://www.google.com
      
      
        "
      
       />

</secondCustomConfiguration>
    

?

?此時就會報錯 “元素 <url> 只能在此節中出現一次。”怎么樣修改能支持上述的情況呢?

NET為我們提供了ConfigurationElementCollection,創建UrlConfigurationElementCollection繼承ConfigurationElementCollection,并且實現2個抽象方法

      
        public
      
      
        class
      
      
         UrlConfigurationElementCollection : ConfigurationElementCollection

    {



        
      
      
        protected
      
      
        override
      
      
         ConfigurationElement CreateNewElement()

        {

            
      
      
        return
      
      
        new
      
      
         UrlConfigurationElement();

        }



        
      
      
        protected
      
      
        override
      
      
        object
      
      
         GetElementKey(ConfigurationElement element)

        {

            
      
      
        return
      
      
         ((UrlConfigurationElement)element).Name;

        }

    }
      
    

?

創建CustomConfigurationThird : ConfigurationSection

       [ConfigurationProperty(
      
        "
      
      
        urls
      
      
        "
      
      
        )]

 [ConfigurationCollection(
      
      
        typeof
      
      
        (UrlConfigurationElementCollection),AddItemName 
      
      = 
      
        "
      
      
        addUrl
      
      
        "
      
      
        ,ClearItemsName 
      
      = 
      
        "
      
      
        clearUrls
      
      
        "
      
      
        , RemoveItemName 
      
      = 
      
        "
      
      
        RemoveUrl
      
      
        "
      
      
        )]

 
      
      
        public
      
      
         UrlConfigurationElementCollection UrlElements

 {

     
      
      
        get
      
       { 
      
        return
      
       (UrlConfigurationElementCollection)
      
        this
      
      [
      
        "
      
      
        urls
      
      
        "
      
      
        ]; }

     
      
      
        set
      
       { 
      
        this
      
      [
      
        "
      
      
        urls
      
      
        "
      
      ] =
      
         value; }

 }
      
    

?

配置文件為

      <thirdCustomConfiguration>

    <urls>

      <addUrl name=
      
        "
      
      
        google
      
      
        "
      
       url=
      
        "
      
      
        http://www.google.com
      
      
        "
      
       />

      <addUrl name=
      
        "
      
      
        sina
      
      
        "
      
       url=
      
        "
      
      
        http://www.sina.com
      
      
        "
      
       />

      <addUrl name=
      
        "
      
      
        360buys
      
      
        "
      
       url=
      
        "
      
      
        http://www.360buys.com
      
      
        "
      
       />

    </urls>

</thirdCustomConfiguration>
    

?輸出結果為:

NET 自定義配置文件 Configuration

好了,這次就簡單的介紹下自定義配置的入門,其實NET提供了很多其他復雜的功能,沒有特別需求的話以上的三種自定義配置基本上就夠用了,我認為是這樣的,還有一點忘記說了,如果自定義配置節點太多的話可以配置sectionGroup,如果設置了分組name是必填項,代碼獲取配置的時候加上sectionGroup name就可以了,如:

?setting = (CustomConfigurationFirst)ConfigurationManager.GetSection("customGroup/firstCustomConfiguration");

       <configSections>

    <sectionGroup name=
      
        "
      
      
        customGroup
      
      
        "
      
      >

      <section name=
      
        "
      
      
        firstCustomConfiguration
      
      
        "
      
       type=
      
        "
      
      
        CustomConfigurationDemo.CustomConfigurationFirst,CustomConfigurationDemo
      
      
        "
      
      />

    </sectionGroup>

    <section name=
      
        "
      
      
        secondCustomConfiguration
      
      
        "
      
       type=
      
        "
      
      
        CustomConfigurationDemo.CustomConfigurationSecond,CustomConfigurationDemo
      
      
        "
      
      />

    <section name=
      
        "
      
      
        thirdCustomConfiguration
      
      
        "
      
       type=
      
        "
      
      
        CustomConfigurationDemo.CustomConfigurationThird,CustomConfigurationDemo
      
      
        "
      
      />

  </configSections>



  <customGroup>

    <firstCustomConfiguration id=
      
        "
      
      
        12
      
      
        "
      
       name=
      
        "
      
      
        name
      
      
        "
      
       firstProperty=
      
        "
      
      
        property2
      
      
        "
      
      />

  </customGroup>
    

?

想深入研究的話可以參考 MSDN

點擊鏈接下載Demo

NET 自定義配置文件 Configuration


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 一区在线观看 | a毛片全部免费播放 | 国产精品1区2区3区 国产精品1区2区3区在线播放 | 成人国产激情福利久久精品 | 久久人与动人物a级毛片 | 波多野结衣一区2区3区 | 国产视频欧美 | 亚洲欧美精品天堂久久综合一区 | 精品国产免费一区二区 | 免费欧洲毛片a级视频老妇女 | 精品国产品欧美日产在线 | 99在线视频精品费观看视 | 色综合一区 | 亚洲精品亚洲人成在线播放 | 国产香蕉一区二区精品视频 | 亚洲精品美女一区二区三区乱码 | 中文国产成人精品久久无广告 | 亚洲高清免费在线观看 | 久久精品视频7 | 亚欧人成精品免费观看 | 激情一区二区三区 | 黄色片网站视频 | 伊人网站视频 | 天天干天天插天天射 | 米奇精品一区二区三区在线观看 | 玖玖成人| 亚洲精品99久久久久中文字幕 | 国产精品一区二区三区四区 | 日本在线亚州精品视频在线 | 日本手机在线视频 | 色福利网| 97久久精品国产成人影院 | 国产精品一区二区久久精品涩爱 | 性生生活三级视频观看 | 国产日日干 | 欧美高清成人 | 国产区一区二区三 | 国产综合久久久久影院 | 成人a毛片| 欧美成人免费xxx大片 | 久操视频在线观看 |