一步一步教你玩轉(zhuǎn).NETFramework的配置文件app.config_第1頁(yè)
一步一步教你玩轉(zhuǎn).NETFramework的配置文件app.config_第2頁(yè)
一步一步教你玩轉(zhuǎn).NETFramework的配置文件app.config_第3頁(yè)
一步一步教你玩轉(zhuǎn).NETFramework的配置文件app.config_第4頁(yè)
一步一步教你玩轉(zhuǎn).NETFramework的配置文件app.config_第5頁(yè)
免費(fèi)預(yù)覽已結(jié)束,剩余5頁(yè)可下載查看

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、一步一步教你玩轉(zhuǎn).NETFramework 的配置文件 app.config在一般的項(xiàng)目中,為了使你的代碼更加靈活,更方便調(diào)整,減少不必要的 hardcode,我們都在 config 中添加許多配置信息,一般可以選擇.NET 自帶的配置文件形式 app.config 或者 web 項(xiàng)目中的web.config 來(lái)完成配置工作。.NET 中提供了幾個(gè)和配置有關(guān)的類(lèi)來(lái)支持用完輕松的完成配置文件的讀寫(xiě)設(shè)置:System.Configuration.ConfigurationSectionGroup一般和你項(xiàng)目中使用的 Assambly 保持 1:1 的對(duì)應(yīng)關(guān)系,這樣劃分使得結(jié)構(gòu)相對(duì)清晰,權(quán)責(zé)明確。當(dāng)

2、然你可以不使用它,這樣一旦你的 Assambly 在別的地方要被重用時(shí),找出相應(yīng)的config 信息就變得很困難。System.Configuration.ConfigurationSection維護(hù)一個(gè)相對(duì)獨(dú)立的配置節(jié),使用時(shí)需現(xiàn)在節(jié)點(diǎn)下聲明。我們熟悉的以及就是.NET 為我們預(yù)留的一個(gè) Section。System.Configuration.ConfigurationElementCollection&System.Configuration.ConfigurationElement就是 Section 下具體的配置信息和配置信息的集合了。下面來(lái)看看怎么使用這些類(lèi)玩轉(zhuǎn) app.c

3、onfig1.初級(jí)玩法最初級(jí)的用法當(dāng)然是使用,我們?cè)?app.config 中添加訪問(wèn)它publicclassAppSettingConfigpublicstringresultValue;publicAppSettingConfig()this.resultValue=ConfigurationManager.AppSettingsMyConfigString.ToString();TestMethodpublicvoidTestAppSettingConfigNode()AppSettingConfigappCon=newAppSettingConfig();Assert.AreEqual

4、(TestConfigData,appCon.resultValue);沒(méi)有問(wèn)題!我們加個(gè) Section 來(lái)看看如何訪問(wèn):注意我們?cè)?section 的 type 中給出了System.Configuration.DictionarySectionHandler,這也限制了我們?cè)诰唧w的ConfigurationElement 中只能使用的形式,使得我們 GetSection()方法返回的是一個(gè)IDictory 對(duì)象,我們可以根據(jù) Key 來(lái)取得相應(yīng)的值publicclassSectionConfigpublicstringresultValue;publicSectionConfig()Sy

5、stem.Configuration.Configurationconfig=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);IDictionarydic=ConfigurationManager.GetSection(MySectionGroup/MySecondSection)asIDictionary;this.resultValue=dicSecond.ToString();TestMethodpublicvoidTestSectionGroupConfigNode()(SectionConf

6、igsc=newSectionConfig();Assert.AreEqual(FirstSection,sc.resultValue);還是沒(méi)問(wèn)題。2 .中級(jí)玩法.NET 支持對(duì)上述提到的 configuration 類(lèi)進(jìn)行擴(kuò)展,我們可以定義自己的Section。繼承自基類(lèi) System.Configuration.ConfigurationSection,ConfigurationSection 已經(jīng)提供了索引器用來(lái)獲取設(shè)置數(shù)據(jù)。在類(lèi)中加上 ConfigurationProperty 屬性來(lái)定義 Section 中的 Element:publicclassCustomSection:Sys

7、tem.Configuration.ConfigurationSection(ConfigurationProperty(sectionId,IsRequired=true,IsKey=true)publicintSectionIdgetreturn(int)basesectionId;setbasesectionId=value;ConfigurationProperty(sectionValue,IsRequired=false)publicstringSectionValuegetreturnbasesectionValue.ToString();setbasesectionValue=

8、value;操作此 Section,我們將其動(dòng)態(tài)加入 app.con 巾 g 中,并讀出來(lái):publicclassCustomSectionBrokerprivateCustomSectioncustomSection=null;publicvoidInsertCustomSection()customSection=newCustomSection();customSection.SectionId=1;customSection.SectionValue=TheFirstValue;System.Configuration.Configurationconfig=Configuration

9、Manager.OpenExeConfiguration(ConfigurationUserLevel.None);config.Sections.Add(CustomSection,customSection);config.Save(ConfigurationSaveMode.Minimal);)publicintGetCustomSectionID()(System.Configuration.Configurationconfig=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);CustomS

10、ectioncs=config.GetSection(CustomSection)asCustomSection;returncs.SectionId;)TestMethodpublicvoidTestCustomSection()(CustomSectionBrokercb=newCustomSectionBroker();cb.InsertCustomSection();Assert.AreEqual(1,cb.GetCustomSectionID();)可以看下現(xiàn)在 app.con 巾 g 文件的變化:這種方式呢?我們?yōu)镃onfigurationElement 類(lèi)的子類(lèi) CustomSe

11、ctionElementA,然后修改 CustomSection 類(lèi)中的 Property,使得類(lèi)型不再是 int 或 string,而是我們創(chuàng)建的新類(lèi)CustomSectionElementA.由于 ChildCustomSectionA 和 ChildCustomSectionB 的結(jié)構(gòu)相對(duì)一致,根據(jù)面向?qū)ο蟮拈_(kāi)發(fā)封閉原則,我們可以先抽象出一個(gè) base 類(lèi),然后讓 ChildCustomSectionA,ChildCustomSectionB 分別繼承自此 base 類(lèi),當(dāng)以后要添加更多的ChildCustomSectionCChildCustomSectionD時(shí),使用這種 Templ

12、ate 的設(shè)計(jì)模式,將更加靈活。publicabstractclassCustomSectionElementBase:System.Configuration.ConfigurationElementConfigurationProperty(childId,IsRequired=true,IsKey=true)publicintChildIDgetreturn(int)basechildId;setbasechildId=value;ConfigurationProperty(childValue,IsRequired=true)publicstringChildValuegetretur

13、nbasechildValue.ToString();setbasechildValue=value;publicclassCustomSectionElementA:CustomSectionElementBasepublicCustomSectionElementA。base.ChildID=1;base.ChildValue=ChildApublicclassCustomSectionElementB:CustomSectionElementBase(publicCustomSectionElementB()(base.ChildID=2;base.ChildValue=ChildB;完

14、成了 ConfigurationElement 的實(shí)現(xiàn),我們可以改寫(xiě)我們上一個(gè)例子中定義的 CustomSection 類(lèi)了:增加了一個(gè)單獨(dú)的 Section,名為CustomSection,并且包含了我們創(chuàng)建的 2 個(gè)configurationproperty。我們還可以繼續(xù)作擴(kuò)展,現(xiàn)在我們的config 中 section的部分呈現(xiàn)的是CustomSectionsectionId=1sectionValue=TheFirstValue 便,我們是不是可以繼續(xù)擴(kuò)展,將其變成比較合理的ChildCustomSectionAchildId=1childValue=,這樣對(duì)于復(fù)雜的配置信息仍然不方

15、ChildA“ChildB創(chuàng)建擴(kuò)展自publicclassCustomSectionWithChildElement:System.Configuration.ConfigurationSection(privateconststringelementChildA=childSectionA;privateconststringelementChildB=childSectionB;ConfigurationProperty(elementChildA,IsRequired=true,IsKey=true)publicCustomSectionElementAChildSectionAgetr

16、eturnbaseelementChildAasCustomSectionElementA;setbaseelementChildA=value;ConfigurationProperty(elementChildB,IsRequired=true)publicCustomSectionElementBChildSectionBgetreturnbaseelementChildBasCustomSectionElementB;setbaseelementChildB=value;publicclassCustomSectionWithChildElementBrokerprivateCusto

17、mSectionWithChildElementcustomSection=null;publicvoidInsertCustomSection()customSection=newCustomSectionWithChildElement。;customSection.ChildSectionA=newCustomSectionElementA();customSection.ChildSectionB=newCustomSectionElementB();System.Configuration.Configurationconfig=ConfigurationManager.OpenEx

18、eConfiguration(ConfigurationUserLevel.None);config.Sections.Add(CustomSectionWithChildElement,customSection);config.Save(ConfigurationSaveMode.Minimal);publicintGetCustomSectionChildAID()System.Configuration.Configurationconfig=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);C

19、ustomSectionWithChildElementcswe=config.GetSection(CustomSectionWithChildElement)asCustomSectionWithChildElement;returncswe.ChildSectionA.ChildID;紅色字體就是修改的地方了,將 Property 改成我們自定義類(lèi)的形式.測(cè)試代碼如下:TestMethodpublicvoidTestCustomSectionWithChildElement()CustomSectionWithChildElementBrokercweb=newCustomSection

20、WithChildElementBroker();cweb.InsertCustomSection();Assert.AreEqual(1,cweb.GetCustomSectionChildAID();看看運(yùn)行后我們的 app.config 變成什么樣子了:cool,好像完成了我們的要求。下面為我們的 CustomSectionWithChildElement 外面再加一層 SectionGroup.publicclassCustomSectionGroup:System.Configuration.ConfigurationSectionGroupConfigurationProperty

21、(customSectionA,IsRequired=true,=true)publicCustomSectionWithChildElementSectionAgetreturnbase.SectionscustomSectionAasCustomSectionWithChildElement;setthis.Sections.Add(customSectionA,value);publicclassCustomSectionGroupWithChildElementBrokerprivateCustomSectionWithChildElementcustomSection=nullpub

22、licvoidInsertCustomSectionGroup()customSection=newCustomSectionWithChildElement。;IsKeyCustomSectionGroupsectionGroup=newCustomSectionGroup();System.Configuration.Configurationconfig=ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);if(config.GetSectionGroup(customSectionGroup)=null)config.SectionGroups.Add(customSectionGroup,sectionGroup);sectionGroup.SectionA=customSection;config.Save(ConfigurationSaveMode.Minimal);publicintGetCustomSectionChildAID()System.Configuration.Configurationconfig=ConfigurationManager.OpenE

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論