C#、Framework 3.5 (VS 2008) を使用しています。
を使用してConfigurationManager
、構成 (デフォルトの app.config ファイルではない) を構成オブジェクトに読み込みます。
Configuration クラスを使用して を取得できましたがConfigurationSection
、そのセクションの値を取得する方法が見つかりませんでした。
構成でConfigurationSection
は、 のタイプはSystem.Configuration.NameValueSectionHandler
です。
価値があるのは、のメソッドを使用したGetSection
ときConfigurationManager
(デフォルトのapp.configファイルにある場合にのみ機能する)、キーと値のペアのコレクションにキャストできるオブジェクトタイプを受け取りました。 Dictionary のような値。ConfigurationSection
ただし、構成クラスからクラスを受け取ったときは、そのようなキャストを行うことができませんでした。
編集: 構成ファイルの例:
<configuration>
<configSections>
<section name="MyParams"
type="System.Configuration.NameValueSectionHandler" />
</configSections>
<MyParams>
<add key="FirstParam" value="One"/>
<add key="SecondParam" value="Two"/>
</MyParams>
</configuration>
app.config にあるときに使用できた方法の例 (「GetSection」メソッドは、デフォルトの app.config 専用です):
NameValueCollection myParamsCollection =
(NameValueCollection)ConfigurationManager.GetSection("MyParams");
Console.WriteLine(myParamsCollection["FirstParam"]);
Console.WriteLine(myParamsCollection["SecondParam"]);