小编Lir*_*n B的帖子

如何获取NameValueSectionHandler类型的ConfigurationSection的值

我正在使用C#,Framework 3.5(VS 2008).

我正在使用将ConfigurationManager配置(而不是默认的app.config文件)加载到Configuration对象中.

使用Configuration类,我能够得到一个ConfigurationSection,但我找不到获取该部分值的方法.

在配置中,ConfigurationSection是类型System.Configuration.NameValueSectionHandler.

对于什么是价值,当我使用的方法GetSectionConfigurationManager(只能当它是对我的默认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)

c# configurationmanager config configurationsection

64
推荐指数
4
解决办法
10万
查看次数