如何查看C#编译器生成的MSIL / CIL?为什么叫组装呢?

发布于 2024-09-11 06:10:37 字数 287 浏览 5 评论 0原文

我是 .NET C# 编程新手。我正在关注几本书。据说,不是将 C# 代码直接编译为机器代码,而是将其转换为中间语言(称为 MSIL 又名 CIL)。但是当我编译时,我得到一个 exe/dll 文件。

  1. 这些 exe/dll 文件中是否包含此 MSIL/CIL?
  2. 我想查看中间语言代码,只是为了感受它的存在。我该如何查看?
  3. 他们将此 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.

  1. Is this MSIL/CIL contained in these exe/dll files?
  2. I want to see that intermediate language code, just to get feel for its existence. How do I view it?
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

沦落红尘 2024-09-18 06:10:37
  1. 是的,更准确地说是在 PE 文件.text 部分>(可移植可执行文件 = *.exe 或 *.dll)。更多信息可以在此处找到。
  2. 最好的选择是使用 ILSpy (Reflector 不再免费)。它是一个免费的反汇编程序,可以将程序集反汇编为 MSIL,也可以反汇编为 C#、VB(在某种程度上)。 .NET Framework SDK 包含 ILDasm,它是官方的 MSIL 反汇编器。
  3. 基本上是的。程序集是包含 MSIL 代码和相应元数据的文件。这并不限于 PE 文件本身,所有当前的 CLR 实现都使用它们。

如果我也可以推荐一本关于这个问题的好书,那就是 Serge Lidin 编写的 Expert .NET 2.0 IL Assembler。他是 MSIL 的设计者。

  1. Yes it is, more exactly in the .text section of the PE file (portable executable = *.exe or *.dll). More information can be found here.
  2. The best choice is to use ILSpy (Reflector is no longer free). It's a free disassembler that can dissassemble your assembly into MSIL but also C#, VB (to some extent). The .NET Framework SDK contains ILDasm, which is the official MSIL dissasembler.
  3. Basically yes. An assembly is a file that contains MSIL code and corresponding metadata. This is not restricted to PE files per se, but all current CLR implementations use them.

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.

风柔一江水 2024-09-18 06:10:37

我最喜欢的查看 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.

不念旧人 2024-09-18 06:10:37

如果您想在线使用,.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.

薄荷港 2024-09-18 06:10:37

另一种选择:使用 ReSharper

在此处输入图像描述
源/IL 同步视图:左侧蓝色背景线与右侧 IL 块相对应

在 Visual Studio 中:

  • 选择 ReSharper |窗户| IL 查看器
  • 或上下文菜单:导航 | IL 代码

支持源代码和 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

enter image description here
Source / IL synced view: left blue background line corresponds with right IL Block

In Visual Studio:

  • Choose ReSharper | Windows | IL Viewer
  • or Context Menu: Navigate | IL Code

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

匿名的好友 2024-09-18 06:10:37

Sharplab
sharplab 是一个在线工具,非常适合简单的用例。在左侧输入代码,IL 将显示在右侧。

Sharplab
sharplab is an online tool, great for simple use cases. Type your code on the left, IL shows up on the right.

腻橙味 2024-09-18 06:10:37

我相信它们被称为“程序集”,因为程序集是一组模块,通过清单组装在一起。

multi-文件汇编
(来源: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.

multi-file assembly
(source: microsoft.com)

See Assembly Contents for details.

惟欲睡 2024-09-18 06:10:37
  1. 是的,它正在组装中。
  2. 您需要 .NET ReflectorILDasm
  3. 有关程序集的更多详细信息,请查看此处

PS 由于您正在阅读一些书籍,我强烈推荐您通过 C# 进行 CLR

  1. Yes it is in assembly.
  2. You need .NET Reflector or ILDasm.
  3. More details on assembly check HERE.

P.S As you are following some books I will highly recommend you CLR via C#.

信愁 2024-09-18 06:10:37

在许多方面,.NET 程序集与 Java 字节码包类似。

  1. 是的。它们还包含清单和其他数据,但 CIL 是 exe/dll 的一部分。
  2. 使用 ILDasmReflector - 大多数人会说 Reflector,因为它功能更强大。两者都会向您展示 CIL 的生成情况。维基百科有一个所有 CIL 指令的列表,以获得更好的感觉(它类似于汇编)。
  3. 我猜它的意思是代码的汇编。这是区分它与原生的好方法。

In many respects, .NET assemblies are similar to Java bytecode packages.

  1. Yes. They also contain manifests and other data, but the CIL is part of the exe/dll.
  2. Use ILDasm or Reflector - most people would say Reflector, as it is much more powerful. Both will show you what CIL was produced. Wikipedia has a list of all CIL instructions, for a better feel (it is assembly like).
  3. I guess it is meant as an assembly of code. A good way to differentiate it from native.
瞳孔里扚悲伤 2024-09-18 06:10:37

我知道这是一个老问题,我更喜欢上面的任何工具。不过,在紧要关头,至少从 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.

执妄 2024-09-18 06:10:37

我刚刚花了几个小时寻找可以让我直接在 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.

神经暖 2024-09-18 06:10:37

根据我的经验,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.

梦太阳 2024-09-18 06:10:37

如果您想查看中间语言,请尝试下载 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!

沉睡月亮 2024-09-18 06:10:37

现在还有另一种选择。您可以在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文