简体   繁体   English

ILSpy生成的代码

[英]ILSpy generated code

I'm trying to figure out how Tasks in C# works under the hood. 我试图弄清楚C#中的Tasks是如何在后台运行的。 I've found this article. 我找到了这篇文章。

http://foreverframe.net/what-lies-beneath-asyncawait-in-c/ http://foreverframe.net/what-lies-beneath-asyncawait-in-c/

But, I've spent some time seeking for meaning of some line, but didn't find anything. 但是,我花了一些时间寻找某些行的含义,但没有找到任何东西。

For example. 例如。

        private int <>s__4;

        private int <>s__5;

        private int <>s__6;

What does " <> " symbol mean? “ <>”符号是什么意思? There a plenty of examples decompuled c# source code, which alway contain something like "<>d__0"? 有大量的反编译c#源代码示例,其中总包含“ <> d__0”之类的内容?

I googled for IL opcode reference, as well as for ILSpy tutorial, which could explain this, but nothing. 我在Google上搜索了IL操作码参考以及ILSpy教程,该教程可以解释这一点,但一无所获。 I'm still curious of full understanding of Task pattern in C#. 我仍然很好奇对C#中的Task模式的完全理解。

Thanks in advance for help with this example, or pointing me for appropriate documentation/references. 在此先感谢您为本示例提供的帮助,或为我提供适当的文档/参考。

The <> characters are illegal in C#, but legal in IL. <>字符在C#中是非法的,但在IL中是合法的。

Let me explain. 让我解释。

When the compiler autogenerates code, such as when it transforms a method using yield return into a state machine, or a async/await method into something similar, it will often construct a class to host this transformed code. 当编译器自动生成代码时,例如当它使用yield return将方法转换为状态机或将async/await方法转换为类似方法时,它通常会构造一个类来承载此转换后的代码。

What used to be local variables are also rewritten to be fields on this class, in order to survive the state transitions which usually end up returning from a method. 过去曾经是局部变量的内容也被重写为该类上的字段,以便承受通常从方法返回的状态转换。

The fields you see are those generated fields. 您看到的字段是那些生成的字段。 The names here are chosen by the compiler in such a way to be legal in IL, but illegal in C#, which means that under no circumstances will the compiler accidentally generate a name that you've also used for a field or identifier. 此处的名称是由编译器以这种方式选择的,即在IL中合法,但在C#中是非法的,这意味着在任何情况下编译器都不会意外生成一个您也用于字段或标识符的名称。

So that's it really, those are legal fields, IL-wise, but illegal names, C#-wise. 就是这样,这些都是合法的字段(IL方面),但非法名称(C#方面)。

Beyond the name, they're just ... fields. 除了名字,它们只是...字段。

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

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