コレクション要素を(子コレクション要素ではなく)親要素内に直接配置できるようにするには、を再定義する必要がありますConfigurationProperty
。たとえば、次のようなコレクション要素があるとします。
public class TestConfigurationElement : ConfigurationElement
{
[ConfigurationProperty("name", IsKey = true, IsRequired = true)]
public string Name
{
get { return (string)this["name"]; }
}
}
そして、次のようなコレクション:
[ConfigurationCollection(typeof(TestConfigurationElement), AddItemName = "test")]
public class TestConfigurationElementCollection : ConfigurationElementCollection
{
protected override ConfigurationElement CreateNewElement()
{
return new TestConfigurationElement();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((TestConfigurationElement)element).Name;
}
}
親セクション/要素を次のように定義する必要があります。
public class TestConfigurationSection : ConfigurationSection
{
[ConfigurationProperty("", IsDefaultCollection = true)]
public TestConfigurationElementCollection Tests
{
get { return (TestConfigurationElementCollection)this[""]; }
}
}
[ConfigurationProperty("", IsDefaultCollection = true)]
属性に注意してください。空の名前を付け、それをデフォルトのコレクションとして設定すると、次のように構成を定義できます。
<testConfig>
<test name="One" />
<test name="Two" />
</testConfig>
それ以外の:
<testConfig>
<tests>
<test name="One" />
<test name="Two" />
</tests>
</testConfig>