ArticleDirectory
- With Xiaojing reading CLR via C # (01)-How does the code run on the. NET platform?
With Xiaojing reading CLR via C # (01)-. NET platform Code How to run it?
When I went to an interview with a famous e-commerce company a few days ago, I asked a lot about my. netProgramAt that time, many of the underlying problems were not answered, and I found that my knowledge was too rough. So I decided to study hard. Today we are looking at the code execution process under the. NET platform system. How does it run?
1.
Source codeCompile as a managed Module
To run a program in the. NET Framework, you must first compile the source code into a managed module. CLR is a runtime that can be used by multiple languages. Many of its features can be used in all development languages for it. Microsoft has developed compilers in multiple languages. during compilation, corresponding compilers are used for syntax check and Code Analyzer. After compilation, a hosted module is generated.
Hosting module?
The managed module is a standard Windows PE file that requires the CLR environment to execute, including IL and metadata, as well as the PE Header and CLR header.
- Il, also known as managed code, is the instruction generated by the compiler after compiling the source file. CLR will compile the CPU instruction at the local cost during runtime.
- Metadata is actually a collection of data tables used to describe the content defined and referenced in the managed module. VS can intelligently perceive the benefits of metadata descriptions.
- PE Header: standard Windows PE File Header, including the file type (such as GUI and Cui) and file creation time.
- CLR header: contains information about the managed module. For example, the CLR version number, the method for hosting the module entry point (main method), and the methoddef metadata.
2.
Managed modules are combined into an assembly
Generally, the compiler will generate an assembly for the generated managed module by default. CLR deals directly with Assembly. The Assembly contains one or more managed modules and logical combinations of resource files. The combination process is as follows:
Some managed modules are on the left. After some tools are processed, a PE file, that is, the Assembly, is generated. There is a manifest data block in the Assembly to describe all the files that constitute the assembly. In addition, the Assembly also contains information about the Assembly it references, which enables the Assembly to implement self-description. In this way, the CLR knows all the content required by the assembly, so the deployment of the Assembly is easier than that of the unmanaged component.
3.
EXE
Or DLL
File startup CLR
Runtime
To run the program, first check whether the. NET Framework is installed on the machine: Run. Enter % WINDIR %/system32 to check whether the mscoree. dll file exists in the target (Microsoft sets up the execution engine when the object is running ).
You can also use clrver.exe to view all CLR versions installed on the machine.
The process of loading and initializing CLR:
4.
Assembly execution
Il Code uses the Real-Time Compiler (JIT) to convert local CPU commands.
The first call process of the method?
- 1. When the program runs for the first time, it will call the jitcompiler function. It can know the methods that are called and the classes that define the method.
- 2. Then, the jitcompiler function searches for the location of the Il code in the metadata, and converts the local CPU command after verification. Save commands in dynamically allocated memory
- 3. Change the called method address to the memory address in step 1 of jitcompiler.
- 4. Jump to the code block and execute the code
- 5. return after execution is complete
Il is a stack-based language and has no type. One of the advantages of Il is to improve the robustness of the program. When converting the Il code to a local CPU command, CLR will execute the security verification process. If the verification fails, an exception will be thrown.
For example, we can see that sometimes the compiler can pass the test, but an exception will still be thrown during the runtime.
Call this method again?
In a program, we often call the same method repeatedly. When we call this method again, we do not need to repeat the verification. We can directly call the existing local code in the memory block, skip the verification and compilation process of the jitcompile function. Therefore, the same method may cause some performance loss only when it is called for the first time, and the subsequent call can be performed at full speed.
Close the program?
Because the compiler saves the local code in the dynamic memory, the local code will be lost when the program is closed. When you start the program again or run two instances of the program at the same time, the JIT compiler will compile the Il code as a local command again.
You may like:Reading CLR via C # (00) with Xiaojing-opening part and Directory