除了使用继承iconfigurationsectionhandler的方法定义处理自定义节点的类,还可以通过继承configurationsection类实现同样效果。
首先说下.net配置文件中一个潜规则
在配置节点时,对于想要进行存储的参数数据,可以采用两种方式:一种是存储到节点的属性中,另一种是存储在节点的文本中。
因为一个节点可以有很多属性,但是只要一个innertext,而要在程序中将这两种形式区分开会带来复杂性。 为了避免这个问题,.net的配置文件只是用属性存储而不使用innertext.
接着,我们来写一个符合这个潜规则的自定义配置文件,方便测试:
<mailservergroup provider="www.baidu.com">
<mailservers>
<mailserver client="http://blog.csdn.net/lhc1105" address="13232@qq.com" username="lhc" password="2343254"/>
<mailserver client="http://blog345.csdn.net/lhc1105" address="132wdfgdsggtaewg32@qq.com" username="dfshs水田如雅" password="2334t243的萨芬234254"/>
<mailserver client="http://blog436.csdn.net/lhc1105" address="132wdgadsfgdtaewg32@qq.com" username="sdfhdfs水田如雅" password="23ewrty2343的萨芬234254"/>
<mailserver client="http://blo345734g.csdn.net/lhc1105" address="132wdgdfagdstaewg32@qq.com" username="sdfher水田如雅" password="23erwt43的萨芬234254"/>
</mailservers>
</mailservergroup>
接着,我们来写相应的处理类,这里我们由内向外来写:
首先是最内层的mailserver:
/// <summary>
/// class mailserverelement:用于映射mailserver节点,这里是实际存储数据的地方;
/// </summary>
/// <remarks>editor:v-liuhch createtime:2015/6/27 21:51:57</remarks>
public sealed class mailserverelement : configurationelement //配置文件中的配置元素
{
/// <summary>
/// gets or sets the client.
/// </summary>
/// <value>the client.</value>
/// <remarks>editor:v-liuhch createtime:2015/6/27 22:05:40</remarks>
[configurationproperty("client", iskey = true, isrequired = true)] //client是必须的key属性,有点儿主键的意思,例如,如果定义多个client相同的节点,循环读取的话就只读取到最后一个值
public string client
{
get
{
return this["client"] as string;
}
set
{
this["client"] = value;
}
}
/// <summary>
/// gets or sets the address.
/// </summary>
/// <value>the address.</value>
/// <remarks>editor:v-liuhch createtime:2015/6/27 22:05:38</remarks>
[configurationproperty("address")]
public string address
{
get
{
return this["address"] as string;
}
set
{
this["address"] = value;
}
}
/// <summary>
/// gets or sets the name of the user.
/// </summary>
/// <value>the name of the user.</value>
/// <remarks>editor:v-liuhch createtime:2015/6/27 22:05:35</remarks>
[configurationproperty("username")]
public string username
{
get
{
return this["username"] as string;
}
set
{
this["username"] = value;
}
}
/// <summary>
/// gets or sets the password.
/// </summary>
/// <value>the password.</value>
/// <remarks>editor:v-liuhch createtime:2015/6/27 22:05:33</remarks>
[configurationproperty("password")]
public string password
{
get
{
return this["password"] as string;
}
set
{
this["password"] = value;
}
}
}
接着是mailservers,它是一个mailserver的集合:
/// <summary>
/// class mailservercollection:映射mailservers节点,为一个集合类,另外还包含了很多对节点的操作方法,大部分继承自configurationelementcollection
/// </summary>
/// <remarks>editor:v-liuhch createtime:2015/6/27 21:52:00</remarks>
public sealed class mailservercollection : configurationelementcollection
{
/// <summary>
/// 获取 <see cref="t:system.configuration.configurationelementcollection" /> 的类型。
/// </summary>
/// <value>the type of the collection.</value>
/// <returns>此集合的 <see cref="t:system.configuration.configurationelementcollectiontype" />。</returns>
/// <remarks>editor:v-liuhch createtime:2015/6/27 22:05:08</remarks>
public override configurationelementcollectiontype collectiontype
{
get
{
return configurationelementcollectiontype.basicmap;
}
}
/// <summary>
/// 当在派生的类中重写时,创建一个新的 <see cref="t:system.configuration.configurationelement" />。
/// </summary>
/// <returns>新的 <see cref="t:system.configuration.configurationelement" />。</returns>
/// <remarks>editor:v-liuhch createtime:2015/6/27 22:05:03</remarks>
protected override configurationelement createnewelement()
{
return new mailserverelement();
}
/// <summary>
/// 在派生类中重写时获取指定配置元素的元素键。
/// </summary>
/// <param name="element">要为其返回键的 <see cref="t:system.configuration.configurationelement" />。</param>
/// <returns>一个 <see cref="t:system.object" />,用作指定 <see cref="t:system.configuration.configurationelement" /> 的键。</returns>
/// <remarks>editor:v-liuhch createtime:2015/6/27 22:04:51</remarks>
protected override object getelementkey(configurationelement element)
{
return (element as mailserverelement).client;
}
/// <summary>
/// 获取在派生的类中重写时用于标识配置文件中此元素集合的名称。
/// </summary>
/// <value>the name of the element.</value>
/// <returns>集合的名称;否则为空字符串。默认值为空字符串。</returns>
/// <remarks>editor:v-liuhch createtime:2015/6/27 23:41:40</remarks>
protected override string elementname
{
get
{
return "mailserver";
}
}
/// <summary>
/// 获取集合中的元素数。
/// </summary>
/// <value>the count.</value>
/// <returns>集合中的元素数。</returns>
/// <remarks>editor:v-liuhch createtime:2015/6/27 22:08:24</remarks>
public new int count
{
get { return base.count; }
}
/// <summary>
/// 获取或设置此配置元素的属性、特性或子元素。
/// </summary>
/// <param name="index">the index.</param>
/// <returns>mailserverelement.</returns>
/// <remarks>editor:v-liuhch</remarks>
public mailserverelement this[int index]
{
get { return baseget(index) as mailserverelement; }
set
{
if (baseget(index) != null)
{
baseremoveat(index);
}
baseadd(index, value);
}
}
/// <summary>
/// 获取或设置此配置元素的属性、特性或子元素。
/// </summary>
/// <param name="name">the name.</param>
/// <returns>mailserverelement.</returns>
/// <remarks>editor:v-liuhch</remarks>
new public mailserverelement this[string name]
{
get { return baseget(name) as mailserverelement; }
}
/// <summary>
/// indexes the of.
/// </summary>
/// <param name="element">the element.</param>
/// <returns>system.int32.</returns>
/// <remarks>editor:v-liuhch createtime:2015/6/27 22:24:16</remarks>
public int indexof(mailserverelement element)
{
return baseindexof(element);
}
/// <summary>
/// adds the specified element.
/// </summary>
/// <param name="element">the element.</param>
/// <remarks>editor:v-liuhch createtime:2015/6/27 22:26:06</remarks>
public void add(mailserverelement element)
{
baseadd(element);
}
/// <summary>
/// removes the specified element.
/// </summary>
/// <param name="element">the element.</param>
/// <remarks>editor:v-liuhch createtime:2015/6/27 22:27:01</remarks>
public void remove(mailserverelement element)
{
if (baseindexof(element) > 0)
{
baseremove(element.client);
}
}
/// <summary>
/// removes at.
/// </summary>
/// <param name="index">the index.</param>
/// <remarks>editor:v-liuhch createtime:2015/6/27 22:33:29</remarks>
public void removeat(int index)
{
baseremoveat(index);
}
/// <summary>
/// removes the specified client.
/// </summary>
/// <param name="client">the client.</param>
/// <remarks>editor:v-liuhch createtime:2015/6/27 22:34:04</remarks>
public void remove(string client)
{
baseremove(client);
}
/// <summary>
/// clears this instance.
/// </summary>
/// <remarks>editor:v-liuhch createtime:2015/6/27 22:34:29</remarks>
public void clear()
{
baseclear();
}
}
最后是最外层的group:
/// <summary>
/// class mailserversection 为入口:
/// </summary>
/// <remarks>editor:v-liuhch createtime:2015/6/27 21:41:02</remarks>
public class mailserversection : configurationsection //继承配置文件中节
{
/// <summary>
/// gets the provider.:映射mailservergroup节点的provider
/// </summary>
/// <value>the provider.</value>
/// <remarks>editor:v-liuhch createtime:2015/6/27 22:05:59</remarks>
[configurationproperty("provider", iskey = true)]
public string provider { get { return this["provider"] as string; } }
/// <summary>
/// gets or sets the mail servers.:映射新添加的节点mailservers节点;这个节点下还包含了若干个mailserver节点,因此它是一个集合类
/// </summary>
/// <value>the mail servers.</value>
/// <remarks>editor:v-liuhch createtime:2015/6/27 22:05:56</remarks>
[configurationproperty("mailservers", isdefaultcollection = false)]
public mailservercollection mailservers
{
get
{
return this["mailservers"] as mailservercollection;
}
set
{
this["mailservers"] = value;
}
}
}
同样,关联处理类和节点:
<section name="mailservergroup" type="继承configurationsection基类.mailserversection,继承configurationsection基类"/>
</configsections>
之后做个测试:
class program
{
static void main(string[] args)
{
test();
}
/// <summary>
/// tests this instance.:读取节点值示例
/// </summary>
/// <remarks>editor:v-liuhch createtime:2015/6/27 23:04:53</remarks>
private static void test() {
mailserversection mailsection = (mailserversection)configurationmanager.getsection("mailservergroup");
console.writeline("mailserversection 的provider属性值:"+mailsection.provider);
foreach (mailserverelement config in mailsection.mailservers)
{
console.writeline("----------------------------------");
console.writeline("client值为:"+config.client);
console.writeline("address值为:"+config.address);
console.writeline("username值为:"+config.username);
console.writeline("password值为:"+config.password);
console.writeline("----------------------------------");
}
console.readkey();
}
}
本来还想传张结果图,但是网速慢,算啦,喜欢玩儿的童鞋自己run下结果。。。。。
以上就是.net 配置文件——继承configurationsection实现自定义处理类处理自定义配置节点 的内容。
