.net ConfigurationSectionDesigner插件使用
最近接觸了vs2010的一款插件:ConfigurationSectionDesigner。ConfigurationSectionDesigner是一個圖型化設計.net的配置塊和自動生成需要代碼和schema定義的codeplex上的一個開源項目,現在分享出來,希望對大家有所幫助。
.Net配置體系中可以是一個Section一個模塊。本示例有2個配置塊組成,并通過Section整合到.net的默認配置文件App.config里面,用configSource屬性分2個外部配置文件分別配置。實現配置文件分模塊、分類展示,使用起來也很方便。
首先你要確保你的vs安裝了ConfigurationSectionDesigner插件,如果沒有,下載安裝即可。http://csd.codeplex.com/releases
一:新建一個wpf工程文件,首先Add New Item,找到剛才安裝的插件自帶的模版,命名為MyConfig.csd。然后建立配置項模型,在新建的.csd文件左邊的Tollbox中有相關的控件,首先拖一個ConfigurationSection命名為MyConfig,分別新增三個Attribute,在屬性頁分別選擇Attribute的Type,再新增兩個Element(SystemConfig、MainFrameService),對于SystemConfig,要拖一個ConfigurationElement命名為SystemConfigElement,并新增三個對應的Attribute,同時選擇對應的Type,然后把上面SystemConfig的Type指定為SystemConfigElement,這樣就關聯在一起了,同時出現一個指向的箭頭。然后就是MainFrameService了,該節點時一個集合,所以要拖一個ConfigurationElementCollection命名為MainFrameServiceCollection,再拖一個ConfigurationElement命名為Service,同時新增對應的Attribute和指定對應的Type.這樣把MainFrameService的Type指定為MainFrameServiceCollection,MainFrameServiceCollection的Item Type指定為Service。配置模型就建立成功了,如下圖,保存時,會生成對應的配置項代碼。
同樣,再插入一個模板,命名為MyConfig2.csd,如下圖:
?
至此,配置模型就建立好了。
二:通過以上工作,我們建立了配置的基本結構,下面是配置的實現結果,如下:
myConfig.config:
<?xml version="1.0" encoding="utf-8" ?> <!--<configuration><configSections><section name="myConfig2" type="WpfApplication4.MyConfig2, WpfApplication4"/></configSections>--><myConfig xmlns="urn:WpfApplication4" localIPV4Scope="110.0" tcpCommTimeout="60" description="abcdefg"><systemConfig encoding="UTF-8" output="C:\" style="江南大叔"/><mainFrameService><service name="n1" channel="c1" server="10.112.40.241" servicetype="t1"/><service name="n2" channel="c2" server="10.112.40.242" servicetype="t2"/><service name="n3" channel="c3" server="10.112.40.243" servicetype="t3"/><service name="n4" channel="c4" server="10.112.40.244" servicetype="t4"/></mainFrameService></myConfig> <!--</configuration>-->
myConfig2.config:
<?xml version="1.0" encoding="utf-8" ?> <!--<configuration><configSections><section name="myConfig2" type="WpfApplication4.MyConfig2, WpfApplication4"/></configSections>--><myConfig2 xmlns="urn:WpfApplication4"><hW><hW hello="h1" word="true"/><hW hello="h2" word="false"/><hW hello="h3" word="true"/></hW></myConfig2> <!--</configuration>-->
App.config:
<?xml version="1.0" encoding="utf-8" ?> <configuration><configSections><section name="myConfig" type="WpfApplication4.MyConfig, WpfApplication4"/><section name="myConfig2" type="WpfApplication4.MyConfig2, WpfApplication4"/></configSections><myConfig configSource="Configs\myConfig.config"/><myConfig2 configSource="Configs\myConfig2.config"/> </configuration>
? 三:這樣就算配置模塊很多,config文件中也顯得很清晰,使用也很方便:
var a = MyConfig.Instance.LocalIPV4Scope;var b = MyConfig.Instance.SystemConfig.Encoding;var ss = MyConfig.Instance.MainFrameService;foreach (Service s in ss){string a1 = s.Name;string a2 = s.Channel;string a3 = s.Server;string a4 = s.Servicetype;}//---------------------------------------------var mm = MyConfig2.Instance.HW;foreach (HW m in mm){string a1 = m.Hello;bool a2 = m.Word;}
?
ps:參考http://www.cnblogs.com/bhtx/archive/2013/03/30/2990242.html
?
轉載于:https://www.cnblogs.com/yuanli/archive/2013/05/19/3086917.html
總結
以上是生活随笔為你收集整理的.net ConfigurationSectionDesigner插件使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 给个说说?
- 下一篇: 哈利波特中类似照明弹的咒语