它们的定义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對配置文件進行讀取解析,然使用反射來每個字段后,值分配來處理相應的屬性。
版權聲明:本文博客原創文章。博客,未經同意,不得轉載。
轉載于:https://www.cnblogs.com/zfyouxi/p/4666316.html
總結
以上是生活随笔為你收集整理的它们的定义app.config中间section节点和在执行中使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 漫威死亡女神和灭霸(漫威死亡女神)
- 下一篇: Nginx TCP代理