如何使用.net
中的ConfigurationElementCollection自定义ConfigurationSection我的WebConfig:
<cachingConfig enable="true">
<cachingEngine name="Engine1" enable="true">
<instance name="Instance1" value="127.0.0.1:11211|127.0.0.2:11211|127.0.0.3:11211" />
<instance name="Instance2" value="127.0.0.1:11211" />
<instance name="Instance3" value="127.0.0.1:11211" />
</cachingEngine>
<cachingEngine name="Engine2" enable="false">
<instance name="Instance1" value="127.0.0.1:11211" />
<instance name="Instance2" value="127.0.0.1:11211" />
</cachingEngine>
</cachingConfig>
我写了,但它没有执行:
public class Configurator:ConfigurationSection { [ConfigurationProperty(“enable”,DefaultValue = true,IsRequired = true)] 内部布尔启用 { get {return(bool)this [“enable”]; } set {this [“enable”] = value; } }
public ItemCollection1 CachingEngine
{
get { return ((ItemCollection1)(base["cachingEngine"])); }
set { base["cachingEngine"] = value; }
}
internal static Configurator GetConfiguration()
{
var configuration = (Configurator)ConfigurationManager.GetSection("cachingConfig");
return configuration ?? new Configurator();
}
}
[ConfigurationCollection(typeof(CachingEngine),CollectionType = ConfigurationElementCollectionType.BasicMapAlternate)] public class ItemCollection1:ConfigurationElementCollection { 内部const字符串ItemPropertyName =“cachingEngine”;
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMapAlternate; }
}
protected override string ElementName
{
get { return ItemPropertyName; }
}
protected override bool IsElementName(string elementName)
{
return (elementName == ItemPropertyName);
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((CachingEngine)element).Name;
}
protected override ConfigurationElement CreateNewElement()
{
return new CachingEngine();
}
public override bool IsReadOnly()
{
return false;
}
}
[ConfigurationCollection(typeof(Instance),CollectionType = ConfigurationElementCollectionType.BasicMapAlternate,AddItemName =“instance”)] public class ItemCollection2:ConfigurationElementCollection { 内部const字符串ItemPropertyName =“instance”;
public override ConfigurationElementCollectionType CollectionType
{
get { return ConfigurationElementCollectionType.BasicMapAlternate; }
}
protected override string ElementName
{
get { return ItemPropertyName; }
}
protected override bool IsElementName(string elementName)
{
return (elementName == ItemPropertyName);
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((CachingEngine)element).Name;
}
protected override ConfigurationElement CreateNewElement()
{
return new CachingEngine();
}
public override bool IsReadOnly()
{
return false;
}
}
public class CachingEngine:ConfigurationElement { [ConfigurationProperty(“name”,DefaultValue =“”,IsRequired = true)] 内部字符串名称 { get {return(string)this [“name”]; } set {this [“name”] = value; } }
[ConfigurationProperty("enable", DefaultValue = true, IsRequired = true)]
internal bool Enable
{
get { return (bool)this["enable"]; }
set { base["enable"] = value; }
}
public ItemCollection2 Instance
{
get { return ((ItemCollection2)(base["instance"])); }
set { base["instance"] = value; }
}
}
public class Instance:ConfigurationElement { [ConfigurationProperty(“name”,DefaultValue =“”,IsRequired = true)] 内部字符串名称 { get {return(string)this [“name”]; } set {this [“name”] = value; } }
[ConfigurationProperty("value", DefaultValue = "", IsRequired = true)]
internal string Value
{
get { return (string)this["value"]; }
set { base["value"] = value; }
}
}