config中自定义配置 -尊龙游戏旗舰厅官网
尊龙游戏旗舰厅官网
收集整理的这篇文章主要介绍了
config中自定义配置
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
1. 定义自己的keyvalue
<section name="testkeyvalue" type="system.configuration.namevaluesectionhandler">section> <testkeyvalue><add key="aaa" value="aaa"/><add key="bbb" value="bbbb"/><add key="ccc" value="ccccc"/> testkeyvalue> var testkeyvalue = configurationmanager.getsection("testkeyvalue") as system.collections.specialized.namevaluecollection;2. 完全自定义section(类型自定义)
<section name="test" type="testapplication.test, testapplication">section> <test aaa="10"><bbb ccc="20">bbb><ddd><add key="aa" value="aaa">add><add key="bb" value="bbb">add>ddd> test> public class test : configurationsection/ {[configurationproperty("aaa", isrequired = true)]public int aaa{get{return (int)base["aaa"];}set{base["aaa"] = value;}}[configurationproperty("bbb", isrequired = false)]public bbb bbb{get{return (bbb)base["bbb"];}set{base["bbb"] = value;}}[configurationproperty("ddd", options = configurationpropertyoptions.isdefaultcollection, isrequired = true)]public namevaluefilecollection ddd{get{return (namevaluefilecollection)base["ddd"];}} }public class bbb : configurationelement {[configurationproperty("ccc", isrequired = true)]public int ccc{get { return (int)base["ccc"]; }set { base["ccc"] = value; }} }[configurationcollection(typeof(keyvalueconfigurationelement))] public class namevaluefilecollection : configurationelementcollection {new public keyvalueconfigurationelement this[string name]{get{return (keyvalueconfigurationelement)base.baseget(name);}}protected override configurationelement createnewelement(){return new keyvalueconfigurationelement();}protected override object getelementkey(configurationelement element){return ((keyvalueconfigurationelement)element).key;} }public class keyvalueconfigurationelement : configurationelement {[configurationproperty("key", isrequired = true)]public string key{get { return base["key"].tostring(); }set { base["key"] = value; }}[configurationproperty("value", isrequired = true)]public string value{get { return base["value"].tostring(); }set { base["value"] = value; }} } var read = configurationmanager.getsection("test") as test;3. 定义sectiongroup,sectiongroup不是collection,里面是多个section,每个section按照上面的方式定义,获取取configuration已有的定义。
<sectiongroup name="sectiongroup" ><section name="testgroup" type="testapplication.testgroup, testapplication"/><section name="testgroup2" type="system.configuration.namevaluesectionhandler"/> sectiongroup><sectiongroup><testgroup><add name="zhangsan" age="19" gender="true" /><add name="lisi" age="20" gender="false" /><add name="wangwu" age="22" gender="true" />testgroup><testgroup2><add key="a" value="aaa"/><add key="b" value="bbb"/>testgroup2> sectiongroup> public class testgroup : configurationsection {public static testgroup fromconfigfile(){return (testgroup)configurationmanager.getsection("sectiongroup");}[configurationproperty("", defaultvalue = null, isdefaultcollection = true, isrequired = false)]public xdcollection content{get { return (xdcollection)base[new configurationproperty("", typeof(xdcollection), null, configurationpropertyoptions.isdefaultcollection)]; }} }[configurationcollection(typeof(testgroupelement))] public class xdcollection : configurationelementcollection {new public testgroupelement this[string name]{get { return (testgroupelement)base[name]; }}protected override configurationelement createnewelement(){return new testgroupelement();}protected override object getelementkey(configurationelement element){return ((testgroupelement)element).name;} }public class testgroupelement : configurationelement {[configurationproperty("name", defaultvalue = "jlq", isrequired = false)]public string name { get { return (string)base["name"]; } set { base["name"] = value; } }[configurationproperty("age", defaultvalue = 18, isrequired = false)]public int age { get { return (int)base["age"]; } set { base["age"] = value; } }[configurationproperty("gender", defaultvalue = false, isrequired = false)]public bool gender { get { return (bool)base["gender"]; } set { base["gender"] = value; } } } var testgroup = configurationmanager.getsection("sectiongroup/testgroup") as testgroup; var testgroup2 = configurationmanager.getsection("sectiongroup/testgroup2") as system.collections.specialized.namevaluecollection;
4. 继承iconfigurationsectionhandler
<sectiongroup name="companyinfo"><section name="companyaddress" type="testapplication.test3,testapplication"/> sectiongroup> <companyinfo><companyaddress><companyname>axxonet solutions india pvt ltdcompanyname><doorno>1301doorno><street>13th cross, indira nagar, 2nd stagestreet><city>bangalorecity><postalcode>560038postalcode><country>indiacountry>companyaddress> companyinfo> public class test3 : iconfigurationsectionhandler {public string companyname { get; set; }public string doorno { get; set; }public string street { get; set; }public string city { get; set; }public int postalcode { get; set; }public string country { get; set; }public object create(object parent, object configcontext, xmlnode section){test3 one = new test3();one.companyname = section.selectsinglenode("companyname").innertext;one.doorno = section.selectsinglenode("doorno").innertext;one.street = section.selectsinglenode("street").innertext;one.city = section.selectsinglenode("city").innertext;one.postalcode =convert.toint32(section.selectsinglenode("postalcode").innertext);one.country = section.selectsinglenode("country").innertext;return one;} } test3 test3 = (test3)configurationmanager.getsection("companyinfo/companyaddress");
参考:http://www.codeproject.com/articles/10981/understanding-section-handlers-app-config-file
转载于:https://www.cnblogs.com/icyj/p/4650754.html
总结
以上是尊龙游戏旗舰厅官网为你收集整理的config中自定义配置的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇:
- 下一篇: javascript计时原理