我正在使用C#,Framework 3.5(VS 2008).
我正在使用将ConfigurationManager配置(而不是默认的app.config文件)加载到Configuration对象中.
使用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>
Run Code Online (Sandbox Code Playgroud)
我在app.config上使用它时的方式示例("GetSection"方法仅适用于默认的app.config):
NameValueCollection myParamsCollection =
(NameValueCollection)ConfigurationManager.GetSection("MyParams");
Console.WriteLine(myParamsCollection["FirstParam"]);
Console.WriteLine(myParamsCollection["SecondParam"]);
Run Code Online (Sandbox Code Playgroud)