簡體   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