我有 WCF 服务,其中包含包含来自 LibW.dll(我的 dll)的数据的列表。在主程序中,我还从 LibW.dll 中获得了列表。
我从 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>
怎么了?
最佳答案
大概是e.Result
是一个 ObservableCollection<T>
那么...即使您已将其声明为 List<IWeather>
为您服务。
看起来你还需要从object
转换至 IWeather
- 假设每个结果确实是一个IWeather
.您可以随时将其复制到列表中,如下所示:
cont = e.Result.Cast<IWeather>().ToList();
... 或更改您的变量类型,以便它可以处理任何 IList<IWeather>
.
关于c# - 无法将类型 System.Collections.ObjectModel.ObservableCollection<> 隐式转换为 System.Collections.Generic.List<>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9830950/