无法隐式转换类型'System.Collections.Generic.List<>'到"System.Threading.Tasks.Task<> [英] Cannot implicitly convert type 'System.Collections.Generic.List<>' to 'System.Threading.Tasks.Task<>>

查看:312
本文介绍了无法隐式转换类型'System.Collections.Generic.List<>'到"System.Threading.Tasks.Task<>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要例外了.

不能将类型'System.Collections.Generic.List<IntegraPay.Domain.SObjects.Industry>'隐式转换为'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<IntegraPay.Domain.SObjects.Industry>>'

下面是我的属性和方法.

Below is my property and method.

  private List<WebFormFieldContent> WebFormFields { get; set; } = 
       new List<WebFormFieldContent>();

  Task<IEnumerable<WebFormFieldContent>> IRegistrationRepository.GetWebFormFields()
        {
            return WebFormFields;
        }

推荐答案

当方法声明中缺少async时,通常会发生此错误.

This error typically happens when you are missing async in the method declaration.

async放在签名中时,C#编译器会添加魔术",以执行从对象到返回该对象的Task<T>的转换.

When you put async in the signature, C# compiler adds "magic" to do the conversion from an object to a Task<T> returning that object.

但是,根据您的情况,async是不必要的,因为您返回的任务的结果已经是:

However, in your situation async is unnecessary, because you return a task with a result that you already have:

return Task.FromResult<IEnumerable<WebFormFieldContent>>(
    WebFormFields
);

这篇关于无法隐式转换类型'System.Collections.Generic.List&lt;&gt;'到"System.Threading.Tasks.Task&lt;&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆