. NET configuration file--Reflection + profile Storage Type instance

Source: Internet
Author: User


profile + Reflection does remove the hassle of choosing a statement. Bring a beautiful bright!


first we improved the class (next to the above):


namespace classlib{//<summary>//Interface igreetingstrategy//</summary>//&LT;REMARKS&G T EDITOR:V-LIUHCH createtime:2015/6/28 11:01:58</remarks> Public interface Igreetingstrategy {string Gre        Etingtype {get;}    void Setgreetingwords (Itextcontrol Textcontrl); }///<summary>//Class englishgreeting//</summary>/&LT;REMARKS&GT;EDITOR:V-LIUHCH Create TIME:2015/6/28 11:02:38</remarks> public class Englishgreeting:igreetingstrategy {public string Gree        Tingtype {get {return ' 中文版 ';}        } public void Setgreetingwords (Itextcontrol textcontrl) {textcontrl.text = "hello,readers"; }}///<summary>//Class chinesegreeting//</summary>//<remarks>editor:v-liuh CH CREATETIME:2015/6/28 11:02:56</remarks> public class Chinesegreeting:igreetingstrategy {private S TringGreetingtype;        Public chinesegreeting (String greetingtype) {this.greetingtype = Greetingtype; Public chinesegreeting (): This ("Chinese") {} public chinesegreeting (XmlNode section) {xmlattribut e attr = section. selectSingleNode ("params"). attributes["Greetingtype"];//gets the property value Greetingtype = attr.        value;//to assign a value to the field} public string Greetingtype {get {return greetingtype;} } public void Setgreetingwords (Itextcontrol textcontrl) {textcontrl.text = "hello." Little Reader!

"; }}///<summary>///Class Generalclass: This class may also have a lot of fields, properties, methods. Here is just shorthand///Ps:generalclass is a common type, this class internally maintains the Igreetingstrategy, call time or according to polymorphic detailed call.

</summary>//<REMARKS>EDITOR:V-LIUHCH CREATETIME:2015/6/28 11:08:04</remarks> public class G Eneralclass {private Igreetingstrategy GS; Public Generalclass (Igreetingstrategy gs) {this.gs = GS; } public string Generalproperty {get {///do some extra work, omitted here Return "<span sytle= ' color:red ' >" + GS. Greetingtype + "</span>"; }} public void Generalmethod (Itextcontrol Textcontrl) {//Do some extra work, omit GS here. Setgreetingwords (Textcontrl); Textcontrl.text = "<span sytle= ' color:red ' >" + textcontrl.text + "</span>"; Omitted....

。。。

} }}




Then, in the configuration file, define the detailed classes we want to use and the handlers that define our own labels:



  <!--greetingstrategy node and its handler configuration-  <configSections>    <section name= "Greetingstrategy" type= "Classlib.greetingconfigurationhandler,classlib"/>  </configSections>  <greetingstrategy type = "Classlib.chinesegreeting,classlib" >    <params greetingtype= "* * * Chinese greetings"/>  <!--Constructor's parameters--  </greetingStrategy>


Over here. Chinesegreeting is the class we want to use, which defines the class that handles Greetingstrategy.


Next, write a detailed implementation of this class:


Namespace classlib{public class Greetingconfigurationhandler:iconfigurationsectionhandler {/* processing has        The creation of the object for the constructor of the parameter: */////<summary>///Create configuration section handler. </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/6/30 20:34:54</remarks> public Object Create (object parent, Object conf Igcontext, System.Xml.XmlNode section) {//Gets the value of the Type property of the node type T = Type.GetType (section. attributes["type"]. Value); Object Obj=null; try {/* 2. Add a constructor to the class to be instantiated, receive a XmlNode node, pass the Greeting_stragetgy node here, and then process it in this constructor; *//assuming that T includes a constructor with a reference of XmlNode, use this construct directly function Type[] paras = {typeof (XmlNode)}; ConstructorInfo constructors = T.getconstructor (paras); if (constructors! = null) {object[] paramters = {section}; Return Activator.CreateInstance (t, paramters); The input read to the constructor of the argument} if (section. selectSingleNode ("params") = = null)//No parameter constructor {obj = Activator.CreateInstance (t); The Else//parameter constructor {/*1, which processes the policy class in this class, obtains the property value of the params node and passes it to the class that is instantiated in detail; */ Gets the value of the params node's property greetingtype XmlAttribute attr = section. selectSingleNode ("params"). attributes["Greetingtype"]; object[] Parameters = {attr. Value}; obj = activator.createinstance (t, parameters); The argument of the input read to the constructor}} catch (Exception) {return null; } return obj; } }}



In the creation method, we first infer that the Chinesegreeting class has no method to construct the node. Suppose there is. The section is directly used as a parameter and is passed in when the instance of the type is created using reflection.


Suppose there is no such construction method. We read the parameters in the XML file in this processing class and then pass in the type instantiation.


two different ways to compare. In fact, it is the same, just past the question of the reading of the reference, and the individual control, that in this class read the configuration file in the form of the constructor function is more flexible, personal preference.


Write something to test:


#region yourself Define node storage type information--reflection method            Igreetingstrategy Greetingstrategy = (igreetingstrategy) Configurationmanager.getsection ("Greetingstrategy");            if (greetingstrategy! = null)            {                Generalclass generalclass = new Generalclass (greetingstrategy);                Ltrgreetingtype.text = Generalclass.generalproperty;                Generalclass.generalmethod (Ltrgreetingword);            }            #endregion



Hey, it's relatively convenient.





The sensation of reflection is strong in pulling the change out, but where the change is taken to the most easy to modify or lower maintenance costs. So the configuration file is on the ...








. NET configuration file--Reflection + profile Storage Type instance

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.