简体   繁体   English

Microsoft.Bcl.Async如何工作?

[英]How Microsoft.Bcl.Async works?

Microsoft.Bcl.Async enables developers to use async/await keywords without .NET Framework 4.5 that they are supposed to target to use them. Microsoft.Bcl.Async使开发人员能够使用async/await关键字,而不使用.NET Framework 4.5,他们应该将它们作为目标使用它们。

That's great, thanks to the incredibly hard work of people in the Microsoft CLR and language teams. 这很棒,这得益于Microsoft CLR和语言团队中人员的辛勤工作。

Now I am curious how this can work. 现在我很好奇这是如何工作的。

async/await require the compiler to do some heavy lifting to turn code into something that can do await operations. async/await要求编译器做一些繁重的工作来将代码变成可以等待操作的东西。

The compiler originally throws compile errors under .NET Framework 4.0, even if it clearly knows what async/await mean(Visual Studio 2012/2013.) 编译器最初在.NET Framework 4.0下抛出编译错误,即使它清楚地知道async/await是什么意思(Visual Studio 2012/2013。)

So how does this library tell the compiler not to throw specific compile errors related to asynchronous operations, and do some code lifting just like being under .NET Framework 4.5? 那么这个库如何告诉编译器不要抛出与异步操作相关的特定编译错误,并且像在.NET Framework 4.5下一样解除一些代码?

async/await is nothing but C# 5.0 compiler transformation. async/await只不过是C#5.0编译器转换。 There is no async/await at IL level. IL级别没有async/await

One simple example would be the using() { } statement which is also a compiler transformation. 一个简单的例子是using() { }语句,它也是一个编译器转换。 It just converts the using statement to try/finally block. 它只是将using语句转换为try/finally块。 However, there is a dependency on existence of IDisposable interface which is defined in .NET 1.1. 但是,依赖于.NET 1.1中定义的IDisposable接口的存在。

Similarly, the async/await transformation depends on certain types like IAsyncStateMachine interface which are defined in .NET 4.5. 同样, async/await转换依赖于某些类型,如.NET 4.5中定义的IAsyncStateMachine接口。 The Microsoft.Bcl.Async gets those type definitions to .NET 4.0. Microsoft.Bcl.Async将这些类型定义获取到.NET 4.0。

EDIT 编辑

How does the Microsoft.Bcl.Async assembly cause the compiler to recognize a new keyword (async/await)? Microsoft.Bcl.Async程序集如何使编译器识别新关键字(async / await)?

No it does not. 不,不是的。 C# 5.0 compiler already knows about the keywords and what to do with them. C#5.0编译器已经知道关键字以及如何处理它们。 However, it can't find the required types as project is targeted to .NET 4.0. 但是,由于项目针对.NET 4.0,因此无法找到所需的类型。 The Microsoft.Bcl.Async package brings in those types. Microsoft.Bcl.Async包引入了这些类型。

MS2012正在安装.net 4.5,因此您无法获得“编译错误4.0”,如您所述

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

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