我正在使用 C#、Framework 3.5 (VS 2008)。
我正在使用将ConfigurationManager
配置(不是默认的 app.config 文件)加载到配置对象中。
使用 Configuration 类,我能够得到一个ConfigurationSection
,但我找不到获取该部分值的方法。
在配置ConfigurationSection
中,类型为System.Configuration.NameValueSectionHandler
.
对于它的价值,当我使用的方法GetSection
(ConfigurationManager
仅在我的默认 app.config 文件上有效)时,我收到了一个对象类型,我可以将其转换为键值对的集合,我刚刚收到像字典一样的值。ConfigurationSection
但是,当我从 Configuration 类接收课程时,我无法进行此类转换。
编辑:配置文件示例:
<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"]);