Introduction to CLR via C # knowledge

Source: Internet
Author: User

 

I believe that many developers who have experience in the interview are asked about the. NET underlying things during the interview, suchProgramHow is it running at the underlying layer? I have met the interviewer's question about the reflection mechanism? The answer was a mess, but I was lucky enough to find a good job. Waking up at work is a time (with a certain degree of Development Foundation. net Operating Mechanism. After buying a book about CLR via C # and carefully tasting the first chapter, I decided to share what I learned with you, although I have known the knowledge before, I still learned a lot. There are still some mistakes in the summary process. I hope you can point them out, and I will correct them as soon as possible. Thank you! (Currently, only the first half of Chapter 1 is summarized, and the second half of the documents have not been sorted out yet. Hope you can understand them)

Let's get down to the truth and start with what the CLR is. Common Language Runtime (CLR) is a common language runtime that can be used by multiple languages at the same time. Many features can be used in all development languages targeting it. The compiler checks the syntax andCodeAnalysis, and then generate a managed module.

Fig 1.1

Now we need to understand another concept. What is a managed module?

The managed module is a standard 32-bit Microsoft Windows Executable body (pe32) file or a standard 64-bit Microsoft Windows Executable body (pe32 +) file, one of the above two types adds the CLR header information (including some information that makes the module A managed module, such as the CLR version, the method entry point of the hosting module (the main method we call) plus metadata (the metadata here is not ours.Source codeA file is a metadata table contained in each managed module. It is a type table, a source code reference type, and a Member table) add the Il code (that is, the intermediate language code compiled by the compiler of each language). At the end of the execution, the CLR controls the execution of the Il code.

You may think that CLR works with the hosting module? Actually not. CLR only works with assembly. What is assembly?

An assembly is a logical grouping of one or more resource files and modules. The reuse and Version Control of an assembly in the entire program are the smallest units. How is an Assembly formed? After we compile the source code file into a hosted module (intermediate language and metadata) by using the compiler in various languages through the process shown in Figure 1.1, in addition, some jpge, GIF, and other resource files required by our program are handed over to the tool that generates the Assembly for processing and then become the Assembly that deals with CLR (by default, the tool used to generate an assembly is completed by the compiler, but not necessarily by the compiler)

So how can we determine whether the. NET Framework is installed on our machine? Enter % WINDIR %/system32 in the running directory to check whether the mscoree. dll file exists.

After talking about this, let's see how CLR makes the code we write run.

After we finish writing the source code file, compile and run the program. (Here we only use C # as an example. I mainly develop C, I will study it in my spare time.) So how did our program run during compilation and running? No matter what language the source file is written in C #, VB, or J #, our language will eventually be compiled into the Il intermediate language through the corresponding compiler, if we need to execute the following code:

Fig 1.2

First, compile our main method into the Il intermediate language, and then convert the local CPU commands (this step is done by JIT (just-in-time or "instant ") completed by the compiler ). Before executing the main method, CLR checks all types of the code of the main method, generates a Data Structure table, and records the type and parameter reference of the method, this allows you to access information of the reference type. At this time, all methods in the console class have been recorded in the data structure table generated by Clr and the memory address of each method is available. However, in the first call to the console. during the writeline method, the JIT compiler knows which method to call, and then JIT compiles il into a CPU command. However, this CPU command is allocated in a dynamic memory, so that the compiled CPU command can be called directly during the second call, saving the trouble of re-compiling. Note: Re-compilation refers to the direct running of the console. writeline method when the application is running. If the program exits, the method will be checked and compiled again.

Fig 1.3

Repeat the following method call process:

1. When the program runs for the first time, it will call the JIT compiler. It can know the methods called and find the classes and implementations that define the method through the data structure table generated by CLR;

2. Then, the JIT compiler finds the Il code of the method in the metadata, verifies the code, and converts it into a CPU command. And allocated in the dynamic memory;

3. the JIT compiler changes the called method address to the memory address in step 2;

4. Jump to the Code Compiled by the main method.

5. The execution is complete and return

When the program is executed for the second time, it does not need JIT verification, but directly calls the local CPU command.

The above are the documents I have prepared in the first half of chapter. In the second half, I will try it out and share it with you. If you have any shortcomings, I will immediately correct them.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.