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 = ...
随机推荐
- Django进阶篇【2】
在学习之前,我们补充一个知识点(static用法) 创建APP 配置: setting.py STATICFILES_DIRS = ( os.path.join(BASE_DIR,'static') ...
- jvm的垃圾回收算法
一.对象存活判断判断对象是否存活一般有两种方式:1.引用计数:每个对象有一个引用计数属性,新增一个引用时计数加1,引用释放时计数减1,计数为0时可以回收.此方法简单,无法解决对象相互循环引用的问题.2 ...
- 将网页封装成苹果APP的牛逼方法,无需发布到appstore,无需越狱即可安装
很多小伙伴都在开发自己的app, 有的实现实现比较简单,就是一个h5页面,然后想要打包成app发布出去. 这个想法很单纯 打包生成个app这个是很简单的,网上一堆打包工具,分分钟可以完成 但是... ...
- ajax请求中设置特殊的RequestHeader
现在ajax应用已经相当广泛了,有很多不错的ajax框架可供使用.ajax是一个异步请求,也主要是一种客户端的脚本行为.那么,如何在请求之前为请求添加特殊的一些头部信息呢? 下面是一个简单的例子,我用 ...
- SQL SERVER 根据地图经纬度计算距离函数
前些天客户提出一个这样的要求:一个手机订餐网,查询当前所在位置的5公里范围的酒店,然后客户好去吃饭. 拿到这个请求后,不知道如何下手,静静地想了一下,在酒店的表中增加两个字段,用来存储酒店所在的经度和 ...
- cocos2dx - 创建地图及玩家(伪)
接上一节内容:cocos2dx - 环境配置,项目创建 本节主要描述cocos中精灵的创建及点击事件的使用 打开创建好的test项目,看到下图的目录结构,真正的游戏逻辑路径在src下. AppDele ...
- python3.6安装Scrapy
环境:win10(64位), Python3.6(64位) 1.安装pyhthon 这个就不多说了,对应版本就下载对应的依赖包 2.安装pywin32 在windows下,必须安装pywin32,安装 ...
- 谷歌Chrome浏览器提示adobe flash player已过期完美解决办法
最近使用谷歌Chrome浏览器提示adobe flash player已过期,浏览网页时一些flash元素的东西都无法正常显示,在网上尝试寻找很多方法,都不能解决,最后,经测试有效方法如下:一:下载最 ...
- python识别html主要文本框
在抓取网页的时候只想抓取主要的文本框,例如 csdn 中的主要文本框为下图红色框: 抓取的思想是,利用bs4查找所有的div,用正则筛选出每个div里面的中文,找到中文字数最多的div就是属于正文的d ...
- Spark 作业调度相关术语
作业(Job):RDD 中由行动操作所生成的一个或多个调度阶段 调度阶段(Stage):每个作业会因为 RDD 间的依赖关系拆分成多组任务集合,称为调度阶段,也叫做任务集(TaskSet).高度阶段的 ...