简体   繁体   English

为什么异步方法返回类型不能从Task派生?

[英]Why async method return type can not be derived from Task?

I have a class (simplified sample and only relevant parts shown) 我有一个班级(简化样本,只显示相关部分)

public class AsyncScenario : Task<Scenario>
{
    public async AsyncScenario Test(Func<Task> action)
    {
        return await this;           
    }
}

I can't compile this because the return type of the method: 我无法编译这个因为方法的返回类型:

Compiler error CS1983: "The return type of an async method must be void, Task or Task<T>" 编译器错误CS1983:“异步方法的返回类型必须为void,任务或任务<T>”

Why is it not allowed for async method to return a class derived from Task<T> ? 为什么不允许异步方法返回从Task<T>派生的类?

Because the C# specification says so? 因为C#规范是这样说的吗? Short of Eric Lippert or some other language design team person chiming in , you're not going to get a better answer than that. 没有Eric Lippert或其他一些语言设计团队人员 ,你不会得到更好的答案。 Anything else would be pure speculation and opinion. 其他任何事情都是纯粹的猜测和观点。

That said, given that in an async method, the return value is implied by the return statement, what do you think the specification rules should be in such an example? 也就是说,在async方法中,返回值由return语句隐含,您认为规范规则在这样的示例中应该是什么? Clearly await this resolves to a Scenario object...how is the C# compiler supposed to synthesize a new AsyncScenario object from that expression type? 显然await this解析为一个Scenario对象...... C#编译器应该如何从该表达式类型合成一个新的AsyncScenario对象?

I suppose you might come up with some additional rules you could put in the specification that would unambiguously provide for some mechanism. 我想你可能会在规范中提出一些额外的规则,这些规则可以明确地提供一些机制。 But it seems to me those rules would be significantly more complex than the rules that exist as the specification stands now, where the return type is always a type that is known to the spec author and has clear rules for creating the first time the method returns and, just as important, changing the state of when the return statement is executed. 但在我看来,这些规则将显著比存在的规范,现在矗立,这里的返回类型始终是已知的规格笔者类型和具有明确的规则,用于创建第一次该方法返回的规则更为复杂同样重要的是,改变执行return语句的状态。

It seems likely to me that the rules exist as they do, simply because to do otherwise would require an inordinate amount of effort, and would dramatically increase the complexity of compilers and the likelihood of bugs in said compilers. 在我看来,这些规则很可能存在,只是因为这样做会需要过多的努力,并且会大大增加编译器的复杂性以及所述编译器中出现错误的可能性。


† Speaking of which, a little searching dug up gold: Why must async methods return Task? †说到这一点,一点点挖掘挖出金币: 为什么异步方法必须返回任务? , written by Lucian Wischik, language designer for VB.NET (but the article is similarly applicable to C#). ,由VB.NET语言设计师Lucian Wischik编写(但该文章同样适用于C#)。 See also Jon Skeet's answer at Using a generic type as a return type of an async method . 另请参阅Jon Skeet在使用泛型类型作为异步方法的返回类型时的答案。

You can await any method, that returns type with GetAwaiter method: 您可以等待任何使用GetAwaiter方法返回类型的方法:

    public static async Task<Scenario> Test()
    {
        return await new AsyncScenario();
    }

    public class AsyncScenario
    {
        public TaskAwaiter<Scenario> GetAwaiter()
        {
            return new TaskAwaiter<Scenario>();
        }
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM