[英]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.