简体   繁体   English

如何在编译/优化后看到C#代码?

[英]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 . 当我从深度的C#中看到一个示例章节时,我正在阅读关于yield关键字: http//csharpindepth.com/Articles/Chapter6/IteratorBlockImplementation.aspx

The first block of code utilizes the yield keyword to make a simple iterator. 第一个代码块使用yield关键字来创建一个简单的迭代器。 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. 除此之外,它还将yield语句分解为状态机。

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: 你可以使用Reflector看看,这可能是你最好的选择:

http://reflector.red-gate.com 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#. 显然编译器实际上并没有生成C#,但我使用Reflector将代码反编译为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 . 使用Reflector对编译的程序集进行逆向工程很容易。 And the C# language spec, which defines how various syntactic-sugary things are compiled, is a public document. 而C#语言规范是一个公共文档,它定义了各种语法含糖事物的编译方式。 The author could have used either approach, or a mixture of the two. 作者可以使用任何一种方法,或两者​​的混合。

Check out ildasm to take a look at the compiled IL. 查看ildasm以查看已编译的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. .NET CLR实际上有一种称为MSIL的程序集,以及汇编程序和解析程序。 So yes, you can actually compile the code, then see the exact compiled CLR instructions. 所以,是的,您可以实际编译代码,然后查看确切编译的CLR指令。

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

dotPeek by JetBrains dotBeek by JetBrains

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

It's free and easy to use :) 它免费且易于使用:)

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

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