日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

config中自定义配置

發布時間:2025/1/21 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 Ltd</companyName><doorNo>1301</doorNo><street>13th Cross, Indira Nagar, 2nd Stage</street><city>Bangalore</city><postalCode>560038</postalCode><country>India</country></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中自定义配置的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。