sách gpt4 ai đã đi

.net - 使用ConfigurationManager.OpenMappedExeConfiguration时如何从定义为NameValueSectionHandler的ConfigSection中获取值

In lại 作者:行者123 更新时间:2023-12-04 17:23:58 28 4
mua khóa gpt4 Nike

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

示例配置文件。












易于阅读的示例代码。
NameValueCollection myParamsCollection =
ConfigurationManager.GetSection("MyParams") as NameValueCollection;

这是无效的代码。
NameValueCollection collection =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
.GetSection("MyParams") as NameValueCollection;

失败,并在编译时出现以下错误。

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

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

ConfigurationManager.GetSection返回对象。

因此,当我必须使用OpenExeConfiguration时如何取回NameValueCollection?

1 Câu trả lời

我遇到了两年前的回答。

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

这是我解决当前问题的方法。

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一起使用的方法。
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);
}
trả về giá trị null;
}

关于.net - 使用ConfigurationManager.OpenMappedExeConfiguration时如何从定义为NameValueSectionHandler的ConfigSection中获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13825323/

28 4 0
Bài viết được đề xuất: embedded - 什么是 4’d# 符号?
Bài viết được đề xuất: sitecore - 获取与 Sitecore 中的 URL 匹配的项目
Bài viết được đề xuất: dependency-injection - 忍者: Constructor parameter
Bài viết được đề xuất: xaml - XAML 中的中心弹出窗口
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com