Customizing the section node in App. Config and its use in run

Source: Internet
Author: User

What do we need to do now if we need to define a node in app. Config as follows?

<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>

So what does each field of this node mean? Section name refers to the name of your custom section, type refers to the class used to receive the corresponding field in this section, and when the program runs, the CLR assigns individual fields to the corresponding properties of the class by reflection. Here, listeners is a collection, so we are going to use a class that inherits from ConfigurationElementCollection to receive it.

   [Namedsection ("Integration.config")] public class Integrationsection:configurationsection {//This property is used to receive listeners This node collection. This class inherits from ConfigurationElementCollection. Need to be in the top//attribute to indicate the corresponding node name, so at the time of conversion, the use of reflection, only to know which node to find this value [ConfigurationProperty ("Listeners", isrequired = Fals e)] public endpointcollection endpointcollection {get {return (endpointcollection) this["Listene RS "];    }}} public class Endpointcollection:configurationelementcollection, ienumerable<endpointelement>        {protected override ConfigurationElement Createnewelement () {return new endpointelement (); } protected Override Object Getelementkey (configurationelement Element) {return (Endpointe lement) 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 {//This needs to indicate which field, the runtime can use reflection to put the corresponding field values in this property to [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 is the most basic class, and configurationelementcollection plays a coordinating role. The node of the corresponding configuration file can be found by ConfigurationElementCollection's attribute. After the node has been found, everything is simple. At this time we will correspond to a single node in the node, write ConfigurationElement this class, the corresponding fields corresponding to the corresponding property on the top. But there is another situation here.

<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>

How are we going to receive this service.info? Obviously here we need to add a property in Integrationsection, a property that inherits directly from ConfigurationElement to receive. Note that here we also need to add a attribute to this property to tell the CRL, which field is assigned to the property here when it is reflected. So why in the previous example, we didn't specifically specify the node name in Endpointcollection? As you can see, under listeners, the name of each node is Add. This is different from the example. We can understand that, according to collection Atrribute found Listners, and then we use the corresponding configurationelement to receive it, but when we write this class, It is necessary to use attribute to write each attribute clearly.


Configurationmanager.getsection ("Integration.config") as Integrationsection, where the CLR will give the corresponding value to the corresponding property. This code is the most critical code, is the CLR to read the configuration file parsing, and then use reflection to assign each field value to the corresponding property of the process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.