简体   繁体   English

无法将类型'System.Collections.Generic.List <>'隐式转换为'System.Threading.Tasks.Task <>>

[英]Cannot implicitly convert type 'System.Collections.Generic.List<>' to 'System.Threading.Tasks.Task<>>

I am getting an exception. 我得到一个例外。

Cannot implicitly convert type 'System.Collections.Generic.List<IntegraPay.Domain.SObjects.Industry>' to 'System.Threading.Tasks.Task<System.Collections.Generic.IEnumerable<IntegraPay.Domain.SObjects.Industry>>' 无法将类型'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;
        }

This error typically happens when you are missing async in the method declaration. 当您在方法声明中缺少async时,通常会发生此错误。

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时,C#编译器会添加“magic”来执行从对象到返回该对象的Task<T>的转换。

However, in your situation async is unnecessary, because you return a task with a result that you already have: 但是,在您的情况下, async是不必要的,因为您返回的任务具有您已经拥有的结果:

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

Your method return type is 你的方法返回类型是

Task<IEnumerable<WebFormFieldContent>> 

but the implementation returns a 但实现返回一个

List<WebFormFieldContent>. 

You can change it to 你可以把它改成

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

Or change the private variable to 或者将私有变量更改为

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

type. 类型。

Or you can add an async key word to make it a synchronous call 或者,您可以添加异步关键字以使其成为同步调用

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法隐式转换'System.Threading.Tasks.Task<system.collections.generic.list> TO'System.Collections.Generic.List&lt;&gt;</system.collections.generic.list> - Cannot implicitly convert 'System.Threading.Tasks.Task<System.Collections.Generic.List> TO 'System.Collections.Generic.List<> xamarin表单无法隐式转换类型&#39;system.threading.tasks.task - xamarin forms cannot implicitly convert type 'system.threading.tasks.task<System.collections.generic list to system.collections.generic List 无法从System.Collections.Generic.List转换 <sales.ScanInfo> 到System.Collections.Generic.IEnumerable <System.Threading.Tasks.Task> - cannot convert from System.Collections.Generic.List<sales.ScanInfo> to System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task> 无法将类型&#39;void&#39;隐式转换为&#39;System.Threading.Tasks.Task&#39; - Cannot implicitly convert type 'void' to 'System.Threading.Tasks.Task' 无法隐式转换类型&#39;System.Threading.Tasks.Task - Cannot implicitly convert type 'System.Threading.Tasks.Task 无法将类型&#39;bool&#39;隐式转换为&#39;System.Threading.Tasks.Task&#39; - Cannot implicitly convert type 'bool' to 'System.Threading.Tasks.Task' 循环&#39;System.Threading.Tasks.Task类型的对象时,foreach语句中的编译错误 - Compilation error in foreach statement while looping an object of type 'System.Threading.Tasks.Task<System.Collections.Generic.List' i 无法隐式转换类型&#39;System.Collections.Generic.list <fooClass> &#39;到&#39;System.Collections.Generic.List <T> - Cannot Implicitly convert type 'System.Collections.Generic.list<fooClass>' to 'System.Collections.Generic.List<T> 无法隐式转换类型&#39;System.Collections.Generic.List <AnonymousType#1> &#39;到&#39;System.Collections.Generic.List - Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List 无法隐式转换类型&#39;System.Collections.Generic.List <MyProduct> &#39;到&#39;System.Collections.Generic.List <T> - Cannot implicitly convert type 'System.Collections.Generic.List<MyProduct>' to 'System.Collections.Generic.List<T>
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM