标签:
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;
参考:http://www.codeproject.com/Articles/10981/Understanding-Section-Handlers-App-config-File
标签:
原文地址:http://www.cnblogs.com/icyJ/p/4650754.html