无法隐式转换类型System.Collections.ObjectModel.ObsObservableCollecti
本文关键字:System Collections ObjectModel ObsObservableCollecti 类型 转换 | 更新日期: 2023-09-27 18:28:38
我有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>
。
需要考虑的一件事是,在服务引用的配置中(右键单击服务引用,然后单击"配置服务引用"),将集合类型从"Observable collection"更改为"System.Collections.Generic.List"