c# - 无法将类型 System.Collections.ObjectModel.ObservableCollection<> 隐式转换为 System.Collections.Generic.List<>

标签 c# wcf

我有 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/

相关文章:

在 Windows Azure 上托管时 Wcf 绑定(bind)的差异

c# - Outlook 2013 内联响应和表单区域

c# - 如何手动将带有列表的 View 模型映射到 dto

c# - Silverlight 自定义属性继承

visual-studio - 这些 WCF 服务引用文件是什么

c# - 将 unity.wcf 与 asp.net 路由集成

WPF + WCF + MVC + EF Web/桌面应用程序

c# - 在 SP2010 中添加事件

c# - C# 中的 BackgroundWorker2_RunWorkerCompleted 的无效跨线程操作

javascript - 将 javascript 变量设置为函数的返回值