如何在VS2017 RC中使用新的异步语义?

11 c# async-await c#-7.0 visual-studio-2017

Visual Studio 2017 RC发行说明引用

语言扩展和分析器

此版本包括一些我们正在为下一版本的C#和Visual Basic工作的新语言扩展.默认情况下启用这些新语言功能,包括:

对于C#:

它说它默认启用,但我无法让它工作.甚至从链接的Github页面下载精确的ArbitraryAsyncReturns.zip(并修复对React NuGet包的引用以删除不相关的错误),但是没有安装自定义VSIX包(用于VS2015),我继续得到

错误CS1983:异步方法的返回类型必须为void,Task或Task <T>

我是否需要采取任何其他步骤才能使其正常工作?


我首先尝试将该特定示例缩减为应该起作用的最小版本,但尝试使用它,我还不知道应该使用什么,什么不应该.至少,鉴于这种语言增强,我期待一个虚假的程序,如

struct Test { }
static class Program {
    static async Test Test() { }
    static void Main() { }
}
Run Code Online (Sandbox Code Playgroud)

无法使用不同的错误消息进行编译.获得相同的错误消息,甚至提示此语言扩展尚未启用,但JaredPar注意到错误消息尚未更新.


我现在将一个所谓的有效示例减少到我认为应该编译的最小版本(但由于未实现的方法而在运行时失败),但是不编译:

using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;

namespace System.Runtime.CompilerServices {
    public class TasklikeAttribute : Attribute {
        public TasklikeAttribute(Type builderType) { }
    }
}

struct TasklikeTypeMethodBuilder<T> {
    public static TasklikeTypeMethodBuilder<T> Create() => throw new NotImplementedException();
    public void Start<TStateMachine>(ref TStateMachine stateMachine) where TStateMachine : IAsyncStateMachine => throw new NotImplementedException();
    public void SetStateMachine(IAsyncStateMachine stateMachine) => throw new NotImplementedException();
    public void SetResult(T result) => throw new NotImplementedException();
    public void SetException(Exception exception) => throw new NotImplementedException();
    public TasklikeType<T> Task => throw new NotImplementedException();
    public void AwaitOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : INotifyCompletion where TStateMachine : IAsyncStateMachine => throw new NotImplementedException();
    public void AwaitUnsafeOnCompleted<TAwaiter, TStateMachine>(ref TAwaiter awaiter, ref TStateMachine stateMachine) where TAwaiter : ICriticalNotifyCompletion where TStateMachine : IAsyncStateMachine => throw new NotImplementedException();
}

[Tasklike(typeof(TasklikeTypeMethodBuilder<>))]
struct TasklikeType<T> { }

static class Program {
    static void Main(string[] args) { }
    static async TasklikeType<string> TasklikeTypeTester() {
        await Task.Yield();
        return "hello";
    }
}
Run Code Online (Sandbox Code Playgroud)

生成与上面相同的编译器错误static async TasklikeType<string> TasklikeTypeTester().

Jar*_*Par 6

您无需其他任何操作即可启用类似任务的返回.这里的问题是此功能的diagonstic消息尚未更新.以下是跟踪问题的链接:

https://github.com/dotnet/roslyn/issues/12621


小智 5

结果显示的TasklikeAttribute属性名称不是VS2017 RC中实现的,而是来自提案的不同版本.实际实现的内容依赖于一种类型System.Runtime.CompilerServices.AsyncMethodBuilderAttribute,它似乎以完全相同的方式工作.

我无法找到这个记录,但我能够在Roslyn测试中找到它,例如CodeGenAsyncTests.cs:

[AsyncMethodBuilder(typeof(ValueTaskMethodBuilder))]
struct ValueTask { }
...
namespace System.Runtime.CompilerServices { class AsyncMethodBuilderAttribute : System.Attribute { public AsyncMethodBuilderAttribute(System.Type t) { } } }
Run Code Online (Sandbox Code Playgroud)

  • `[AsyncMethodBuilder]`比`[Tasklike]`:( (2认同)