.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/p/3086917.html