これはうまくいきました-
コード:
class Program
{
static void Main(string[] args)
{
NameValueCollection nvc = ConfigurationManager.GetSection("MyAppSettings") as NameValueCollection;
for(int i=0; i<nvc.Count; i++)
{
Console.WriteLine(nvc.AllKeys[i] + " " + nvc[i]);
}
Console.ReadLine();
}
}
class ParentSection : ConfigurationSection
{
//This may have some custom implementation
}
class MyAppSettingsSection : ParentSection
{
public static MyAppSettingsSection GetConfig()
{
return (MyAppSettingsSection)ConfigurationManager.GetSection("MyAppSettings");
}
[ConfigurationProperty("", IsDefaultCollection = true)]
public NameValueConfigurationCollection Settings
{
get
{
return (NameValueConfigurationCollection)base[""];
}
}
}
構成:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!-- <section name="MyAppSettings" type="CustomAppSettings.MyAppSettingsSection, CustomAppSettings"/> -->
<section name="MyAppSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</configSections>
<MyAppSettings>
<add key="1" value="one"/>
<add key="2" value="two"/>
<add key="3" value="three"/>
<add key="4" value="four"/>
</MyAppSettings>
</configuration>
私の主な懸念は、私のセクションがカスタムセクションから継承する必要があり、ConfigurationManager.GetSection( "MyAppSettings")が呼び出されたときにNameValueCollectionを返したいということでした。
typeプロパティをAppSettingsSectionに変更しましたが、画像のどこにも表示されておらず、機能していました。今、私はそれがどのように機能したかを理解する必要がありますが、今のところ良いことは私が動作するサンプルを持っていることです:)
更新:残念ながら、これは意図したことを達成するための期待された方法ではありませんでした。カスタムセクションがまったく表示されていないため、残念ながらこれは最善の方法ではありません。
もちろん、appsettingsセクションの名前を変更したいだけなら、これは魅力のように機能します。