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

前端 未结 13 996
南方客
南方客 2020-11-28 18:53

I\'m new to .NET C# programming. I\'m following few books. It is said that instead of compiling it directly to binary code (Native code). High level code is converted into i

相关标签:
13条回答
  • 2020-11-28 19:44

    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

    0 讨论(0)
  • 2020-11-28 19:50
    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.

    0 讨论(0)
  • 2020-11-28 19:50

    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.

    0 讨论(0)
  • 2020-11-28 19:50

    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.
    0 讨论(0)
  • 2020-11-28 19:55

    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.

    0 讨论(0)
  • 2020-11-28 19:56

    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.

    0 讨论(0)
提交回复
热议问题