What is the IL code and where can I find this code?

Asked

Viewed 2,090 times

13

In my question about static builders the user Maniero showed how is the code generated from a static constructor by . NET, this code is called Code IL according to his reply.

See the IL code of my question:

.class private auto ansi '<Module>'
{
} // end of class <Module>

.class public auto ansi beforefieldinit Program
    extends [mscorlib]System.Object
{
    // Methods
    .method public hidebysig static 
        void Main () cil managed 
    {
        // Method begins at RVA 0x2050
        // Code size 32 (0x20)
        .maxstack 2
        .locals init (
            [0] class MinhaClasse
        )

        IL_0000: nop
        IL_0001: newobj instance void MinhaClasse::.ctor()
        IL_0006: stloc.0
        IL_0007: ldloc.0
        IL_0008: ldstr "Hello Stackoverflow"
        IL_000d: callvirt instance void MinhaClasse::set_Propriedade(string)
        IL_0012: nop
        IL_0013: ldloc.0
        IL_0014: callvirt instance string MinhaClasse::get_Propriedade()
        IL_0019: call void [mscorlib]System.Console::WriteLine(string)
        IL_001e: nop
        IL_001f: ret
    } // end of method Program::Main

    .method public hidebysig specialname rtspecialname 
        instance void .ctor () cil managed 
    {
        // Method begins at RVA 0x207c
        // Code size 8 (0x8)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: call instance void [mscorlib]System.Object::.ctor()
        IL_0006: nop
        IL_0007: ret
    } // end of method Program::.ctor

} // end of class Program

.class public auto ansi MinhaClasse
    extends [mscorlib]System.Object
{
    // Fields
    .field private string '<Propriedade>k__BackingField'
    .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
        01 00 00 00
    )
    .custom instance void [mscorlib]System.Diagnostics.DebuggerBrowsableAttribute::.ctor(valuetype [mscorlib]System.Diagnostics.DebuggerBrowsableState) = (
        01 00 00 00 00 00 00 00
    )

    // Methods
    .method public hidebysig specialname 
        instance string get_Propriedade () cil managed 
    {
        .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
            01 00 00 00
        )
        // Method begins at RVA 0x2085
        // Code size 7 (0x7)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: ldfld string MinhaClasse::'<Propriedade>k__BackingField'
        IL_0006: ret
    } // end of method MinhaClasse::get_Propriedade

    .method public hidebysig specialname 
        instance void set_Propriedade (
            string 'value'
        ) cil managed 
    {
        .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilerGeneratedAttribute::.ctor() = (
            01 00 00 00
        )
        // Method begins at RVA 0x208d
        // Code size 8 (0x8)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: ldarg.1
        IL_0002: stfld string MinhaClasse::'<Propriedade>k__BackingField'
        IL_0007: ret
    } // end of method MinhaClasse::set_Propriedade

    .method private hidebysig specialname rtspecialname static  
        void .cctor () cil managed   //<============== construtor estático aqui
    {
        // Method begins at RVA 0x2096
        // Code size 2 (0x2)
        .maxstack 8

        IL_0000: nop
        IL_0001: ret
    } // end of method MinhaClasse::.cctor

    .method public hidebysig specialname rtspecialname 
        instance void .ctor () cil managed   //<============== construtor de instância aqui
    {
        // Method begins at RVA 0x207c
        // Code size 8 (0x8)
        .maxstack 8

        IL_0000: ldarg.0
        IL_0001: call instance void [mscorlib]System.Object::.ctor()
        IL_0006: nop
        IL_0007: ret
    } // end of method MinhaClasse::.ctor

    // Properties
    .property instance string Propriedade()
    {
        .get instance string MinhaClasse::get_Propriedade()
        .set instance void MinhaClasse::set_Propriedade(string)
    }

} // end of class MinhaClasse

Which is the code corresponding to the class I used as an example in my question.


Doubts

What is this Code IL and how to see the IL code of my software that I develop in Visual Studio in C#?

  • Check this question, it may be useful http://answall.com/q/118694/18246

2 answers

11


IL is the Intermediate Language, i.e., it is an intermediate language generated by compilers who intend to run on top of the CLR and conform to the CLI. This code is a kind of assembly language. The code runs on a platform that is a virtual machine that simulates a processor that will run the bytecode generated by this Assembly. The official name is Common Intermediate Language, or CIL, and some like to call it MSIL because it was created by Microsoft.

It would even be possible for this virtual machine to perform this bytecode, but what actually happens is that it is converted to native code from the actual processor being used before it is used. This conversion may occur before running, on application startup, early on installation, or more recently it may even be generated like this in the application creation process.

The specification of all this can be downloaded.

The . NET itself has a decompiler called Ildasm. Mono owns the Cecil. You can get the IL code even on .NET Fiddle or Sharplab. There are several other free and paid, some good, others not so much. Some examples:

  • 1

    Geez, bigown... I’m trying to compete, but you’re a machine for figuring out answers. Good job!

  • I have heard that a code already written in IL is much faster than a code in C#. So, is it advantageous to convert a code to the IL? Like when we decrease Csss and Jss codes to gain faster.

  • This is generally not true, of course there are patterns that you can write something that runs faster with enormous effort. Even in Assembly this is not usually true anymore in many situations, imagine in IL that is of higher level.

6

The IL code Maniero refers to is the Common Intermediate Language, an intermediate language equivalent to Assembly in compiled programming languages.

Since C# is an interpreted language the Microsoft compiler generates the intermediate code in such a way that for the virtual machine that runs it is as optimized as binary code (not always true, but this is the idea).

The IL code can be obtained by running a kind of "disassembler" to exes compiled in . NET, such as Ilspy.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.