. NET is how to compile the source code into managed code, and how to merge managed code into an assembly? First, we first understand the common CLR,. NET Framework,il and other nouns.
One, talk about common nouns.
The CLR (Common language runtime, Common Language Runtime), as well as a Java virtual machine, is a runtime environment that can be used by a variety of programming languages. The core features of the CLR include memory management, assembly loading, security, exception handling, and thread synchronization, which can be used by all languages that target the CLR. and ensure the necessary separation between the application and the underlying operating system. The CLR is the primary execution engine for the. NET Framework.
IL (Intermediate language): The code generated when the compiler compiles the source code.
The Framework class library is included in the. NET Framework (framework classes LIBRARY,FCL). The FCL is a general term for a set of DLL assemblies that contains thousands of type definitions and functions.
Second, how to compile the source code into managed code
Compiling the source code into a managed module is the process by which the local code compiler compiles the Il (intermediate code) based on the machine's CPU architecture (such as x86,x64 or IA64). Il is managed code and requires the CLR to manage its execution. Therefore, it is necessary to install the CLR,CLR on the machine currently as part of the. NET Framework. Of course, in addition to generating IL during compilation, the compiler also needs to generate complete metadata in the managed module.
Note: We can interpret the compiler as a grammar checker, which is responsible for checking the source code, ensuring that the code we write is legitimate and meaningful, and that the CLR doesn't care what programming language the developer uses, so different companies can target the CLR with their own language and compilers. We can create source code files in any language that supports the CLR, and then check the syntax and parse the source code with the corresponding compiler. The CLR outputs the code that describes your intentions.
Whichever compiler, the result is a managed module (managed module). A managed module is a standard 32-bit Microsoft Windows Portable Execution Body (PE32) file, or a standard 64-bit Windows portable execution Body (pe32+) file that requires the CLR to execute. By the way, managed assemblies always take advantage of Windows Data Execution Protection (execution PREVENTION,DEP) and address space layout randomization (addr space layout randomization ASLR). These two features are designed to enhance the security of the entire system. Specify the following:
(1) PE32 or pe32+ header: A standard Windows PE file header, similar to the "Common Object File Format" (Common object document Format,coff) header. If the header is in PE32 format, the file can run on either the 32-bit or 64-bit version of Windows. If these headers are in pe32+ format, the files can only be run on the 64-bit version of Windows. This header also identifies the file type, including the Gui,cui latter DLL, and contains a time stamp to indicate when the file was generated. For modules that contain only IL code, most of the information in the PE32 (+) header is ignored. For modules that contain local CPU code, this header contains information about the local CPU code.
(2) CLR header: Contains information that makes this module a managed module, which can be interpreted by the CLR and some utilities. The header contains the MethodDef metadata token (token) that requires a CLR version, some flags, a managed module entry method (the Main method), and the location/size of the module's metadata, resources, strong names, some flags, and other less important data items.
(3) Meta data: Each managed module contains a metadata table. There are two main types of tables: one type of table describes the types and members defined in the source code, and the other type of table describes the types and members that the source code applies to. Because the compiler generates metadata and code at the same time, binds them together and embeds the resulting managed module, the metadata and the IL code it describes will never lose synchronization. Metadata enables the metadata to eliminate the need for local C + + header and library files, while IntelliSense technology can help us write code because it indicates that a type provides information such as the method attribute field events, metadata allows the garbage collector to track the lifetime of objects and facilitates garbage collection ; metadata ensures that only type-safe operations are performed; metadata is able to serialize the field of one object into memory and send it to other machines for deserialization.
(4) IL (intermediate Language) code: the code generated when the compiler compiles the source code. At run time, the CLR compiles il to the cost of the CPU instruction.
Third, if managed code is merged into an assembly
The CLR is typically working with assemblies, not modules. An assembly is a logical grouping of one or more module/resource files. Assemblies are reuse, security, and minimum version control units. You can generate either single-file or multiple-file assemblies. An assembly is equivalent to a "component." Some managed modules and resource (or data) files are prepared to be processed by a tool. The tool generates a separate PE32 (+) file to represent the logical grouping of files. What actually happens is that the PE32 (+) file contains a block of data called "manifest". A manifest is another collection of metadata tables. These tables describe the files that make up the assembly, the types of publicly exported files implemented in the Assembly, and the resources or data files that are associated with the assembly.
The source code is compiled into managed code, and managed code is merged into an assembly;