简体   繁体   English

ReadOnlyNameValueCollection(从ConfigurationManager.GetSection读取)

[英]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.

相关问题 ConfigurationManager.GetSection()函数 - ConfigurationManager.GetSection() function ConfigurationManager.GetSection跳过重复项 - ConfigurationManager.GetSection Skips Duplicates ConfigurationManager.GetSection 返回 null - ConfigurationManager.GetSection returns null ConfigurationManager.GetSection给出错误“无法从程序集加载类型...” - ConfigurationManager.GetSection Gives Error “Could not load type…from assembly…” ConfigurationManager.GetSection 不继承自 System.Configuration.IConfigurationSectionHandler - ConfigurationManager.GetSection does not inherit from System.Configuration.IConfigurationSectionHandler ConfigurationSection ConfigurationManager.GetSection()始终返回null - ConfigurationSection ConfigurationManager.GetSection() always returns null ConfigurationManager.GetSection和Configuration.GetSection有什么区别? - What is Difference between ConfigurationManager.GetSection and Configuration.GetSection? ConfigurationManager.GetSection始终为对象提供默认值 - ConfigurationManager.GetSection always gives object with default values ConfigurationManager.GetSection不返回自定义部分 - ConfigurationManager.GetSection doesn't return custom section 如何在.Net Core 2.2中正确使用ConfigurationManager.GetSection() - How to use properly ConfigurationManager.GetSection() in .Net Core 2.2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM