上一篇说了利用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;
}