繁体   English   中英

ReadOnlyNameValueCollection(从ConfigurationManager.GetSection读取)

[英]ReadOnlyNameValueCollection (reading from ConfigurationManager.GetSection)

好的,所以......

<section name="test" type="System.Configuration.NameValueFileSectionHandler" />
<test>
   <add key="foo" value="bar" />
</test>

var test = ConfigurationManager.GetSection("test");

到现在为止还挺好。 调试器显示test包含一个键foo

GetSection返回object ,所以我们需要一个GetSection

var type = test.GetType();
// FullName: System.Configuration.ReadOnlyNameValueCollection
// Assembly: System

好的,这应该很简单。 所以....

using System;

var test = ConfigurationManager
               .GetSection("test") as ReadOnlyNameValueCollection;

错误!

The type or namespace ReadOnlyNameValueCollection does not exist in the namespace System.Configuration. Are you missing an assembly reference?

错... wtf?

System.Collections.Specialized.NameValueCollection可以使代码正常工作,但我真的不明白为什么会出错。

在MSDN上搜索ReadOnlyNameValueCollection显示该类没有任何文档。 它似乎不存在。 然而,我的代码中有一个这种类型的实例。

System.Configuration.ReadOnlyNameValueCollection是System.dll程序集的internal类。 所以你不能从你的代码中引用它。 它派生自System.Collections.Specialized.NameValueCollection ,这就是为什么你能够使用强制转换来实现这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM