[英]ReadOnlyNameValueCollection (reading from ConfigurationManager.GetSection)
Ok, so..... 好的,所以......
<section name="test" type="System.Configuration.NameValueFileSectionHandler" />
<test>
<add key="foo" value="bar" />
</test>
var test = ConfigurationManager.GetSection("test");
So far so good. 到现在为止还挺好。 The debugger shows
test
contains one key, foo
. 调试器显示
test
包含一个键foo
。
But GetSection
returns object
, so we need a cast: 但
GetSection
返回object
,所以我们需要一个GetSection
:
var type = test.GetType();
// FullName: System.Configuration.ReadOnlyNameValueCollection
// Assembly: System
Ok, this should be simple enough. 好的,这应该很简单。 So....
所以....
using System;
var test = ConfigurationManager
.GetSection("test") as ReadOnlyNameValueCollection;
error! 错误!
The type or namespace ReadOnlyNameValueCollection does not exist in the namespace System.Configuration. Are you missing an assembly reference?
err... wtf? 错... wtf?
A cast to System.Collections.Specialized.NameValueCollection
gets the code working, but I don't really understand why the error. 对
System.Collections.Specialized.NameValueCollection
可以使代码正常工作,但我真的不明白为什么会出错。
And a search for ReadOnlyNameValueCollection
on MSDN shows there is no documentation on this class at all. 在MSDN上搜索
ReadOnlyNameValueCollection
显示该类没有任何文档。 It doesn't seem to exist. 它似乎不存在。 Yet I have an instance of that type in my code.
然而,我的代码中有一个这种类型的实例。
System.Configuration.ReadOnlyNameValueCollection
is an internal
class to the System.dll assembly. System.Configuration.ReadOnlyNameValueCollection
是System.dll程序集的internal
类。 So you can't refer to it from your code. 所以你不能从你的代码中引用它。 It derives from
System.Collections.Specialized.NameValueCollection
, though, so that's why you're able to do that with the cast. 它派生自
System.Collections.Specialized.NameValueCollection
,这就是为什么你能够使用强制转换来实现这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.