Mam własne sekcje konfiguracji niestandardowej, ale chciałbym utworzyć nowy element, który zawiera w sobie proste klucze/wartości. Teraz mam działającą wersję, ale wydaje się całkiem sporo kodu na tak prostsze zadanie. Czy istnieje ulepszony sposób robienia rzeczy?Jak zdefiniować podstawową sekcję konfiguracji niestandardowej?
Poniżej znajduje się obnażona wersja mojej konfiguracji i niestandardowej klasy konfiguracji.
Klasa web.config
<myRootNode>
<myNode>
<add key="a" value="" />
<add key="b" value="" />
<add key="c" value="" />
...
</myNode>
...any other nodes
</myRootNode>
Konfiguracja użytkownika
public class MyRootNode : ConfigurationSection
{
[ConfigurationProperty("myNode")]
public MyNodeElement MyNode
{
get { return (MyNodeElement)this["myNode"]; }
}
}
[ConfigurationCollection(typeof(BaseElement), AddItemName = "add")]
public class MyNodeElement : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new BaseElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((BaseElement)element).Key;
}
public BaseElement this[int index]
{
get { return this.BaseGet(index) as BaseElement; }
}
}
public class BaseElement : ConfigurationElement
{
[ConfigurationProperty("key", IsRequired = true, IsKey = true)]
public string Key
{
get { return this["key"].ToString(); }
}
[ConfigurationProperty("value", IsRequired = true)]
public string Value
{
get { return this["value"].ToString(); }
}
}
Spróbuj tego artykułu: [Pisanie sekcji ustawień konfiguracji niestandardowej w języku C#] (http://www.codearsenal.net/2012/10/writing-custom-configuration-section-in-csharp.html) –