. NET configuration file--Unified node configuration management

Source: Internet
Author: User


for. NET, if you want to handle it separately, add a specified class for this node, and if we define many custom nodes in an application, then by doing so, there will be a lot more processing classes, in order to avoid too many classes, We specify a custom node as a portal for all custom nodes, and then write only one processing class, which is the portal and reads the other nodes.


For example, a configuration file defines only one entry node:


<!--Common Configuration Storage Method--  <tracefact type= "Classlib.configmanager,classlib" >        <forum name= " Tracefact.net Community ">       <root url=" http:192.168.24.204 "/>      <replycount>20</replycount >      <pageSize>30</pageSize>      <offlineTime>20</offlineTime>    </forum>        <blog>      <root url= "http:asdakfj.com"/>      <urlMappings>        <rewriteRule>          <request>~/(\d{4})/default\.aspx</request>          <sendto>~blogdetail.aspx?year=$1</ sendto>        </rewriteRule>      </urlMappings>    </blog>           <mailserver address= " Www.baidu.com "Username=" LHC "password=" 234r32r "/>        <greetingstrategy type=" classlib.chinesegreeting, Classlib "/>      </traceFact>  



Processing class:


 <summary>//Class Generalconfighandler//</summary>/<remarks>editor:v-liuhch creat ETIME:2015/7/1 10:50:40</remarks> public class Generalconfighandler:iconfigurationsectionhandler {//        <summary>///Create configuration section handlers. </summary>//<param name= "Parent" > Parent object. </param>//<param name= "Configcontext" > Configuration Context object. </param>//<param name= "section" > Stanza XML node. </param>///<returns> Create a section handler object. </returns>//<exception cref= "system.notimplementedexception" ></exception>//<remar KS&GT;EDITOR:V-LIUHCH CREATETIME:2015/7/1 10:50:42</remarks> public Object Create (object parent, Object Confi Gcontext, XmlNode section) {//Gets the value of the node Type property type T=type.gettype (section. attributes["type"].        Value);//return ConfigManager//Direct section to pass object[] paramters={section};    The type instance that will be created object obj = null; try {obj = activator.createinstance (t, paramters);//Use the constructor with parameters} catch (E            xception) {return null;        } return obj; }    }



The class above is primarily designed to implement a specific configuration node management class, where a reference to each child node class is stored in the management class of the configuration node.



 <summary>///Generalconfighandler is created by reflection dynamically, it is actually just as a container//This class is just a container that contains a reference to its specific node configuration, and through the Tracefact root node, Gets the child node under Tracefact, and then creates an instance of the type used to map the specific child node///</summary>//&LT;REMARKS&GT;EDITOR:V-LIUHCH CREATETIME:2015/7/1 1        4:06:21</remarks> public class ConfigManager {private XmlNode sections;        Private Froumconfiguration Forumconfig;        For other nodes, the strongly typed access class is also defined as a member variable placed here ....        Public froumconfiguration Froumconfig {get {return forumconfig;}            } Public ConfigManager (XmlNode section) {this.section = section; Forumconfig = new Froumconfiguration (section.        selectSingleNode ("forum")); }}///<summary>///Specific child node Configuration forum node: processing <forum name= "Tracefact.net Community" >//</summary&gt    ;        &LT;REMARKS&GT;EDITOR:V-LIUHCH CREATETIME:2015/7/1 14:22:08</remarks> public class froumconfiguration{                Private XmlNode Forumnode; Pass the Forum node in.       Public froumconfiguration (XmlNode section) {this.forumnode=section; }///<summary>//Gets the name.        <forum name= "Tracefact.net Community" >///</summary>/<value>the name.</value>            <remarks>editor:v-liuhch createtime:2015/7/1 14:58:00</remarks> public string name{ get{return forumnode.attributes["name"].                Value; }}///<summary>//Gets the root url.:<root url= "http:192.168.24.204"/>//&lt ;/summary>//<value>the root url.</value>//<remarks>editor:v-liuhch createtime:201 5/7/1 14:56:08</remarks> public string rooturl{get{return Forumnode.selec Tsinglenode ("root"). attributes["url"].            Value; }}///<summary>//Gets the size of the Page.<pagesize>30</pagesize>//</summary>//<value>the size of the page.</value>///<remarks>ed                ITOR:V-LIUHCH createtime:2015/7/1 14:57:18</remarks> public int pagesize{get{ return int. Parse (Forumnode.selectsinglenode ("PageSize").            InnerText); }}///<summary>//Gets the reply count.<replycount>20</replycount>///& lt;/summary>//<value>the reply count.</value>//<remarks>editor:v-liuhch CreateTim E:2015/7/1 14:57:03</remarks> public int replycount{get{return int. Parse (Forumnode.selectsinglenode ("Replycount").            InnerText); }}///<summary>//Gets the offline time.<offlinetime>20</offlinetime>// /</summary>//<value>the offline time.</value>//<remarks>editor:v-liuhch CreaTETIME:2015/7/1 14:57:35</remarks> public int Offlinetime {get {return int. Parse (Forumnode.selectsinglenode ("Offlinetime").            InnerText); }        }    }


In this way, we use this class each time to access the node when we are using it:


ConfigManager config = configurationmanager.getsection ("Tracefact") as ConfigManager;            Value            ltrname.text = config.froumConfig.Name;            Ltrofflinetime.text = Config.froumConfig.OfflineTime.ToString ();            Ltrpagesize.text = Config.froumConfig.PageSize.ToString ();            Ltrreplycount.text = Config.froumConfig.ReplyCount.ToString ();            Ltrrooturl.text = Config.froumConfig.RootUrl.ToString ();





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

. NET configuration file--Unified node configuration management

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.