2012-03-22 95 views
1

我已經有WCF服務,其中包含來自LibW.dll(我的DLL)的數據列表。在主程序中,我也從LibW.dll獲得了列表。不能隱式轉換類型System.Collections.ObjectModel.ObservableCollection <>到System.Collections.Generic.List <>

我返回列表從WCF服務

[OperationContract] 
public List<IWeather> Final() 
{ 
    return returner; 
} 

,然後嘗試設置方法的結果重視

cont = e.Result; 

其中

List<LibW.IWeather> cont=new List<LibW.IWeather>(); 

但我有這樣的錯誤 不能隱式轉換類型System.Collections.ObjectModel.ObservableCollection<object>' to 'System.Collections.Generic.List<NavigationGadget.IWeather>

怎麼了?

+0

請參閱[我是否*在Silverlight WCF客戶端中使用ObservableCollection?](http://stackoverflow.com/q/1911441/119477) – 2012-03-22 21:48:44

回答

5

據推測e.ResultObservableCollection<T>然後......即使您已在您的服務中聲明它爲List<IWeather>

看起來你需要投從objectIWeather - 假設每個結果真的IWeather。你總是可以其複製到一個列表,像這樣:

cont = e.Result.Cast<IWeather>().ToList(); 

...或更改變量的類型,因此它可以處理任何IList<IWeather>

+0

現在我有錯誤=無法轉換類型System.Collections。 Generic.List to System.Collections.Generic.List 2012-03-22 21:52:58

+0

@АртёмЦарионов:好的,用你原來的格式(我已經修復),我們看不到涉及的類型。將編輯。 – 2012-03-22 21:54:09

3

需要考慮的一件事是,在服務參考的配置中(右鍵單擊服務參考,然後單擊「配置服務參考」),將集合類型從「Observable Collection」更改爲「System.Collections.Generic.List 「

相關問題