当使用ConfigurationManager.OpenMappedExeConfiguration时,如何从ConfigSection中获取定义为NameValueSectionHandler的值 [英] How do I get the values from a ConfigSection defined as NameValueSectionHandler when using ConfigurationManager.OpenMappedExeConfiguration

查看:1289
本文介绍了当使用ConfigurationManager.OpenMappedExeConfiguration时,如何从ConfigSection中获取定义为NameValueSectionHandler的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您使用应用程序的当前配置文件时,从使用由System.Configuration.NameValueSectionHandler定义的部分的配置文件获取值很容易。

Getting the values from a config file that uses a section defined by System.Configuration.NameValueSectionHandler is easy when you're using the current config file for the application.

示例配置文件。

<configuration>
  <configSections>
    <section name="MyParams" type="System.Configuration.NameValueSectionHandler" />
  </configSections>

  <MyParams>
    <add key="FirstParam" value="One"/>
    <add key="SecondParam" value="Two"/>
  </MyParams>
</configuration>

示例代码可以方便地读取。

Example Code that easily reads it.

NameValueCollection myParamsCollection =
   ConfigurationManager.GetSection("MyParams") as NameValueCollection;

这是不起作用的代码。

NameValueCollection collection =
  ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
  .GetSection("MyParams") as NameValueCollection;

编译时出现以下错误。

无法通过引用转换,装箱转换,取消装箱转换,换行或空类型转换将类型System.Configuration.ConfigurationSection转换为System.Collections.Specialized.NameValueCollection。

Cannot convert type 'System.Configuration.ConfigurationSection' to 'System.Collections.Specialized.NameValueCollection' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion.

ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)返回System.Configuration.Configuration,Configuration.GetSection返回ConfigurationSection。

ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None) returns a System.Configuration.Configuration, and Configuration.GetSection returns ConfigurationSection.

ConfigurationManager.GetSection返回对象。

ConfigurationManager.GetSection returns object.

那么,当我必须使用OpenExeConfiguration时,如何获取我的NameValueCollection?

So, how do I get back my NameValueCollection when I have to use OpenExeConfiguration?

推荐答案

我在两年前遇到了我自己的回答。

I ran across my own answer from two years ago.

NameValueSectionHandler - 我可以使用这个部分类型写回应用程序配置文件吗?

NameValueSectionHandler - can i use this section type for writing back to the application config file?

这是我的方法来解决我当前的问题。 / p>

This is my approach to solve my current issue.

ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap() { 
   ExeConfigFilename = "path to config here" 
    };

Configuration config = ConfigurationManager.OpenMappedExeConfiguration(
   configFileMap, ConfigurationUserLevel.None);

ConfigurationSection myParamsSection = config.GetSection("MyParams");

string myParamsSectionRawXml = myParamsSection .SectionInformation.GetRawXml();
XmlDocument sectionXmlDoc = new XmlDocument();
sectionXmlDoc.Load(new StringReader(myParamsSectionRawXml ));
NameValueSectionHandler handler = new NameValueSectionHandler();

NameValueCollection handlerCreatedCollection = 
   handler.Create(null, null, sectionXmlDoc.DocumentElement) as NameValueCollection;

Console.WriteLine(handlerCreatedCollection.Count);

一种适用于任何遗留IConfigurationSectionHandler类型NameValueSectionHandler,DictionarySectionHandler,SingleTagSectionHandler的方法。

A method that works with any of the legacy IConfigurationSectionHandler types NameValueSectionHandler, DictionarySectionHandler, SingleTagSectionHandler.

public static object GetConfigurationValues(string configFileName, string sectionName)
{
    ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap() { ExeConfigFilename = configFileName };
    Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
    ConfigurationSection section = config.GetSection(sectionName);
    string xml = section.SectionInformation.GetRawXml();
    XmlDocument doc = new XmlDocument();
    doc.Load(XmlReader.Create(new StringReader(xml)));
    string type = section.SectionInformation.Type;
    string assemblyName = typeof(IConfigurationSectionHandler).Assembly.GetName().FullName;
    ObjectHandle configSectionHandlerHandle = Activator.CreateInstance(assemblyName, section.SectionInformation.Type);
    if (configSectionHandlerHandle != null)
    {
        IConfigurationSectionHandler handler = configSectionHandlerHandle.Unwrap() as IConfigurationSectionHandler;
        return handler.Create(null, null, doc.DocumentElement);
    }
    return null;
}

这篇关于当使用ConfigurationManager.OpenMappedExeConfiguration时,如何从ConfigSection中获取定义为NameValueSectionHandler的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆