如何查看C#编译器生成的MSIL / CIL?为什么叫组装呢?
我是 .NET C# 编程新手。我正在关注几本书。据说,不是将 C# 代码直接编译为机器代码,而是将其转换为中间语言(称为 MSIL 又名 CIL)。但是当我编译时,我得到一个 exe/dll 文件。
- 这些 exe/dll 文件中是否包含此 MSIL/CIL?
- 我想查看中间语言代码,只是为了感受它的存在。我该如何查看?
- 他们将此 exe/dll 文件称为
程序集
。他们使用这个“花哨的词”只是为了将它们与包含本机/机器代码的 exe/dll 文件区分开来吗?
I'm new to .NET C# programming. I'm following few books. It is said that instead of compiling C# code directly to machine code, it is converted into an intermediate language (called MSIL aka CIL). But when I compile, I get an exe/dll file.
- Is this MSIL/CIL contained in these exe/dll files?
- I want to see that intermediate language code, just to get feel for its existence. How do I view it?
- They are calling this exe/dll file an
assembly
. Are they using this "fancy word" just to differentiate these from the exe/dll files that contain native/machine code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
如果我也可以推荐一本关于这个问题的好书,那就是 Serge Lidin 编写的 Expert .NET 2.0 IL Assembler。他是 MSIL 的设计者。
.text
section of the PE file (portable executable = *.exe or *.dll). More information can be found here.If I may recommend a good book on that matter too, it's Expert .NET 2.0 IL Assembler by Serge Lidin. He's the guy who designed MSIL.
我最喜欢的查看 C# 代码片段 IL 的方法之一是使用免费的 LINQPad 工具< /a>.输入一些代码并选择顶部的“C# statements”(或“C# Program”,以适合的为准)后,单击代码输入区域下方的“IL”按钮,您将看到生成的IL。
为此,使用 LINQPad 比加载 Visual Studio 或从命令行运行编译器,然后加载 ILDASM 并使用文本编辑器打开 .il 文件要方便得多。
One of my favorite ways to see IL for a snippet of C# is to use the free LINQPad tool. After entering some code and choosing "C# statements" at the top (or "C# Program", whichever suits), click the "IL" button under the code entry area, and you will see the generated IL.
Using LINQPad for this is much more convenient than loading up Visual Studio or running the compiler from the command line, then loading up ILDASM and opening the .il file with a text editor.
如果您想在线使用,.NET Fiddle 非常好。只需粘贴您的代码并单击右上角的
View IL
选项即可。If you want it online, .NET Fiddle is excellent. Just paste your code and click
View IL
option at the top right.另一种选择:使用 ReSharper
源/IL 同步视图:左侧蓝色背景线与右侧 IL 块相对应
在 Visual Studio 中:
支持源代码和 IL 的同步视图 - 当您单击源代码中的语句时,IL 中的相应块会突出显示(反之亦然)。显示来自 Microsoft Developer Network 的 IL 描述和来自 ECMA 标准规范的“通用中间语言 (CIL) 指令集”。
请参阅 Resharper 帮助中的查看中间语言 (IL)。上图来自 Resharper 帮助。
免费选项是使用 Jetbrains dotPeek
另请参阅:“使用 ReSharper 探索中间语言 (IL) 和dotPeek”,作者 Maarten Balliauw,2017 年 1 月 19 日 - Jetbrains 博客
Another option: Use ReSharper
Source / IL synced view: left blue background line corresponds with right IL Block
In Visual Studio:
Supports synced view of Source Code and IL - when you click on a statement in source, the corresponding block in IL is highlighted (and vice versa). Displays descriptions for IL from Microsoft Developer Network and "Common Intermediate Language (CIL) instruction set" from ECMA standard specification.
see Viewing Intermediate Language (IL) in Resharper Help. Picture above is from Resharper Help.
Free option is to use Jetbrains dotPeek
see also: "Exploring Intermediate Language (IL) with ReSharper and dotPeek", by Maarten Balliauw, January 19, 2017 - Jetbrains Blog
sharplab 是一个在线工具,非常适合简单的用例。在左侧输入代码,IL 将显示在右侧。
sharplab is an online tool, great for simple use cases. Type your code on the left, IL shows up on the right.
我相信它们被称为“程序集”,因为程序集是一组模块,通过清单组装在一起。
(来源:microsoft.com)
请参阅 < a href="https://learn.microsoft.com/en-us/dotnet/framework/app-domains/ assembly-contents" rel="nofollow noreferrer">程序集内容了解详细信息。
I believe that they are called "assemblies" because an assembly is a set of modules, assembled together by a manifest.
(source: microsoft.com)
See Assembly Contents for details.
PS 由于您正在阅读一些书籍,我强烈推荐您通过 C# 进行 CLR。
P.S As you are following some books I will highly recommend you CLR via C#.
在许多方面,.NET 程序集与 Java 字节码包类似。
In many respects, .NET assemblies are similar to Java bytecode packages.
我知道这是一个老问题,我更喜欢上面的任何工具。不过,在紧要关头,至少从 2005 版开始,Visual Studio 附带的 MSIL 查看器就已包含在内。
该工具名为
ildasm.exe
,位于默认 Visual Studio 后面的以下文件夹中安装:Visual Studio 2005
“
C:\Program Files (x86)\Microsoft Visual Studio 8\SDK\v2.0\Bin\ildasm.exe”
Visual Studio 2008
“
C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\ildasm.exe
”Visual Studio 2010
“
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\ildasm.exe
”有关详细信息,请参阅:“如何:查看程序集内容"。
I know this is an old question, and I'd prefer any of the tools above. However, in a pinch, there has been an MSIL viewer in the box with Visual Studio since at least Version 2005.
The tool is named
ildasm.exe
, and is located in the following folders after default Visual Studio installations:Visual Studio 2005
"
C:\Program Files (x86)\Microsoft Visual Studio 8\SDK\v2.0\Bin\ildasm.exe"
Visual Studio 2008
"
C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\ildasm.exe
"Visual Studio 2010
"
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\ildasm.exe
"For more information, see: "How to: View Assembly Contents" in the MSDN Library.
我刚刚花了几个小时寻找可以让我直接在 Visual Studio 中查看 IL 代码的最佳工具。
到目前为止,我找到的唯一解决方案是 Msiler https://visualstudiogallery.msdn.microsoft.com/60fc53d4 -e414-461b-a27c-3d5d2a53f637
它工作得很好!
否则,第二个最佳解决方案是 JetBrains dotPeek,但没有 Visual Studio 集成,说实话,它非常棒。
I have just spent a couple of hours searching for the best tool that could let me view the IL code directly inside Visual Studio.
The only solution I found so far is Msiler https://visualstudiogallery.msdn.microsoft.com/60fc53d4-e414-461b-a27c-3d5d2a53f637
it works quite well!
Otherwise the second best solution, but without visual studio integration, is JetBrains dotPeek, which is quite awesome to be honest.
根据我的经验,IL 相关知识的最佳来源是 Andrew Troelsen“Pro C# 和 .NET Platform”。从第 3 版开始,他有非常非常出色的章节(大约 50 页)介绍如何理解 IL,甚至编写自己的代码并使用 ILAsm。我利用这些信息来调查 .NET 世界中是否存在多重继承。您还可以尝试在 IL 中使用一些非常有趣的功能(例如,过滤仅存在于 VB 中但不存在于 C# 中的异常)。
我强烈建议阅读该章。
最终,.NET Reflector 成为研究程序集 IL 代码的企业标准,而 Richter 的书绝对是“必读”的东西。但是从上面提到的其他书籍中,您可以揭示真正有用的东西:)
是的,.NET 世界中的每个程序集都包含一些 IL 代码(与清单一起),可以通过 Reflector 或 ILDasm 查看这些代码。更重要的是,Reflector 可以向您展示 C# 和 VB 优化代码。这意味着任何人都可以查看程序集的源代码,这就是商业产品中使用混淆器的原因。
From my experience the best source of IL-related knowledge is Andrew Troelsen “Pro C# and .NET Platform”. Starting from 3rd edition he has really, really outstanding chapter (approx 50 pages) on how to understand IL and even write your own code and use ILAsm. I’ve employed that information to investigate whether multiple inheritance exists in .NET world. Also you could try to employ some very interesting features in IL (e.g. filtering of exceptions which only exists in VB but not in C#).
I highly recommend to read that chapter.
Eventually, .NET Reflector is an enterprise standard for investigating IL code of assemblies and Richter's book is definitely "must read" stuff. But from other books like mentioned above you could reveal really useful things :)
Yes, each assembly in .NET world holds some IL code (alongsite with manifest) which could be viewed thru Reflector or ILDasm. Even more, Reflector could show you C# and VB optimized code. This means that any person could view the source code of an assembly and that's why in commercial products obfuscators are used.
如果您想查看中间语言,请尝试下载 JustDecompile from Telerik (目前免费,但需要注册)。
拖入 DLL 并从顶部的下拉框中选择
IL
!I you want to view the intermediate language, Try downloading JustDecompile from Telerik (Which is currently free, requires a sign up though).
Drag in your DLL and choose
IL
from the drop down box at the top!现在还有另一种选择。您可以在 VS Code 中使用 此扩展使用 Roslyn 构建。 目前仅限于 .NET Core。
There is now another option. You can do this in VS Code with this extension built with Roslyn. This is currently limited to .NET Core.