I was reading about the yield
keyword when I came across开发者_如何学JAVA a sample chapter from C# in Depth: http://csharpindepth.com/Articles/Chapter6/IteratorBlockImplementation.aspx.
The first block of code utilizes the yield
keyword to make a simple iterator. But, the second block of code shows this code after the compiler has had its way with it. Among other things, it has exploded the yield
statement into a state machine.
Several other examples of code being modified by the compiler is evident on the page.
My question is: Was the author actually able to access the code after compilation, or did he infer what it was going to look like?
You can have a look using Reflector, that's probably your best bet:
http://reflector.red-gate.com
The author himself mentioned:
Obviously the compiler doesn't actually produce C#, but I've used Reflector to decompile the code as C#.
in the same paragraph, titled High level overview: what's the pattern?
Probably both. It's quite easy to reverse-engineer compiled assemblies using Reflector. And the C# language spec, which defines how various syntactic-sugary things are compiled, is a public document. The author could have used either approach, or a mixture of the two.
Check out ildasm to take a look at the compiled IL.
(Really, it's good fun once you get your eye in)
.NET CLR actually has a form of assembly called MSIL, along with an assembler and dissembler. So yes, you can actually compile the code, then see the exact compiled CLR instructions.
https://web.archive.org/web/20211020103545/https://www.4guysfromrolla.com/articles/080404-1.aspx
dotPeek by JetBrains
https://www.jetbrains.com/decompiler/
It's free and easy to use :)
精彩评论