The underlying operating mechanism clr-c#

Source: Internet
Author: User

Execution model of the CLR (C #)

Process:

1. Compile the source code into the delegate code-->2. Merge delegate code into assembly-->3. Execute assembly code-->4. When the common language is loaded

Note: The CLR (Common Language Common language) CLR is a "runtime" that can be used by multiple programming languages. The core capabilities of the CLR (e.g. memory management, assembly loading, security, exception handling, thread synchronization, and so on) can be used by all languages that target the CLR.

1. Compile the source code into a delegate module:

The CLR doesn't really care about developers using that language to write the source code, which means that when we write C # code, we certainly have some steps to follow the CLR, so we need a corresponding CLR-oriented compiler that can compile C # code so that the CLR can recognize what you write. The compiler checks the syntax and parses the source code, resulting in a managed module


Note:

A managed module is a PE (Portal executable) file that can be executed in the CLR.

The book describes managed modules by PE32 or pe32+ headers, CLR headers, metadata, IL (intermediate language) code. When you look at it, it's important to understand that metadata and IL are better.

metadata : Contains two types of metadata tables: One table describes the types and members defined in the source code, and the other table describes the types and members of the source code references .

IL ( intermediate language) Code : code generated when the compiler compiles source code. At run time, the CLR compiles il to the cost of the CPU instruction. (IL code is sometimes called managed code because the CLR wants to manage its execution)

2. Merge managed code into an assembly:

(The CLR does not actually work with modules.) Instead, he works with the assembly.)

As mentioned earlier, the CLR does not recognize your specific language and requires the appropriate compiler to generate the appropriate managed modules. This would say that the actual not working with the module introduces the concept of assembly. The article says that the Assembly is an abstract concept.

Managed modules (IL and metadata), resource files (. jepg,.gif,.html, and so on)-tools that combine multiple managed modules and resource files into assemblies (C # compiler CSC.exe)-Assembly (manifest: A set of files in a description assembly)

3. Load the common language runtime:

The assembly that you build can be either an executable application or a DLL that contains a set of types that can be used by the executing program. Ultimately, the CLR manages the execution of code in these assemblies.

Loading the common language for the moment, we can consider initializing the CLR after a series of windows, then loading the EXE assembly and calling its entry method main (). The managed application will then start and run.

4. Execute assembly code:

In the first step, when the source code is compiled into a managed module, it is mentioned that the metadata is always associated with a file containing IL code, because the compiler generates metadata and code at the same time, binds them together, and embeds the resulting managed module, so the metadata and the IL code he describes are not used to lose synchronization

As you can imagine, when you call the entry method (Main), the metadata comes in, and they're a good pair of friends.

The underlying operating mechanism clr-c#

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.