- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
它说 here :
Async methods can have the following return types:
Task
, for an async method that performs an operation but returns novalue.
Task <TResult>
, for an async method that returns a value.
void
, for an event handler.Starting with C# 7.0, any type that has an accessible
GetAwaiter
method. The object returned by theGetAwaiter
method must implementtheSystem.Runtime.CompilerServices.ICriticalNotifyCompletion
interface.Starting with C# 8.0,
IAsyncEnumerable<T>
, for an async method thatreturns an async stream.
问题。如何理解这句话:“异步方法可以具有以下返回类型:...任何具有可访问的 GetAwaiter
方法的类型”,如果实际不是?
此代码无效:
using System.Runtime.CompilerServices;
async A Method() //Error CS1983 The return type of an async method must be void, Task, Task<T>, a task-like type, IAsyncEnumerable<T>, or IAsyncEnumerator<T>
{
await new A();
}
class A
{
public TaskAwaiter GetAwaiter()
{
return new TaskAwaiter();
}
}
最佳答案
摘要文档有点困惑。 GetAwaiter
不足以用作返回类型; GetAwaiter
更多关于 await
比async
.
In addition, the type returned from the GetAwaiter method must have the System.Runtime.CompilerServices.AsyncMethodBuilderAttribute attribute.
AsyncMethodBuilder
属性为编译器提供了足够的信息来实际构建和控制异步状态机的返回类型实例。
TaskAwaiter<T>
does not have这个属性。它通常不需要它,因为任务返回类型是“祖父”并且由编译器特殊处理。
关于c# - 异步方法不能返回任何具有可访问的 GetAwaiter 方法的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73439341/
我在 C# 中有这个 xUnit 方法,它测试一个 web api [Fact] public async Task GetWeatherForecast() {
这个问题在这里已经有了答案: Calling async methods from non-async code (4 个答案) 关闭 3 年前。 我有一个需要同步调用的异步任务(是的,不幸的是,这
它说 here : Async methods can have the following return types: Task, for an async method that performs
在 .net 4.0 中,我经常使用 Task.ContinueWith。但随后 I spotted "task.GetAwaiter()"这似乎具有相同的目的。 有什么不同? 最佳答案 如果你的目标
下面我粘贴了一些代码。在正文的第一行,我调用了一个等待调用 Task> GetSkuGcnList();最后一行,在 mapPromise.GetAwaiter() 中获取结果时抛出此异常: An e
我在使用下面的一组代码时遇到以下错误,它在“alliancelookup”行上出错,我不确定我做错了什么,但我自己看不到任何东西。我运行到 crest 的查询似乎运行良好,但我遇到的问题似乎与 Awa
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 7 年前。 Improve this qu
我在 Entity Framework 中使用 WebAPI 来创建新端点,但我遇到了一些问题。我正在尝试使用 Linq Where 语句来获取我的数据,但我收到以下错误。 'IQueryable'
我遇到了多个错误。因为我是这个异步/等待过程的新手。因此,我几乎没有做任何研究:- 我有一个像这样的函数:- public async Task GetMultipleTblResult(AdminB
我正在试验任务。我有 private async Task GetStringWithInnerCallConfigureAwaitFalseAsync() { await Task.Dela
我正在尝试为我的一个项目寻找最佳实践。它是一个典型的 WPF 应用程序,具有显示项目列表的 UI 和返回结果的数据服务。 我们正在异步调用服务,以免阻塞 UI。我们面前有 2 个选择: 使用 Asyn
我在一些地方读到 .GetAwaiter().GetResult(); 可能导致死锁,我们应该使用 async/await 相反。但是我看到很多使用它的代码示例。可以用吗?在哪些情况下会出现死锁?还有
客户端 iGame Channel = new ChannelFactory ( new BasicHttpBinding ( BasicHttpSecurityMode . None ) , new
假设我有一个 Task 生成 int ,以及接受 int 的回调: Task task = ...; Action f = ...; 现在我想设置它,以便一旦任务完成并返回其整数结果,callfack
我正在尝试为我的一个项目找到最佳实践。它是一个典型的 WPF 应用程序,其 UI 显示项目列表,并且有一个返回结果的数据服务。 我们异步调用服务,以免阻塞 UI。我们面前有两个选择: 使用 Async
我正在尝试同步运行异步任务,并且想知道 .RunSynchronously() 之间有什么区别和GetAwaiter().GetResult()是。 我见过很多 .Wait() 之间的比较以及这两种方
早上好,这是我在社区中的第一个问题。我正在使用 C# WebAPI 框架,我正在尝试将所有 Controller 的方法更改为异步方法。我的 Controller 从 GenericControlle
我有这个代码: async function test() { var mongo = await MongoClient.connect(connectionString); var
这个问题在这里已经有了答案: UWP app: FileOpenPicker PickSingleFileAsync() can't await (1 个回答) 关闭 5 年前。 我正在构建一个 U
我有一个返回对象列表<>的方法。此方法需要一段时间才能运行。 private List GetBigList() { ... slow stuff } 此方法从 4 或 5 个来源调用。所以,
我是一名优秀的程序员,十分优秀!