How does running an . NET application work?

Asked

Viewed 847 times

18

There are several terms I always hear/read when I see talk about running. NET applications, such as MSIL, CIL, IL, JIT, Jiter, Roslyn, CLR, among others I must have forgotten.

I would like a brief explanation of how the execution of a . NET application works, along with a short explanation of those names I quoted.

If it makes a difference, I prefer examples in C# and, if possible, that the examples are based on this code (this to maintain a pattern between the answers).

using System;
public static void Main(string[] args)
{
     string nome = "Joaquim da Silva";
     Console.WriteLine(nome);
}  

2 answers

13


The .NET Compiler Platform (formerly called Roslyn) is used as the basis of the C# or VB.NET code compiler. This can be understood in the question What is a programming language, IDE and compiler?.

Once compiled, the language itself no longer matters. Everything becomes a "machine code".

After the compilation process of the source code, an executable is generated (in the background a container, is not a common executable like those natively generated by compiler like C++, for example) with IL code (Intermediate Language) which is binary code that we could call machine code of the platform CLR (Commom Language Runtime) and with metadata.

CIL is the "official" name of this intermediate code (Common Intermediate Language). MSIL is what some call it. MS is obviously from Microsoft, since initially the expectation was that this would be a technology owned by this company.

One of the components of the CLR is the Jitter. We could call it the JIT Compiler (although the "er" can be explained by the English grammar indicating that it is an agent executing some action). JIT means Just-In-Time, or "when you need it". This means it will be invoked when the code is executed. His role is to transform this internal machine code from this platform (the .NET, Mono, etc.) to the machine code of the physical platform where the compiled software is running.

After this process, the code roughly rotates as if it were native from the beginning. Jitter brings the advantage of always being able to optimize the execution with each execution, but it takes longer to initialize the software and requires a more complete and heavy environment to follow the execution.

This environment is implemented by VES (Virtual Environment System).

All this is defined by the standard of CLI (Common Language Infraestructure).

That answer might help a little.

The names and versions, which is what, are a little confusing.

The .NET 5 a little changed the relationship between all these things.

Obviously many doubts can still persist and I suggest new more specific questions at each point that deserves something dedicated.

The above code is compiled for CIL code (shown so for a human to view and not for the computer):

// Metadata version: v4.0.30319
.assembly extern mscorlib
{
  .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .z\V.4..
  .ver 4:0:0:0
}
.assembly '41188d85-05c0-4a3b-bbaf-cfc8464a6216'
{
  .hash algorithm 0x00008004
  .ver 0:0:0:0
}
.module '41188d85-05c0-4a3b-bbaf-cfc8464a6216.dll'
// MVID: {A36CB87C-35BA-4424-83CC-9E8C29792DDE}
.imagebase 0x10000000
.file alignment 0x00000200
.stackreserve 0x00100000
.subsystem 0x0003       // WINDOWS_CUI
.corflags 0x00000001    //  ILONLY
// Image base: 0x00BD0000


// =============== CLASS MEMBERS DECLARATION ===================

.class public auto ansi beforefieldinit Exemplo
       extends [mscorlib]System.Object
{
  .method public hidebysig static void  Main() cil managed
  {
    // 
    .maxstack  1
    .locals init (string V_0)
    IL_0000:  nop
    IL_0001:  ldstr      "Joaquim da Silva"
    IL_0006:  stloc.0
    IL_0007:  ldloc.0
    IL_0008:  call       void [mscorlib]System.Console::WriteLine(string)
    IL_000d:  nop
    IL_000e:  ret
  } // end of method Exemplo::Main

  .method public hidebysig specialname rtspecialname 
          instance void  .ctor() cil managed
  {
    // 
    .maxstack  8
    IL_0000:  ldarg.0
    IL_0001:  call       instance void [mscorlib]System.Object::.ctor()
    IL_0006:  nop
    IL_0007:  ret
  } // end of method Exemplo::.ctor

} // end of class Exemplo

This is a way of visualizing the code humanely, in the executable there are only a few bytes that do not make sense to us, there is this organization and comments like this.

  • Which tool you used to generate this CIL? I’m using the Linqpad, but by the way it just generates the other type.

  • The IL Disassembler that comes along with the . Net.

  • Don’t forget the no less important C++/CLI, which has a strong influence on the development of interfaces between codes, such as Java running calls in modules written in Csharp.

5

The managed execution process includes the following steps, which are discussed in detail later in this topic:

Choosing a compiler.

To get the advantages provided by the common language Runtime, you must use one or more language compilers aimed at Runtime.

Compile your code for MSIL.

Build converts your source code into Microsoft Intermediate language (MSIL) and generates the required metadata.

MSIL compilation for native code.

At the time of execution, a just-in-time (JIT) translates MSIL into native code. During this build, the code must pass a verification process that examines the MSIL and metadata to find out if the code can be determined to be heavily typed.

Running code.

The common language Runtime provides the infrastructure that allows execution to take place and services that can be used during execution.

https://msdn.microsoft.com/pt-br/library/k5532s8a%28v=vs.110%29.aspx

Browser other questions tagged

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