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