How can I view MSIL / CIL generated by C# compiler? Why is it called assembly?

こ雲淡風輕ζ 提交于 2019-11-27 02:39:06
  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.

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.

If you want it online, .NET Fiddle is excellent. Just paste your code and click View IL option at the top right.

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

Another option: Use ReSharper

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

John Saunders

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.

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.

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.

Eric Burcham

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.

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.

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!

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

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.

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