2016-08-30 65 views
0

我有一個返回KeyValuePair的WCF Web服務。即:來自WCF的KeyValuePair - 通過ASP.Net消費時的轉換錯誤

List<KeyVauePair<string,int>> 

當我測試這在WCF測試客戶端,它說的返回類型爲:

System.Collections.Generic.KeyValuePair<System.Collections.Generic.KeyValuePair<System.String,System.Int32>> 

因此多數民衆贊成好,花花公子。然後我去消耗通過在ASP.Net這個Web方法:

List<KeyValuePair<string, int>> testkv = WCF.GetOptionSetValues("contact", "types"); 

但是,這並不工作,編譯器說:

無法隱式轉換類型System.Collections.Generic.KeyValuePair []以System.Collections.Generic.List System.Collections.Generic.KeyValuePair

出於某種原因,它似乎編譯器認爲從Web方法的返回類型爲:

System.Collections.Generic.KeyValuePair[] 

但它不是一個數組。它是一種:

System.Collections.Generic.KeyValuePair 

我很奇怪,爲什麼編譯器解釋網頁的方法KeyValuePair [],而不是KeyValuePair?

回答

1

KeyValuePairKeyValuePair列表的集合不是一回事。如果你想要一個List,你可能需要遍歷你的集合並構建一個。

foreach (KeyValuePair<string, int> pair in WCF.GetOptionSetValues("contact", "types")) 
{ 
    myList.Add(pair); 
}