如何在C#中实现基于任务的接口方法 [英] How to implement task based interface method in C#

查看:77
本文介绍了如何在C#中实现基于任务的接口方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

公共类ICountry< t>

{

任务< IEnumerable< T>> GetAllAsync(); 





}



公共类国家< t> :ICountry< t>

{

public async Task< ienumerable>< t>> GetAllAsync()

{

//返回await _Context.Set< t>()。ToList();



错误是

 '  List< t> '不包含定义  '  GetAwaiter'且没有扩展方法'  GetAwaiter'接受类型为' 列表< t>'可以找到(你错过了使用指令或汇编参考?)





} < br $>
}



我尝试过:



i试试这个



公共异步任务< ienumerable>< t>> GetAllAsync()

{

返回等待_Context.Set< t>()。ToList();

//错误

'List< t>'不包含'GetAwaiter'的定义,并且没有扩展方法'GetAwaiter'接受类型为'List< t>'的第一个参数'(您是否缺少using指令或汇编参考?)



}

它显示错误

List< t>不包含getawaiter的定义没有扩展方法

解决方案

看看这是否给你一些想法:

 使用系统; 
使用 System.Collections.Generic;
使用 System.Threading.Tasks;

命名空间 YourNameSpace
{
public interface ICountry< T>
{
任务< IEnumerable< T>> GetAllAsync();
}

public class AwaitEx< T> :ICountry< T>
{
public async 任务< IEnumerable< T>> GetAllAsync(???? context)
{
// ????返回await context.Set(????);
}
}
}

您需要显示代码,告诉我们'上下文'是


public class ICountry<t>
{

Task<IEnumerable<T>> GetAllAsync();



}

public class Country<t>:ICountry<t>
{
public async Task<ienumerable><t>> GetAllAsync()
{
//return await _Context.Set<t>().ToList();

error is

'List<t>' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'List<t>' could be found (are you missing a using directive or an assembly reference?)



}
}

What I have tried:

i try this

public async Task<ienumerable><t>> GetAllAsync()
{
return await _Context.Set<t>().ToList();
// error
'List<t>' does not contain a definition for 'GetAwaiter' and no extension method 'GetAwaiter' accepting a first argument of type 'List<t>' could be found (are you missing a using directive or an assembly reference?)

}
it show me the error
List<t> does not contain defination for getawaiter no extension method

解决方案

See if this gives you some ideas:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace YourNameSpace
{
    public interface ICountry<T> 
    {
        Task<IEnumerable<T>> GetAllAsync();
    }

    public class AwaitEx<T> : ICountry<T>
    {
        public async Task<IEnumerable<T>> GetAllAsync(???? context)
        {
            // ???? return await context.Set(????);
        }
    }
}

You need to show code that will tell us what 'Context is.


这篇关于如何在C#中实现基于任务的接口方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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