How to run ildasm on .Net core exe/assembly?

不羁岁月 提交于 2020-05-29 14:49:58

问题


I'm trying to run ildasm (Intermediate Language Disassembler) installed as part of Visual Studio 2019 against .NetCore3.1 console app.

Use Developer command prompt of Visual Studio 2019 and run following command

ildasm.exe D:\DotNet\IntroductionToCsharp\IntroductionToCsharp\bin\Debug\netcoreapp3.1\IntroductionToCsharp\bin\Debug\netcoreapp3.1\IntroductionToCsharp.exe

But following error occur:

'D:\DotNet\IntroductionToCsharp\IntroductionToCsharp\bin\Debug\netcoreapp3.1\ IntroductionToCsharp.exe' has no valid CLR header and cannot be disassembled

this folder content

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?


回答1:


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).

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.




回答2:


NOTE: EXE in netcore exist just for "windows users", you should use always dll

See here

You can try this steps for netcore assembly (dll):

1 You need to have coreclr in the same folder of you dll netcore

2 (run commands in commandline VS)

ildasm.exe /all /out:<path to output>.il <path to dll>.dll



回答3:


You should point ildasm to your assembly file (.dll or .exe) not your project folder for ex.

ldasm.exe D:\DotNet\IntroductionToCsharp\IntroductionToCsharp\bin\Debug\netcoreapp3.1\IntroductionToCsharp.exe


来源:https://stackoverflow.com/questions/59818364/how-to-run-ildasm-on-net-core-exe-assembly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!