上一篇说了利用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)的更多相关文章

  1. app.config 配置多项 配置集合 自定义配置

    C#程序的配置文件,使用的最多的是appSettings 下的<add key="Interval" value="30"/>,这种配置单项的很方便 ...

  2. app.config 配置多项 配置集合 自定义配置(3)

    再说说利用app.config配置多个自定义的方法.先看这个例子:美国家庭Simpson的家里有父亲母亲和三个儿女,而中国的老王只有独生子女.结构如下: <?xml version=" ...

  3. app.config 配置多项 配置集合 自定义配置(4) 自动增加配置项到配置文件的两种方法

    一,按照xml文件处理: 配置文件如下图(最后的图片). 自动写入configSections和configSections的实例 1.自动写入configSections Configuration ...

  4. C# 读取app.config配置文件节点键值,提示"配置系统未能初始化" 错误的解决方案

    MSDN里写到, 如果配置文件中包含 configSections 元素,则 configSections 元素必须是 configuration 元素的第一个子元素. 将自己添加的appSettin ...

  5. C# App.config 自定义 配置节 报错“配置系统未能初始化” 解决方法

    App.config,结果运行的时候出现了 "配置系统未能初始化" 的错误.找了半天才发现是下面的原因造成的: "如果配置文件中包含configSections元素,则c ...

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

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

  7. 【flask】flask项目配置 app.config

    [理论] 在很多情况下,你需要设置程序的某些行为,这时你就需要使用配置变量.在Flask中,配置变量就是一些大写形式的Python变量, 你也可以称之为配置参数或配置键.使用统一的配置变量可以避免在程 ...

  8. .Net Core 自定义配置源从配置中心读取配置

    配置,几乎所有的应用程序都离不开它..Net Framework时代我们使用App.config.Web.config,到了.Net Core的时代我们使用appsettings.json,这些我们再 ...

  9. C# App.config 详解

      读语句: String str = ConfigurationManager.AppSettings["DemoKey"]; 写语句: Configuration cfa = ...

随机推荐

  1. eclipse建立一个jsp项目遇到的问题及解决

    打开eclipse  在workplace 区域空白处,右键 填写好Project name,之后,点击finished 即可. 选中webcontent,新建一个文件夹,并新建一个jsp 文件 新建 ...

  2. JFrame的层次结构以及背景颜色设置问题

    JFrame的层次结构: JFrame:窗体,也就是窗口的框架.默认为不可见.不透明的(可以使用isVisible和isOpaque来验证).创建窗口时,最后一步需要调用setVisible(true ...

  3. Python实战之IO多路复用select实例

    Select方法: 句柄列表11, 句柄列表22, 句柄列表33 = select.select(句柄序列1, 句柄序列2, 句柄序列3, 超时时间)   参数: 可接受四个参数(前三个必须) 返回值 ...

  4. 扩展javascript扩展(类,对象,原型)

     扩展javascript扩展(类,对象,原型)

  5. MSSQL查询数据分页

    这几天刚好碰到数据的分页查询,觉得不错,Mark一下,方法有两种,都是使用select top,效率如何就不在这讨论 方法1:利用select top配合not in(或者not exists),查询 ...

  6. ASP.NET Core 处理 404 Not Found

    问题 在没有修改任何配置的情况下,这是用户使用 Chrome 访问不存在的URL时会看到的内容: 幸运的是,处理错误状态代码非常简单,我们将在下面介绍三种技术. 解决方案 在以前的ASP.NET MV ...

  7. iOS开发之AutoLayout中的Content Hugging Priority和 Content Compression Resistance Priority解析

    本篇博客的内容也不算太复杂,算是AutoLayout的一些高级的用法.本篇博客我们主要通过一些示例来看一下AutoLayout中的Content Hugging Priority以及Content C ...

  8. mac idea sbt工程打jar包

    1.首先保证sbt已下载,否则下载homebrew:在命令行输入/usr/bin/ruby XXX ->下载完成后在终端输入brew install sbt ->安装完毕后可以打jar包 ...

  9. Admob - Google广告接入

    前言 现在免费小游戏及应用的主要收入渠道就是通过接入广告.而Google的Admob适用于全球范围内的广告接入,文档方面及后台管理也是较为完善,接入还是比较便捷的. 不过Google目前还在墙外,虽然 ...

  10. vDSP加速的应用

    vDSP 是IOS提供一系列加速处理算法..在优化时可以考虑应用一二... 1.在项目中加入Accelerate.framework库 点开项目属性->Build Phases->Link ...