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 = ...
随机推荐
- 解决VMware下安装Ubuntu 16.04 不支持1920X1080分辨率的问题
解决方法: flashmx@ubuntu:~$ cvt # 192.07M9) hsync: 67.16 kHz; pclk: 173.00 MHz Modeline -hsync +vsync fl ...
- js 将一大段时间均分为很多个小时间段
最近写项目,遇到一个将选中时间段平均分割为若干小段,然后根据小段时间在数据库查询求均值的问题,后台大哥犯懒,非说后台做不了,让我分好传给他ヾ(. ̄□ ̄)ツ゜゜゜好气呦,但还要保持微笑,我就是这么懂礼貌 ...
- Bootstrap 禁用滚动条
Bootstrap中禁用滚动条的方法 逻辑: 当点击弹窗按钮后,js会为body元素添加一个modal-open的类,该类主要内容如下 .modal-open .modal { overflow-x: ...
- 深入Javascript之this
前言 近期准备好好的读一读<你不知道的JavaScript(上卷)>这本书,俗话说的好,好记性不如烂笔头,读到this这章感觉是时候需要一些笔记了.文中如有错误之处,欢迎指出. 什么是th ...
- 如何通过写bat 安装Windows服务,本人亲测成功
1. 安装的bat文件 @echo on color 2f mode con: cols=80 lines=25 @echo 请按任意键开始安装后台服务... pause cd /d %~dp0 Le ...
- C++数组做参数
首先,看一下下面这段代码: void changearr(int a[],int n){ cout<<sizeof(a)<<endl; // 输出4}in ...
- Akka(34): Http:Unmarshalling,from Json
Unmarshalling是Akka-http内把网上可传输格式的数据转变成程序高级结构话数据的过程,比如把Json数据转换成某个自定义类型的实例.按具体流程来说就是先把Json转换成可传输格式数据如 ...
- Supervised Learning and Unsupervised Learning
Supervised Learning In supervised learning, we are given a data set and already know what our correc ...
- 最新数据库排行,Oracle略显疲惫
9月份TOPDB Top Database Index排行榜出炉,TOPDB Top Database Index是根据数据库在谷歌上的搜索频率分析得出的,数据库被搜索的频率越大,表示数 9月份的TO ...
- Python Web框架篇:Django cookie和session
part 1 概念 在Django里面,cookie和session都记录了客户端的某种状态,用来跟踪用户访问网站的整个回话. 两者最大的区别是cookie的信息是存放在浏览器客户端的,而sessio ...