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 = , 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

config中自定义配置的更多相关文章

  1. 项目文件中含有两个config文件,app.config与app1.config,如何获取app1.config中的配置

    想要通过配置文件配置C#前台画面,好奇做了以下测试:在项目中新建了app.config与app1.config两个配置文件,请教一下各位高手如果想从app1.config中读取配置信息应该如何读取?采 ...

  2. [转]WinForm和WebForm下读取app.config web.config 中邮件配置的方法

    本文转自:http://blog.csdn.net/jinbinhan/article/details/1598386 1. 在WinForm下读取 App.config中的邮件配置语句如下: Con ...

  3. web.config or app.config 中configSections配置节点

    以前还真没见过,今天看项目中有在用,简单写了个Demo,这样配置的好处就是可以自定义配置,更加模块化,直接上代码; 1.配置文件 由于我创建的是一个控制台项目,所以配置文件是App.Config:(这 ...

  4. C# 中自定义配置

    微软在ConfigurationManager类里面为我们提供了AppSetting和ConnectionStrings 两个常用配置, 但是有时候我们需要自定的配置,例如 <image lef ...

  5. c# 操作.config中AppSettings配置节

    ConfigurationSettings.AppSettings[key].ToString(); 这种方式很眼熟吧? 不过这种方式基本过时了,虽然还能用. 微软建议采用ConfigurationM ...

  6. ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节

    主要代码,一定要继续System.Configuration.ConfigurationSection,具体的节点名称可以自行修改 using System; using System.Data; u ...

  7. EntityFramework在root目录web.config中的配置设置

    未找到具有固定名称“System.Data.SqlClient”的 ADO.NET 提供程序的实体框架提供程序.请确保在应用程序配置文件的“entityFramework”节中注册了该提供程序.有关详 ...

  8. springboot读取application.properties中自定义配置

    假设在application-xxx.properties中配置 user.name=yuhk 一.在Controller中读取 @Value("{$user.name}") pr ...

  9. ASP.NET使用ConfigurationSection在Web.Config创建自定义配置节集合

    核心代码 using System; using System.Data; using System.Configuration; using System.Web; using System.Web ...

随机推荐

  1. C++中复制构造函数

    复制构造函数 复制构造函数用于: 根据另一个同类型的对象显示或隐式初始化一个对象 复制一个对象,将它作为实参传给一个函数 从函数返回时复制一个对象 初始化顺序容器中的元素 根据元素初始化式列表初始化数 ...

  2. openSUSE 13.1 Milestone 4 发布

    openSUSE 13.1 发布第四个里程碑版本,下载地址: openSUSE-Factory-KDE-Live-Build0652-x86_64.iso (949MB, MD5, torrent) ...

  3. elixir 高可用系列(五) Supervisor

    概述 OTP 平台的容错性高,是因为它提供了机制来监控所有 processes 的状态,如果有进程出现异常, 不仅可以及时检测到错误,还可以对 processes 进行重启等操作. 有了 superv ...

  4. SRS文档 王倩倩 201303014004

    设计阶段 Spec 图书管理系统functional spec:软件功能说明书, 主要用来说明软件的外部功能, 和用户的交互情况 (把软件当作一个黑盒子).从用户的角度描述软件产品的功能, 输入,输出 ...

  5. CI框架笔记

    @update 2016-4-2 13:45:35 一.目录结构 ci_demo ├─myapp 应用主目录 │ ├─autoload.php 自定义的自动加载文件(可选) │ ├─myapp.php ...

  6. Linux asyn-io for socket

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <unistd.h&g ...

  7. UICollectionView基础

    初始化部分: UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init]; self.myColl ...

  8. Maven学习总结(八)——使用Maven构建多模块项目

    在平时的Javaweb项目开发中为了便于后期的维护,我们一般会进行分层开发,最常见的就是分为domain(域模型层).dao(数据库访问层).service(业务逻辑层).web(表现层),这样分层之 ...

  9. Language

    TODO Java(jdk8),Scala,Spring,Spark,Python,Linux,C,Netty,HttpClient,Hadoop YARN,RaspberryPi Jetty,Tom ...

  10. 在C#中如何读取枚举值的描述属性

    在C#中,有时候我们需要读取枚举值的描述属性,也就是说这个枚举值代表了什么意思.比如本文中枚举值 Chinese ,我们希望知道它代表意思的说明(即“中文”). 有下面的枚举: 1 2 3 4 5 6 ...