问题描述
我正在尝试针对 .NetCore3.1 控制台应用运行作为 Visual Studio 2019 一部分安装的 ildasm(中级语言反汇编程序).
I'm trying to run ildasm (Intermediate Language Disassembler) installed as part of Visual Studio 2019 against .NetCore3.1 console app.
使用 Visual Studio 2019 的开发人员命令提示符并运行以下命令
Use Developer command prompt of Visual Studio 2019 and run following command
ildasm.exeD:DotNetIntroductionToCsharpIntroductionToCsharpinDebugetcoreapp3.1IntroductionToCsharpinDebugetcoreapp3.1IntroductionToCsharp.exe
但是出现如下错误:
'D:DotNetIntroductionToCsharpIntroductionToCsharpinDebugetcoreapp3.1 IntroductionToCsharp.exe' 没有有效的 CLR 头,无法反汇编
注意:在非 Core (.Net Framework 4.x) exe 上运行相同的命令可以正常工作.也许在 .Net Core 中查看 IL 需要一些特殊的东西?
Note: running the same command on non-Core (.Net Framework 4.x) exe works fine. Maybe there is something special needed to look at IL in .Net Core?
推荐答案
正如已经指出的那样,.NET Core 中的 .exe
只不过是一个宿主.它的唯一目的是加载 CoreCLR,然后 JIT 编译并执行程序集的 Main 方法.代码本身存储在 .dll
中.您可以使用您最喜欢的反汇编程序(ILDASM、JetBrains 的 dotPeek、ILSpy 等)检查这一点.
As it was pointed out already, the .exe
in .NET Core is nothing more than a host. It's sole purpose is to get the CoreCLR loaded then JIT compile and execute your assembly's Main method. The code itself is stored within the .dll
. You can check this with your favorite disassembler (ILDASM, JetBrains' dotPeek, ILSpy, etc).
为什么 IL 反汇编器可以针对 .NET Framework 中的 .exe
工作?因为可执行文件将是一个托管程序集,具有相应的托管标头.与 .NET Core 不同,.NET Framework 得到了 Windows 操作系统的 Image Loader 的广泛支持,可以设置所有内容,然后在可执行文件中运行托管代码.参见讨论的第二点 此处.
Why could an IL disassembler work against an .exe
in .NET Framework ? Because the executable would be a managed assembly, with a corresponding managed header. Unlike .NET Core, .NET Framework has extensive support from the Windows operating system's Image Loader to get everything set up and then run the managed code within the executable. See the second point discussed here.
这篇关于如何在 .Net core exe/assembly 上运行 ildasm?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!