如何Microsoft.Bcl.Async工作?工作、Microsoft、Bcl、Async

2023-09-03 12:26:43 作者:你可真够呛!

Microsoft.Bcl.Async 开发人员可以使用异步/计谋无关键字的.NET Framework 4.5,他们是应该定位到使用它们。

Microsoft.Bcl.Async enables developers to use async/await keywords without .NET Framework 4.5 that they are supposed to target to use them.

这是伟大的,这要感谢的人在微软CLR和语言团队非常努力的工作。

That's great, thanks to the incredibly hard work of people in the Microsoft CLR and language teams.

现在我很好奇如何能正常工作。

Now I am curious how this can work.

异步/计谋要求编译器做一些繁重的转向code到的东西,可以做的等待操作。

async/await require the compiler to do some heavy lifting to turn code into something that can do await operations.

编译器的原的引发下,.NET Framework 4.0中编译错误,即使它清楚地知道什么异步/计谋平均值(的Visual Studio 2012 / 2013。)

The compiler originally throws compile errors under .NET Framework 4.0, even if it clearly knows what async/await mean(Visual Studio 2012/2013.)

那么,这怎样库告诉编译器不要把涉及到异步操作特定的编译错误,并做一些code起重就像在.NET框架4.5?

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?

如何在 Microsoft.Bcl.Async 组件会导致编译器来识别一个新的关键字(异步/等待)?

How does the Microsoft.Bcl.Async assembly cause the compiler to recognize a new keyword (async/await)?

推荐答案

异步/计谋无非是C#5.0编译器改造。没有异步/计谋 IL 的水平。

async/await is nothing but C# 5.0 compiler transformation. There is no async/await at IL level.

一个简单的例子是使用(){} 语句,这也是一个编译器改造。它只是转换了使用语句尝试/终于块。然而,在存在的IDisposable 接口的依赖关系是在.NET 1.1中定义。

One simple example would be the using() { } statement which is also a compiler transformation. It just converts the using statement to try/finally block. However, there is a dependency on existence of IDisposable interface which is defined in .NET 1.1.

同样,异步/计谋转变依赖于某些类型的如 IAsyncStateMachine 接口,这是在.NET 4.5中定义。该 Microsoft.Bcl.Async 获得这些类型定义到.NET 4.0。

Similarly, the async/await transformation depends on certain types like IAsyncStateMachine interface which are defined in .NET 4.5. The Microsoft.Bcl.Async gets those type definitions to .NET 4.0.

修改

如何在Microsoft.Bcl.Async组件会导致编译器来识别一个新的关键字(异步/等待)?

How does the Microsoft.Bcl.Async assembly cause the compiler to recognize a new keyword (async/await)?

没有事实并非如此。 C#5.0编译器已经知道的关键字和如何处理它们。然而,它不能找到所需的类型作为项目的目标是到.NET 4.0。该 Microsoft.Bcl.Async 包带来了这些类型。

No it does not. C# 5.0 compiler already knows about the keywords and what to do with them. However, it can't find the required types as project is targeted to .NET 4.0. The Microsoft.Bcl.Async package brings in those types.