c# - Extending key value pair functionality in app.config -
I have several rules in my console application that I want to turn on and off in app.config.
The App.config key values support joints, so good times - I can just define some rule keys and store the flag in value.
Now I want to add an unspecified comment field to get a rule description, however, there is no facility to do this.
Obviously I can roll my own config file and read it with standard XML methods, but I'm thinking that there should be a better way of doing this, this app.config file inside.
I could also include comments within key / value but it also seems unsatisfactory.
Any thoughts?
OK, under the initial lead of Powell and I got some code, Managed in Hope this is useful for other weary passengers! : -)
App.config
& lt ;? Xml version = "1.0" encoding = "UTF-8"? & Gt; & Lt; Configuration & gt; & Lt; ConfigSections & gt; & Lt; Section name = "rule set" type = "expanded kvp. Address of rule, extended kvp" / & gt; & Lt; / ConfigSections & gt; & Lt; Startup & gt; & Lt; Supported serial version = "v4.0" sku = ".NETFramework, version = v4.5" /> & Lt; / Startup & gt; & Lt; Rule-Set & gt; & Lt; TheRules & gt; & Lt; Add ruleId = "RL101" ruleActive = "true" ruleDesc = "do not do it" /> & Lt; Add ruleId = "RL202" ruleactive = "false" rule.dc = "avoid this" /> & Lt; Add ruleId = "RL303" RandomActive = "True" ruleDocus = "Other Missing" /> & Lt; / TheRules & gt; & Lt; / Rule-set & gt; & Lt; / Configuration & gt;
Program.cs
using the system; Using System.configuration; Namespace Extended KVP {Class Program {Static Zero Main (String [] ARG) {var connectionManagerDataTransaction = Configuration ManagerGetScroll (Regulations if (connectionManagerDataSection = Null!) {Foreach (RuleElement element in connectionManagerDataSection.ConnectionManagerEndpoints) {Console.WriteLine (String .format ("ruleid: {0}, RuleActive: {1}, RuleDesc: {2}", element.RuleId, Element.RuleActive, element.RuleDesc));}}}}}
ConfigReader.cs
Using System.configuration; Namespace Extended KVP {Public Class Rule Class: Config Ration Description {public key string sectionName = "ruleset"; personal prong string RuleCollectionName = "TheRules"; [ConfigurationPolicy (RuleCollection), AddItemName = "Add")] (RuleCollection) base [RuleCollectionName];}}} Public class rule selection: ConfigurationAllanCollection {Create safe override configuration elementNew (); } Safe Override Object GetElementKey {RuleElement element}. RuleId; }} Public class RuleElement: ConfigurationElement {{ConfigurationProperty ("ruleid", IsRequired = true)} public string ruleid {{return return (string) this ["ruleid"]; } Set {this ["ruleId"] = value; }} [ConfigurationProperty ("ruleDesc", IsRequired = true)] Public String RuleDesc {{return (string) this ["ruleDesc"]; } Set {this ["ruleDesc"] = value; }} [ConfigProperty ("ruleActive", IsRequired = false, DefaultValueXML = false)] Public bool RuleActive {{return (bool) this ["ruleActive"]; } Set {this ["ruleActive"] = value; }}}}
Comments
Post a Comment