【发布时间】:2011-03-28 13:07:20
【问题描述】:
我正在使用 C#,Framework 3.5 (VS 2008)。
我正在使用ConfigurationManager 将配置(不是默认的 app.config 文件)加载到配置对象中。
使用 Configuration 类,我能够获得 ConfigurationSection,但我找不到获取该部分值的方法。
在配置中,ConfigurationSection 的类型为 System.Configuration.NameValueSectionHandler。
对于它的价值,当我使用 ConfigurationManager 的方法 GetSection 时(仅在我的默认 app.config 文件中有效),我收到了一个对象类型,我可以将其转换为对集合键值,我刚刚收到像字典一样的值。但是,当我从 Configuration 类收到 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"]);
【问题讨论】:
-
如果您使用 .Net 4.0 版,那么动态可能会有所帮助
标签: c# config configurationmanager configurationsection