"Framework Design (version 2nd) CLR via C #" study note (001) -- compile the source code into a managed Module

Source: Internet
Author: User
Tags types of tables

Statement: This article is a reading note. Most of the content is copied from the book and used as your own learning. (* ^__ ^ *) Xi ......

In the past, when developing a system, we always had to decide what to use.Programming Language. This is a difficult task because different languages have different capabilities. However, since the emergence of. net, the problem has been solved. Let's take a look at the figure below.

The Common Language Runtime (CLR) is a Runtime Library that can be used by multiple programming languages. CLR features can be used by all languages targeting it. In fact, during runtime, CLR does not care about the language used by developers.Source code. That is to say, when selecting a programming language, we should select the easiest programming language based on the intent we want to express.

In fact, we can regard the compiler as a syntax checker andCodeAnalyzer. Microsoft has created several language editors for this Runtime Library, such as C ++/CLI, C #, Visual Basic, JScript, J #, and an intermediate language, il) assembler. In addition to Microsoft, some companies, schools, or enthusiasts have created their own compilers, which can also generate code for CLR.

The local code compiler generates specific code for the CPU architecture. Instead, all CLR compatible compilers generate "Intermediate Language (IL)" code. Il code is also called managed code.

In addition to generating Il, each CLR-oriented compiler also needs to generate complete metadata (metadata) in each managed module ). In short, metadata (metadata) is a series of special data tables that describe the content defined in the module, such as the type and its members. In addition, some metadata tables also indicate the content referenced by the hosting module, such as the import type and its members. Although we know that metadata is a superset of some old technologies. These old technologies include "Type Library" and "Interface Definition Language (IDL)" files. Note that CLR metadata is much more complete than they are. In addition, unlike the Type Library and IDL, metadata is always associated with files containing il code. In fact, metadata is always embedded in the same EXE/dll as the code, which makes the two inseparable. Since the compiler generates metadata and code at the same time and binds them to the ultimately generated managed module, the metadata and its description of the Il runtime will never be lost.

So what exactly does the hosting module contain? Let's take a look:

    •  Pe32 or pe32 + HeaderThe standard Windows PE file header is similar to the "common object file format (coff)" header. If this header uses the pe32 format, the file can be run on Windows 32-bit or 64-bit. If the header file is in pe32 + format, the file can only run on Windows 64-bit. This header also indicates the file type: Gui, Cui, or DLL, and contains a timestamp to indicate the file generation time. For modules that only contain il code, most of the information in the pe32 (+) header is ignored. For a module that contains the local CPU code, this header contains information related to the local CPU code.
    •  CLR HeaderContains information that makes this module A managed module (by Clr and practicalProgram). The header contains the required CLR version, some flags, and methoddef metadata tag of the managed module entry method (main method, the module metadata, resources, strong names, some signs, and locations/sizes of other less important data items.
    •  MetadataEach managed module contains metadata tables. There are two types of tables: one is to describe the types and Members defined in source code, and the other is to describe the types and members referenced in source code.
    •  Intermediate Language (IL) CodeThe code generated when the compiler compiles the source code. During running, CLR compiles the Il to the local CPU command cost.

Next, let's take a simple look at the purpose of metadata. Here we only list some of it.

    • During compilation, metadata eliminates the need for both the header and the library file, because all information related to the referenced type/member is included in the file where Il is used to implement the type/member. The compiler can read metadata directly from the managed module.
    • Microsoft Visual Studio uses metadata to help us write code. Its "intelligent perception" feature parses metadata and specifies the methods, attributes, events, and fields that a type provides. For a method, you can also specify the parameters required by the method.
    • The code verification process of CLR uses metadata to ensure that the Code only performs "safe" operations.
    • Metadata allows you to serialize an object field to one memory, send it to another machine, deserialize it, and recreate the object state on a remote machine.
    • Metadata allows the Garbage Collector to track the lifetime of an object. The garbage collector can determine the type of any object and know which fields in the object reference other objects based on the metadata.

 

Related Article

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.