Как настроить ConfigurationSection с ConfigurationElementCollection в .net
Мой 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>
Я написал, но он не выполняется:
открытый класс Конфигуратор: ConfigurationSection
{
[ConfigurationProperty («enable», DefaultValue = true, IsRequired = true)]
Внутренний bool Enable
{
get {return (bool) this ["enable"]; }
set {this ["enable"] = значение; }
}
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)]
открытый класс ItemCollection1: ConfigurationElementCollection
{
внутренняя константная строка 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")]
открытый класс ItemCollection2: ConfigurationElementCollection
{
внутренняя константная строка 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;
}
} * * тысяча двадцать-один
открытый класс CachingEngine: ConfigurationElement
{
[ConfigurationProperty ("name", DefaultValue = "", IsRequired = true)]
Имя внутренней строки
{
get {return (string) this ["name"]; }
set {this ["name"] = значение; }
}
[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; }
}
}
публичный класс Экземпляр: ConfigurationElement
{
[ConfigurationProperty ("name", DefaultValue = "", IsRequired = true)]
Имя внутренней строки
{
get {return (string) this ["name"]; }
set {this ["name"] = значение; }
}
[ConfigurationProperty("value", DefaultValue = "", IsRequired = true)]
internal string Value
{
get { return (string)this["value"]; }
set { base["value"] = value; }
}
}