アプリケーションの構成ファイルからアイテムのコレクションを取得しようとしています。すべてがOKに見えますが、私は常に0の要素(関係なく、私は設定ファイルを置くことを...)App.configファイルからコレクションを読み取る
をフェッチ私のコードは次のとおりです。
using System.Configuration;
namespace CustomSettingConfiguration
{
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("Tests", IsDefaultCollection = true)]
public TestConfigurationElementCollection Tests
{
get { return (TestConfigurationElementCollection)this["Tests"]; }
}
}
}
およびコンフィギュレーションファイル:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="TestConfigurationSection" type="CustomSettingConfiguration.TestConfigurationSection" />
</configSections>
<TestConfigurationSection>
<Tests>
<test name="One" />
<test name="Two" />
</Tests>
</TestConfigurationSection>
</configuration>
それを使用する:
TestConfigurationSection a = new TestConfigurationSection();
var tests = a.Tests;
任意のアイデア??事前
応答ありがとうございますが、それは動作しません。 –
はい、私のせいで、正解を見つけましたが、すぐに投稿します –
答えが更新されました。 –