欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

NET 自定義配置文件 Configuration

系統(tǒng) 2513 0

說(shuō)起.NET的配置文件,.NET的開發(fā)人員無(wú)人不知,無(wú)人不用,如下面的配置節(jié)點(diǎn),基本上每個(gè).NET開發(fā)的項(xiàng)目都會(huì)出現(xiàn)像下面的配置,出現(xiàn)在App.config或者Web.config中

      <connectionStrings> 

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

</connectionStrings>

<appSettings>

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

</appSettings>
    

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

其實(shí)NET已經(jīng)提供了自定義配置的基類和一些接口,對(duì)創(chuàng)建自定義的配置已經(jīng)非常方便了,下面就開始做幾個(gè)簡(jiǎn)單的實(shí)例吧

1. 創(chuàng)建一個(gè)控制臺(tái)的項(xiàng)目?CustomConfigurationDemo,添加App.Config并且引用System.configuration dll

NET 自定義配置文件 Configuration

2. 創(chuàng)建?CustomConfigurationFirst類繼承ConfigurationSection,添加屬性long Id, string Name,string FirstProperty,并且通過(guò)ConfigurationPropertyAttribute標(biāo)記屬性,第一個(gè)字符串為 配置文件中配置的屬性名,DefaultValue為默認(rèn)值,其他屬性就不一一介紹了,可以參考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; }

        }

    }
      
    

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

      <?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>
    

一切準(zhǔn)備就緒,用控制臺(tái)程序打印出我們剛剛配置的屬性看看吧!

       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("--------------------------------------------------");


    

運(yùn)行結(jié)果如下,和我們配置文件中設(shè)置的值一樣,是不是感覺(jué)很簡(jiǎn)單。

NET 自定義配置文件 Configuration

3. 有時(shí)候我們需要在配置文件中加一些子節(jié)點(diǎn),應(yīng)該怎么做呢?

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

      [ConfigurationProperty(
      
        "
      
      
        url
      
      
        "
      
      
        )]


      
      
        public
      
      
         UrlConfigurationElement UrlElement

{

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

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

}
      
    

?此時(shí)配置文件添加的配置為:

      <secondCustomConfiguration>

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

</secondCustomConfiguration>
    

?

?然后通過(guò)代碼獲取配置屬性:

       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(
      
      
        "
      
      
        --------------------------------------------------
      
      
        "
      
      );
    

?

輸出結(jié)果為:與配置文件一樣

4. 以上是創(chuàng)建一個(gè)配置節(jié)點(diǎn)的情況,假如我們修改配置為

      <secondCustomConfiguration>

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

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

</secondCustomConfiguration>
    

?

?此時(shí)就會(huì)報(bào)錯(cuò) “元素 <url> 只能在此節(jié)中出現(xiàn)一次。”怎么樣修改能支持上述的情況呢?

NET為我們提供了ConfigurationElementCollection,創(chuàng)建UrlConfigurationElementCollection繼承ConfigurationElementCollection,并且實(shí)現(xiàn)2個(gè)抽象方法

      
        public
      
      
        class
      
      
         UrlConfigurationElementCollection : ConfigurationElementCollection

    {



        
      
      
        protected
      
      
        override
      
      
         ConfigurationElement CreateNewElement()

        {

            
      
      
        return
      
      
        new
      
      
         UrlConfigurationElement();

        }



        
      
      
        protected
      
      
        override
      
      
        object
      
      
         GetElementKey(ConfigurationElement element)

        {

            
      
      
        return
      
      
         ((UrlConfigurationElement)element).Name;

        }

    }
      
    

?

創(chuàng)建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>
    

?輸出結(jié)果為:

NET 自定義配置文件 Configuration

好了,這次就簡(jiǎn)單的介紹下自定義配置的入門,其實(shí)NET提供了很多其他復(fù)雜的功能,沒(méi)有特別需求的話以上的三種自定義配置基本上就夠用了,我認(rèn)為是這樣的,還有一點(diǎn)忘記說(shuō)了,如果自定義配置節(jié)點(diǎn)太多的話可以配置sectionGroup,如果設(shè)置了分組name是必填項(xiàng),代碼獲取配置的時(shí)候加上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

點(diǎn)擊鏈接下載Demo

NET 自定義配置文件 Configuration


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 午夜影院在线播放 | 免费av大全 | 偷拍自拍网站 | 国产成人免费永久播放视频平台 | 日韩精品一区二区电影 | 黄a大片| 午夜性色一区二区三区不卡视频 | 国产毛片久久精品 | 9久热这里只有精品视频在线观看 | 久久久中文字幕日本 | 一级免费黄色免费片 | 欧洲免费在线视频 | 114美女做爰视频在线 | 欧美激情人成日本在线视频 | 干干干操操操 | 日韩欧美精品一区 | 九九热视频精品在线观看 | 亚洲欧美日韩在线 | 久操伊人 | 欧美大片在线免费观看 | 成人黄色免费视频 | 国产四虎精品8848hh | 欧美一级片手机在线观看 | 午夜理论电影在线观看亚洲 | 成人综合久久精品色婷婷 | 久操不卡 | 国产精品二区三区 | 久久精品国产亚洲一区二区 | 亚洲成人自拍偷拍 | 免费在线国产视频 | 国产精品国产精品国产专区不卡 | 牛牛碰在线视频 | 视频国产一区 | 性做久久久 | 国产高清免费 | 天天操天天干天天爽 | 国产人成激情视频在线观看 | 一级片免费在线播放 | 国产精品成熟老女人 | 爱爱视频天天干 | 天天操狠狠操夜夜操 |