app.config 配置多项 配置集合 自定义配置(2)
上一篇说了利用app.config自定义节点配置,那是利用工具来实现,其实也一全部编码的方式来实现.
举一个栗子.
Simpson一家有父亲James,母亲Kate,和三个儿女Jim,Aaron和Lukas.结构如下.
<family surname="Simpson">
<father firstName="James" lastName="Simpson"/>
<mother firstName="Kate" lastName="Simpson"/>
<children >
<add firstName="Jim" lastName="Simpson"/>
<add firstName="Aaron" lastName="Simpson"/>
<add firstName="Lukas" lastName="Simpson"/>
</children>
</family>
加在Config中像下面这个样子.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="family" type="SimpsonFamily.Config.FamilySection,SimpsonFamily"/>
</configSections>
<family surname="Simpson">
<father firstName="James" lastName="Simpson"/>
<mother firstName="Kate" lastName="Simpson"/>
<children >
<add firstName="Jim" lastName="Simpson"/>
<add firstName="Aaron" lastName="Simpson"/>
<add firstName="Lukas" lastName="Simpson"/>
</children> </family>
</configuration>
注意上面代码中的type,"SimpsonFamily.Config.FamilySection"表示这个section的类的路径(命名空间+类名),"SimpsonFamily"其实就是程序集的名字,因为这里要用到反射.
一般还可以有 "Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"这种,这里我们省略.
接下来咱们编码:
在名为SimpsonFamily的工程中建立文件夹config,增加class ,命名 FamilySection,继承自System.Configuration.ConfigurationSection类(需要添加引用).
这个家庭的姓氏为Simpson,通过XML中的属性来实现的.father和mother通过子节点实现.而孩子们又是集合.应该这样编码.
1.增加一个新类--儿女的集合Children,继承自ConfigurationElementCollection,实现他的一些集合操作方法;
class Children : ConfigurationElementCollection
{
protected override object GetElementKey(ConfigurationElement element)
{
return ((Child)element).FirstName;
} protected override ConfigurationElement CreateNewElement()
{
return new Child();
} public Child this[int i]
{
get { return (Child)base.BaseGet(i); }
} public Child this[string key]
{
get { return (Child)base.BaseGet(key); }
} }
2.新增类:father,mother,child,有属性firstName和lastName.集成自ConfigurationElement
class Person : ConfigurationElement
{
[ConfigurationProperty("firstName", IsRequired = true, IsKey = true)]
public string FirstName
{
get { return (string)base["firstName"]; }
set { base["firstName"] = value; }
}
[ConfigurationProperty("lastName", IsRequired = true)]
public string LastName
{
get { return (string)base["lastName"]; }
set { base["lastName"] = value; }
}
}
class Mother : Person { }
class Father : Person { }
class Child : Person { }
3.新增类SimpsonFamily.Config.FamilySection,增加属性surname,增加father和mother,增加集合children(children中的type明显应为Child).
class FamilySection : System.Configuration.ConfigurationSection
{
[ConfigurationProperty("surname", IsRequired = true)]
public string Surname
{
get { return (string)base["surname"]; }
set { base["surname"] = value; }
} [ConfigurationProperty("father", IsDefaultCollection = false)]
public Father Father
{
get { return (Father)base["father"]; }
set { base["father"] = value; }
} [ConfigurationProperty("mother", IsDefaultCollection = false)]
public Mother Mother
{
get { return (Mother)base["mother"]; }
set { base["mother"] = value; }
} [ConfigurationProperty("children", IsDefaultCollection = false)]
[ConfigurationCollection(typeof(Child), CollectionType = ConfigurationElementCollectionType.AddRemoveClearMap, RemoveItemName = "remove")]
public Children Children
{
get { return (Children)base["children"]; }
set { base["children"] = value; }
}
}
测试代码:
var section = (FamilySection)System.Configuration.ConfigurationManager.GetSection("family");
string surname = section.Surname;
Father f = section.Father;
Mother m = section.Mother;
for (int i = 0; i < section.Children.Count; i++)
{
string firstname = section.Children[i].FirstName;
string lastname = section.Children[i].LastName;
}
app.config 配置多项 配置集合 自定义配置(2)的更多相关文章
- app.config 配置多项 配置集合 自定义配置
C#程序的配置文件,使用的最多的是appSettings 下的<add key="Interval" value="30"/>,这种配置单项的很方便 ...
- app.config 配置多项 配置集合 自定义配置(3)
再说说利用app.config配置多个自定义的方法.先看这个例子:美国家庭Simpson的家里有父亲母亲和三个儿女,而中国的老王只有独生子女.结构如下: <?xml version=" ...
- app.config 配置多项 配置集合 自定义配置(4) 自动增加配置项到配置文件的两种方法
一,按照xml文件处理: 配置文件如下图(最后的图片). 自动写入configSections和configSections的实例 1.自动写入configSections Configuration ...
- C# 读取app.config配置文件节点键值,提示"配置系统未能初始化" 错误的解决方案
MSDN里写到, 如果配置文件中包含 configSections 元素,则 configSections 元素必须是 configuration 元素的第一个子元素. 将自己添加的appSettin ...
- C# App.config 自定义 配置节 报错“配置系统未能初始化” 解决方法
App.config,结果运行的时候出现了 "配置系统未能初始化" 的错误.找了半天才发现是下面的原因造成的: "如果配置文件中包含configSections元素,则c ...
- web.config or app.config 中configSections配置节点
以前还真没见过,今天看项目中有在用,简单写了个Demo,这样配置的好处就是可以自定义配置,更加模块化,直接上代码; 1.配置文件 由于我创建的是一个控制台项目,所以配置文件是App.Config:(这 ...
- 【flask】flask项目配置 app.config
[理论] 在很多情况下,你需要设置程序的某些行为,这时你就需要使用配置变量.在Flask中,配置变量就是一些大写形式的Python变量, 你也可以称之为配置参数或配置键.使用统一的配置变量可以避免在程 ...
- .Net Core 自定义配置源从配置中心读取配置
配置,几乎所有的应用程序都离不开它..Net Framework时代我们使用App.config.Web.config,到了.Net Core的时代我们使用appsettings.json,这些我们再 ...
- C# App.config 详解
读语句: String str = ConfigurationManager.AppSettings["DemoKey"]; 写语句: Configuration cfa = ...
随机推荐
- 在Java环境上运行redis
首先你得有Java环境,不多说,参考http://jingyan.baidu.com/article/f96699bb8b38e0894e3c1bef.html 下载redis驱动包 链接:http: ...
- PHP的ntohl网络字节序函数及相关知识
PHP与C服务器的socket通信,在做数据转换的时候,PHP没有提供对应将网络字节序和机器字节序相互转换的程序,但是根据函数的意义,我们可以做相应的转换来实现这一函数: function ntohl ...
- HDU 1219 AC Me
strlen能不用就不用 #include<cstdio> #include<cstdlib> #include<iostream> #include<alg ...
- Guava快速入门
Guava快速入门 Java诞生于1995年,在这20年的时间里Java已经成为世界上最流行的编程语言之一.虽然Java语言时常经历各种各样的吐槽,但它仍然是一门在不断发展.变化的语言--除了语言本身 ...
- 【机器学习实战】第7章 集成方法 ensemble method
第7章 集成方法 ensemble method 集成方法: ensemble method(元算法: meta algorithm) 概述 概念:是对其他算法进行组合的一种形式. 通俗来说: 当做重 ...
- visio流程图软件
microsoft visio 2013 64位 链接:http://pan.baidu.com/s/1bnjWa1T 密码:9ylc 标准图表 使用现有的数据,您可以生成许多种类的 Visio 标准 ...
- 深入浅出:JavaScript作用域链
1. 什么是作用域 任何程序设计语言都有作用域的概念,简单的说,作用域就是变量的作用范围. 2. 变量的分类和变量作用域的分类 在JavaScript中,变量分为全局变量和局部变量,与此相对应的,变量 ...
- [机房练习赛7.26] YYR字符串
1 无尽的矩阵(matrix.c/cpp/pas) 1.1 题目描述 从前有一个的小矩阵,矩阵的每个元素是一个字母(区分大小写),突然有一天它发生了变异,覆盖了整个二维空间,即不停自我复制产生相同 ...
- 【ASP.NET MVC 学习笔记】- 05 依赖注入工具Ninject
本文参考:http://www.cnblogs.com/willick/p/3223042.html 1.Ninject是一款轻量级的DI工具,可通过VS的插件NuGet将其引用到项目中. 2.使用N ...
- css 文字和子元素水平垂直居中
关于水平垂直居中,这是一个很简单的问题,但是很多时候,往往简单的东西,反而做不出来.这就是基础不扎实的缘故吧,我参照一些资料,总结了水平垂直居中的几种方法如下: 1 .文字水平垂直居中 这个比较简单, ...