첫째, 나는 Unity Configuration를 사용하여 사용을 권장합니다.
코드 :
public class MySection : ConfigurationSection
{
protected static ConfigurationPropertyCollection properties = new ConfigurationPropertyCollection();
private static ConfigurationProperty propElements = new ConfigurationProperty("elements", typeof(MyElementCollection), null, ConfigurationPropertyOptions.IsRequired | ConfigurationPropertyOptions.IsDefaultCollection);
static BotSection()
{
properties.Add(propElements);
}
[ConfigurationProperty("elements", DefaultValue = null, IsRequired = true)]
[ConfigurationCollection(typeof(MyElementCollection), AddItemName = "add", ClearItemsName = "clear", RemoveItemName = "remove")]
public MyElementCollection Elements
{
get
{
return (MyElementCollection)this[propElements];
}
set
{
this[propElements] = value;
}
}
}
public class MyElementCollection : ConfigurationElementCollection,
IEnumerable<ConfigurationElement> // most important difference with default solution
{
public void Add(MyElement element)
{
base.BaseAdd(element);
}
public void Clear()
{
base.BaseClear();
}
protected override ConfigurationElement CreateNewElement()
{
return new MyElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((MyElement)element).Id;
}
IEnumerator<MyElement> IEnumerable<MyElement>.GetEnumerator()
{
return this.OfType<MyElement>().GetEnumerator();
}
}
public class MyElement : ConfigurationElement
{
protected static ConfigurationPropertyCollection properties = new ConfigurationPropertyCollection();
private static ConfigurationProperty propValue= new ConfigurationProperty("value", typeof(int), -1, ConfigurationPropertyOptions.IsRequired);
public int Value
{
get
{
return (int)this[propValue];
}
set
{
this[propValue] = value;
}
}
}
구성 :
<configuration>
<configSections>
<section name="MySection" type="MySection, MyAssembly"/>
</configSections>
<MySection>
<elements>
<clear />
<add value="10" />
<remove value="10" />
<add value="20" />
<add value="30" />
</elements>
</MySection>
</configuration>
Nothing ... Google에서 검색했지만 답변을 찾지 못했습니다 (또는 올바른 키워드를 사용하지 않았습니다). –
Google [ "custom con (http://www.google.com/search?q=custom+configuration+section) – abatishchev