Using a generic type as a return type of an async method

后端 未结 2 1845
清歌不尽
清歌不尽 2020-12-17 20:35

A previous question made me wonder why the following method would raise a compile time error:

The return type of an async method must be void, Task or

相关标签:
2条回答
  • 2020-12-17 20:57

    I cannot think of a valid definition of MyMethodAsync that would allow it to return a generic T derived from Task without knowing at compile time what that type is or taking a parameter of some kind.

    If you are really returning a Task or Task<T> then you can update your signature to reflect that fact and avoid the problem.

    If you honestly need some type derived from Task then you will need to rewrite your logic to instead return Task or Task<T> and wrap around that other type. Assuming that is unacceptable you will need to drop the async and handle the state machine yourself.

    0 讨论(0)
  • 2020-12-17 20:59

    Three problems:

    • Just because T is "Task or a derived type" doesn't mean that it's Task or Task<T>. What would you expect if I called MyMethodAsync<MyCustomTask> where MyCustomTask derives from Task?

    • The compiler needs to know whether it's building a state machine returning Task or Task<T> when it compiles the method - it uses different helper classes in the different cases

    • If an async method has a return type of Task, any return statements can't specify a value; if it has a return type of Task<T> any return statements must specify a value which is implicitly convertible to T. How can that work within MyMethodAsync? It's a bit like saying "my method is either void or returns a T - you can decide when you call it".

    It's not clear what you're trying to achieve here, but basically this isn't going to work.

    0 讨论(0)
提交回复
热议问题