zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    华为摄像机sdc开发_02_华为摄像机到手后的一些问题
    华为摄像机sdc开发_01_基础开发环境搭建
    LNMP环境搭建及服务器加固
    常用数据库端口号
    Linux文件查找
    SpringBoot使用jackson
    Vue.js源码解析-Vue初始化流程之动态创建DOM
    Vue.js源码解析-Vue初始化流程
    Vue.js源码解析-从scripts脚本看vue构建
    Linux系统编程-文件IO
  • 原文地址:https://www.cnblogs.com/icyJ/p/4650754.html
Copyright © 2011-2022 走看看