简体   繁体   中英

ILSpy generated code

I'm trying to figure out how Tasks in C# works under the hood. I've found this article.

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"?

I googled for IL opcode reference, as well as for ILSpy tutorial, which could explain this, but nothing. I'm still curious of full understanding of Task pattern in C#.

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.

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.

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.

So that's it really, those are legal fields, IL-wise, but illegal names, C#-wise.

Beyond the name, they're just ... fields.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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