简体   繁体   中英

How is it possible to see C# code after compilation/optimization?

I was reading about the yield keyword when I came across 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.

http://www.4guysfromrolla.com/articles/080404-1.aspx

by JetBrains by JetBrains

https://www.jetbrains.com/decompiler/

It's free and easy to use :)

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