实现代码如下:
public class MySection3 : ConfigurationSection
{
[ConfigurationProperty("Command1", IsRequired = true)]
public MyTextElement Command1
{
get { return (MyTextElement)this["Command1"]; }
}
[ConfigurationProperty("Command2", IsRequired = true)]
public MyTextElement Command2
{
get { return (MyTextElement)this["Command2"]; }
}
}
public class MyTextElement : ConfigurationElement
{
protected override void DeserializeElement(System.Xml.XmlReader reader, bool serializeCollectionKey)
{
CommandText = reader.ReadElementContentAs(typeof(string), null) as string;
}
protected override bool SerializeElement(System.Xml.XmlWriter writer, bool serializeCollectionKey)
{
if( writer != null )
writer.WriteCData(CommandText);
return true;
}
[ConfigurationProperty("data", IsRequired = false)]
public string CommandText
{
get { return this["data"].ToString(); }
set { this["data"] = value; }
}
}
{
[ConfigurationProperty("Command1", IsRequired = true)]
public MyTextElement Command1
{
get { return (MyTextElement)this["Command1"]; }
}
[ConfigurationProperty("Command2", IsRequired = true)]
public MyTextElement Command2
{
get { return (MyTextElement)this["Command2"]; }
}
}
public class MyTextElement : ConfigurationElement
{
protected override void DeserializeElement(System.Xml.XmlReader reader, bool serializeCollectionKey)
{
CommandText = reader.ReadElementContentAs(typeof(string), null) as string;
}
protected override bool SerializeElement(System.Xml.XmlWriter writer, bool serializeCollectionKey)
{
if( writer != null )
writer.WriteCData(CommandText);
return true;
}
[ConfigurationProperty("data", IsRequired = false)]
public string CommandText
{
get { return this["data"].ToString(); }
set { this["data"] = value; }
}
}
小结:
在实现上大体可参考MySection2,
每个ConfigurationElement由我们来控制如何读写XML,也就是要重载方法SerializeElement,DeserializeElement
config文件 - Collection
<MySection444>
<add key="aa" value="11111"></add>
<add key="bb" value="22222"></add>
<add key="cc" value="33333"></add>
</MySection444>
<add key="aa" value="11111"></add>
<add key="bb" value="22222"></add>
<add key="cc" value="33333"></add>
</MySection444>
这种类似的配置方式,在ASP.NET的HttpHandler, HttpModule中太常见了,想不想知道如何实现它们? 代码如下:
public class MySection4 : ConfigurationSection // 所有配置节点都要选择这个基类
{
private static readonly ConfigurationProperty s_property
= new ConfigurationProperty(string.Empty, typeof(MyKeyValueCollection), null,
ConfigurationPropertyOptions.IsDefaultCollection);
[ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
public MyKeyValueCollection KeyValues
{
get
{
return (MyKeyValueCollection)base[s_property];
}
}
}
[ConfigurationCollection(typeof(MyKeyValueSetting))]
public class MyKeyValueCollection : ConfigurationElementCollection // 自定义一个集合
{
// 基本上,所有的方法都只要简单地调用基类的实现就可以了。
public MyKeyValueCollection() : base(StringComparer.OrdinalIgnoreCase) // 忽略大小写
{
}
// 其实关键就是这个索引器。但它也是调用基类的实现,只是做下类型转就行了。
new public MyKeyValueSetting this[string name]
{
get
{
return (MyKeyValueSetting)base.BaseGet(name);
}
}
// 下面二个方法中抽象类中必须要实现的。
protected override ConfigurationElement CreateNewElement()
{
return new MyKeyValueSetting();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((MyKeyValueSetting)element).Key;
}
// 说明:如果不需要在代码中修改集合,可以不实现Add, Clear, Remove
public void Add(MyKeyValueSetting setting)
{
this.BaseAdd(setting);
}
public void Clear()
{
base.BaseClear();
}
public void Remove(string name)
{
base.BaseRemove(name);
}
}
public class MyKeyValueSetting : ConfigurationElement // 集合中的每个元素
{
[ConfigurationProperty("key", IsRequired = true)]
public string Key
{
get { return this["key"].ToString(); }
set { this["key"] = value; }
}
[ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get { return this["value"].ToString(); }
set { this["value"] = value; }
}
}
{
private static readonly ConfigurationProperty s_property
= new ConfigurationProperty(string.Empty, typeof(MyKeyValueCollection), null,
ConfigurationPropertyOptions.IsDefaultCollection);
[ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)]
public MyKeyValueCollection KeyValues
{
get
{
return (MyKeyValueCollection)base[s_property];
}
}
}
[ConfigurationCollection(typeof(MyKeyValueSetting))]
public class MyKeyValueCollection : ConfigurationElementCollection // 自定义一个集合
{
// 基本上,所有的方法都只要简单地调用基类的实现就可以了。
public MyKeyValueCollection() : base(StringComparer.OrdinalIgnoreCase) // 忽略大小写
{
}
// 其实关键就是这个索引器。但它也是调用基类的实现,只是做下类型转就行了。
new public MyKeyValueSetting this[string name]
{
get
{
return (MyKeyValueSetting)base.BaseGet(name);
}
}
// 下面二个方法中抽象类中必须要实现的。
protected override ConfigurationElement CreateNewElement()
{
return new MyKeyValueSetting();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((MyKeyValueSetting)element).Key;
}
// 说明:如果不需要在代码中修改集合,可以不实现Add, Clear, Remove
public void Add(MyKeyValueSetting setting)
{
this.BaseAdd(setting);
}
public void Clear()
{
base.BaseClear();
}
public void Remove(string name)
{
base.BaseRemove(name);
}
}
public class MyKeyValueSetting : ConfigurationElement // 集合中的每个元素
{
[ConfigurationProperty("key", IsRequired = true)]
public string Key
{
get { return this["key"].ToString(); }
set { this["key"] = value; }
}
[ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get { return this["value"].ToString(); }
set { this["value"] = value; }
}
}
小结:
①为每个集合中的参数项创建一个从ConfigurationElement继承的派生类,可参考MySection1
②为集合创建一个从ConfigurationElementCollection继承的集合类,具体在实现时主要就是调用基类的方法。
③在创建ConfigurationSection的继承类时,创建一个表示集合的属性就可以了,注意[ConfigurationProperty]的各参数。