当前位置: 首页 > news >正文

它们的定义app.config中间section节点和在执行中使用

如果现在我们需要在app.config一个节点的在下面的例子中,定义,我们需要如何进行操作?

<configSections>
    <section name="integration.config" type="UtilityComponent.WinService.Utilities.Config.Integration.IntegrationSection, UtilityComponent.WinService"/>
</configSections>

<integration.config>
    <listeners>
      <add queue="my_queue_Publish" service="PublishService"/>
      <add queue="my_queue_sub" service="SubscribeService"/>
    </listeners>
</integration.config>

那么这个节点的各个字段都代表什么意思?section中name指的是你自己定义的这个section的名字。type指的是用于接收这个section中相应字段的类,在程序执行的时候CLR会通过反射将各个字段赋值给这个类的相应属性。

在这里,listeners是一个集合,所以我们要用一个继承自ConfigurationElementCollection的类来进行接收。

   [NamedSection("integration.config")]
    public class IntegrationSection : ConfigurationSection
    {
	//这个属性是用来接收listeners这个节点集合。

这个类继承自ConfigurationElementCollection. 须要在这个属性上边 //用Attribute的方式表明相应的节点名称,这样在转换的时候,利用反射,才知道去哪个节点找这个值 [ConfigurationProperty("listeners", IsRequired = false)] public EndpointCollection EndpointCollection { get { return (EndpointCollection)this["listeners"]; } } } public class EndpointCollection : ConfigurationElementCollection, IEnumerable<EndpointElement> { protected override ConfigurationElement CreateNewElement() { return new EndpointElement(); } protected override object GetElementKey(ConfigurationElement element) { return ((EndpointElement)element).Queue; } public new IEnumerator<EndpointElement> GetEnumerator() { int count = Count; for (var i = 0; i < count; i++) { yield return BaseGet(i) as EndpointElement; } } } public class EndpointElement : ConfigurationElement { //这里须要表明是哪个字段。执行时才干利用反射把相应字段相应的值放到这个属性中来 [ConfigurationProperty("queue", IsKey = true)] public string Queue { get { return (string)this["queue"]; } set { this["queue"] = value; } } [ConfigurationProperty("service", IsKey = false, IsRequired = false)] public string Service { get { return (string)this["service"]; } set { this["service"] = value; } } public override bool IsReadOnly() { return false; } }

ConfigurationElement是最主要的类,ConfigurationElementCollection起到了协调的作用。

通过ConfigurationElementCollection的Attribute才干找到相应的配置文件的节点。之后节点找到了,一切就简单了。这时候我们就相应节点中的单个节点,写ConfigurationElement这个类,把相应的字段相应到相应的属性上边就能够了。可是这里有还有一种情况。

<configSections>
    <section name="integration.config" type="UtilityComponent.WinService.Utilities.Config.Integration.IntegrationSection, UtilityComponent.WinService"/>
</configSections>

<integration.config>
    <listeners>
      <add queue="my_queue_Publish" service="PublishService"/>
      <add queue="my_queue_sub" service="SubscribeService"/>
    </listeners>

    <service.info name="WMSScenarioService" description="WMS Use case implemented using NVS windows service."/>
</integration.config>

我们怎么去接收这个service.info?非常显然这里我们须要在IntegrationSection添加一个属性,一个直接继承自ConfigurationElement的属性来接收。注意,这里我们也须要给这个属性添加一个Attribute来告诉CRL。在反射的时候。是把哪个字段来赋给这里的属性。那为什么在上一个的样例中。我们没有在EndpointCollection中特别指明节点名字呢?由于你能够看出,listeners下边。每一个节点的名字都是add. 跟这个样例不同。

我们能够这样理解,依据Collection的Atrribute找到了listners,然后我们就用对应的ConfigurationElement来接收即可了,可是我们写这个类的时候,就要用Attribute把每一个属性写清楚。


ConfigurationManager.GetSection("integration.config") as IntegrationSection,这里CLR就会把对应的值给对应的属性。这一句代码是最为关键的代码,是CLR对配置文件进行读取解析,然使用反射来每个字段后,值分配来处理相应的属性。

版权声明:本文博客原创文章。博客,未经同意,不得转载。

相关文章:

  • 如何让mysql的自动递增的字段重新从1开始呢?(
  • read by other session的优化记录
  • sql Sever的存储过程转换为mysql的
  • 7月21日13家中国域名商(国际域名)解析量报告
  • NoSQL精粹读书笔记-第1章
  • iOS开发-App Icons的尺寸大小
  • js 时间戳转为日期格式
  • 540C: Ice Cave
  • JavaScript判断IE版本
  • EditPlus自动补全、模板配置
  • 引子——从Mac OS X的Lion说起
  • 悠然乱弹:“最好的模板引擎”Beetl 剖析及与Tiny模板引擎对比
  • c# 反射类字段
  • Oracle 学习之RMAN(十四)恢复实战--基于时间点恢复
  • HAProxy+Keepalived实现Web服务器负载均衡
  • 【JavaScript】通过闭包创建具有私有属性的实例对象
  • 【MySQL经典案例分析】 Waiting for table metadata lock
  • 2018以太坊智能合约编程语言solidity的最佳IDEs
  • HTML中设置input等文本框为不可操作
  • Java 11 发布计划来了,已确定 3个 新特性!!
  • leetcode388. Longest Absolute File Path
  • vue 配置sass、scss全局变量
  • Vue--数据传输
  • 干货 | 以太坊Mist负责人教你建立无服务器应用
  • 警报:线上事故之CountDownLatch的威力
  • 前端每日实战:70# 视频演示如何用纯 CSS 创作一只徘徊的果冻怪兽
  • 区块链将重新定义世界
  • 如何使用 OAuth 2.0 将 LinkedIn 集成入 iOS 应用
  • 使用SAX解析XML
  • 一个6年java程序员的工作感悟,写给还在迷茫的你
  • ​【已解决】npm install​卡主不动的情况
  • # 学号 2017-2018-20172309 《程序设计与数据结构》实验三报告
  • #NOIP 2014# day.1 生活大爆炸版 石头剪刀布
  • (8)Linux使用C语言读取proc/stat等cpu使用数据
  • (done) ROC曲线 和 AUC值 分别是什么?
  • (二十五)admin-boot项目之集成消息队列Rabbitmq
  • (附源码)spring boot公选课在线选课系统 毕业设计 142011
  • (五)关系数据库标准语言SQL
  • (已解决)什么是vue导航守卫
  • (转)c++ std::pair 与 std::make
  • (转)大型网站的系统架构
  • ... 是什么 ?... 有什么用处?
  • .bat批处理(七):PC端从手机内复制文件到本地
  • .form文件_SSM框架文件上传篇
  • .jks文件(JAVA KeyStore)
  • .Net6支持的操作系统版本(.net8已来,你还在用.netframework4.5吗)
  • .NET企业级应用架构设计系列之结尾篇
  • .pub是什么文件_Rust 模块和文件 - 「译」
  • ??myeclipse+tomcat
  • @Query中countQuery的介绍
  • [ C++ ] STL---stack与queue
  • [2019.3.20]BZOJ4573 [Zjoi2016]大森林
  • [Angularjs]asp.net mvc+angularjs+web api单页应用之CRUD操作
  • [Asp.net MVC]Asp.net MVC5系列——Razor语法
  • [C++]C++基础知识概述