如何在VS2017 RC中使用新的异步语义? [英] How do I get the new async semantics working in VS2017 RC?

查看:88
本文介绍了如何在VS2017 RC中使用新的异步语义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

引用Visual Studio 2017 RC发行说明

语言扩展和分析器

此版本包括一些提议的新语言扩展,我们正在为下一版本的C#和Visual Basic进行开发.这些新的语言功能默认情况下处于启用状态,其中包括:

This release includes some proposed new language extensions that we are working on for the next versions of C# and Visual Basic. These new language features are enabled by default and include:

对于C#:

  • Task-like return types for async methods: This introduces the ability to return any task-like type from an async method. Previously these return types were constrained to Task<T> and Task.

它说它是默认启用的,但是我无法使它工作.即使从链接的Github页面上下载了确切的ArbitraryAsyncReturns.zip文件(并修复了对React NuGet软件包的引用以消除不相关的错误),但是却没有安装自定义VSIX软件包(适用于VS2015),我仍然可以得到

It says it's enabled by default, but I'm not able to get this to work. Even taking the exact ArbitraryAsyncReturns.zip download from the linked Github page (and fixing up the references to React NuGet packages to remove unrelated errors), but without installing the custom VSIX package (which is for VS2015), I continue to get

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

error CS1983: The return type of an async method must be void, Task or Task<T>

我是否需要采取其他措施才能使此工作正常进行?

Do I need to take any additional steps to get this working?

我首先尝试将特定示例缩小为应该工作的最小版本,但是尝试使用它时,我仍然不知道什么应该工作,什么不应该.至少,鉴于这种语言的增强,我希望有一个伪造的程序,例如

I first tried reducing that specific example to a minimal version that should work, but trying to play with it, I didn't know yet what should work and what shouldn't. At the very least though, given this language enhancement, I expected a bogus program such as

struct Test { }
static class Program {
    static async Test Test() { }
    static void Main() { }
}

无法使用其他错误消息进行编译.即使收到相同的错误消息,也表明尚未启用此语言扩展,但是JaredPar注意到错误消息只是尚未更新.

to fail to compile with a different error message. Getting the same error message even then suggested that this language extension was not yet enabled, but JaredPar noticed that the error message simply hasn't been updated yet.

我现在将一个可能有效的示例简化为我认为应该编译的最小版本(但由于未实现的方法而在运行时失败),但无法编译:

I now reduced one of the supposedly valid examples to a minimal version that I think should compile (but fail at run-time due to the unimplemented methods), but doesn't compile:

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";
    }
}

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

The same compiler error as above is generated for static async TasklikeType<string> TasklikeTypeTester().

推荐答案

TasklikeAttribute属性名称原来不是VS2017 RC中实现的,而是来自该提案的不同版本.实际实现的内容取决于类型System.Runtime.CompilerServices.AsyncMethodBuilderAttribute,它的工作方式似乎完全相同.

The TasklikeAttribute attribute name turns out isn't what's implemented in VS2017 RC, that's from a different version of the proposal. What's actually implemented relies on a type System.Runtime.CompilerServices.AsyncMethodBuilderAttribute, which appears to work exactly the same way.

我找不到此文档,但是我可以在Roslyn测试中找到它,例如

I was not able to find this documented, but I was able to find this in the Roslyn tests, for example CodeGenAsyncTests.cs:

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

这篇关于如何在VS2017 RC中使用新的异步语义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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