7

好吧,那么……

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

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

到目前为止,一切都很好。调试器显示test包含一个键,foo.

但是GetSection返回object,所以我们需要一个演员表:

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显示,根本没有关于此类的文档。它似乎不存在。然而,我的代码中有一个这种类型的实例。

4

1 回答 1

15

System.Configuration.ReadOnlyNameValueCollectioninternal是System.dll 程序集的一个类。所以你不能从你的代码中引用它。但是,它源自System.Collections.Specialized.NameValueCollection,所以这就是为什么您可以使用演员阵容做到这一点。

于 2011-05-16T15:02:03.470 回答