Understand the CLR principle in .NET (1)

zhaozj2021-02-16  47

Understand the CLR principle in .NET

Author: yarshray saga join (To reprint please include the author, thank you!)

First of all, the purpose is to let everyone understand, CLR is. The role in NET is important, if you want to master it. Net then understands the principle of CLR operation is an inevitable thing. For example, you want to realize dynamic acquisition information, dynamic creation, post-binding, reflection characteristics in the program, then the correct understanding of the principle of CLR is particularly important.

Let me make me an overall introduction to help you understand some of the exciting features in the CLR. Understanding these features will better help you understand CLR.

* Nothing code --- msil (intermediate language)

* Let us use the same language - CLR (time time when public language)

* Part of our hands - Assembly (assembly)

* Let us run in the same system - CTS (General Type System)

* Products after the universe --- metadata (metadata)

* Let our language can communicate --- CLS (public language system)

* Interaction in dynamics - Reflection (reflection)

* Belongs to our own space --- Namespace (Name Space)

The purpose of this article strives to be streamlined, it is streptching, so that I have to save the details, but in order to fully and complete, I will put it in the text, so that readers can be deeply in-depth. To understanding. (Note: The connection is part of MSDN, so you have to install .NET merge MSDN to view.)

vocabulary:

Because I want to be abbreviated, I will use the original word directly. But in order to help understand, I will show here:

MSIL: Microsoft Intermediate Language

Reflection: Reflection

Metadata: metadata

PE: can perform a portable file

AskEMBLY: assembly (assembly)

Namespace: Name Space

CTS: Universal Type System

GARBAGE COLECTION: Unwanted unit recycling

CLR: Public language system

Attribute: Attributes (Note Don't confuse with Property)

Boxing: Packing

UNBOXING: Unboxing

Text: Let us enter the whole text

* Nothing code --- msil (intermediate language)

1. Talk about MSIL

legend:

Platform has no local code

MSIL

use. Code written by Net supported language

Jit

Compile

MSIL (Microsoft Intermediate Language) Microsoft's intermediate language. Similar to Java virtual machines, it is an instruction set that is not related to the CPU. When compiling as a managed code, the compiler translates the source code into MSIL,

As shown in FIG. MSIL includes instructions for loading, storage, and initializing objects, and object call methods, as well as instructions for arithmetic and logical operations, control flow, direct memory access, exception processing, and other operations. Before you can execute the code, MSIL must be converted to a CPU-specific code, which is usually done through the real-time (JIT) compiler. Since each computer structure that is supported by the public language runtime provides one or more JIT compilers, it can be compiled and executed on any supported structure. This summarizes the above is: the intermediate language is a set of instructions independent of the CPU, which can be translated into a local code of the target platform by instant compiler Jitter. 2. Talk about PE

Windows PE and one. NET PE The main difference is that Windows PE is performed by the operating system, and .NET PE is converted to the CLR of .NET Framework. Identify a PE is. NET or Windows depends on his universal target file Whether the format (COFF) uses Windows operating system. Target file format (COFF) specifies that any files are divided into two parts: the file data itself, and the header file string of the data content included in the description file.

The MSIL assembler generates portable (PE) files from the MSIL assembly language. You can run a result executable (this file contains MSIL and the desired metadata) to determine if the MSIL is executed in expected. This is why I talk about PE.

. PE reference in NET:

http://www.9cbs.net/develop/Article/13\13683.shtm

. PE in NET

.NET PE File Format

l coff and pehaders

l C code and data sections

l metadata Tables and il Organization

l Java class file format vs. .NET PE File for

So how did the PE file do? Here is a typical .NET application execution process:

The user executes the application (PE file) output by the compiler, the operating system loads the PE file, and other DLL (.NET dynamic connection libraries).

The operating system loader jumps to the program's entry point according to the executable file header in the PE file. Obviously, the operating system cannot perform an intermediate language, which is also designed to jump to the _ corexemain () function entry to Mscoree.dll (core support DLL of .NET platform).

The Corexemain () function starts executing the intermediate language code in the PE file. The execution here is that the CRL (General Language Runtime) is in units of calls, and the intermediate language is compiled into the machine binary code, execute and exactly the machine cache as needed.

During the execution of the program, the GC (garbage collector) is responsible for the distribution, release of the memory, etc.

The program is executed, and the operating system uninstall the application.

3. Tool reference:

The MSIL disassembler is a partner tool for the MSIL assembler (ILASM.exe). ILDASM.EXE uses a portable executable (PE) file containing Microsoft Intermediate Language (MSIL) code and creates the corresponding text file as the ILASM.EXE input.

MS-Help: //ms.vscc/ms.msdnvs.2052/cptools/html/cpconmsildisassemblerildasmexe.htm

* Let us use the same language - CLR (time time when public language)

1. The characteristics of CLR:

1. Inter-language application

When the programmer is written in the source code with its favorite programming language, this source code is compiled into a separate executable unit (PE) before being converted into a media language (IL). This way, no matter you a VB. NET programmers, or a C # programmer, even using managed C programmers. As long as it is compiled into IL, it is equivalent.

First, the compilation and output EXE is a standard executable file header that is added by the intermediate language (IL), metadata and an additional compiler (such as the Win32 platform plus a standard Win32 executable) Head) The PE (Portable Executable, portable actuator) file, not a traditional binary executable - although they have the same extension. The intermediate language is a set of instruction sets independent of the CPU, which can be translated into local code for the target platform by instant compiler JITTER. The intermediate language code makes all the advanced languages ​​of all Microsoft.Net platforms C #, VB.NET, VC.NET, and the like to be independent, and interoperability between languages. Metadata is a collection of tables embedded in the PE file.

2. safety

. Net provides a set of security schemes. Access security check responsible for the code. Allows us to access resources and operations. The code needs to be trusted by different degrees of trust after identifying identification and origin. Security Policy is a set of configurable rules that follow this rule when deciding to allow the code to perform operations. The security policy is set by administrator and is enforced by the running library. Renault ensures that the code can only access the resource allowed by the security policy and call the code allowed by the Security Policy.

Whenever a load assembly occurs, the RTM uses the security policy to determine the permission to grant the assembly. After checking the information (called evidence) of the description assembly identifier, the runtime uses the security policy determines the degree of trust of the code and the authority to grant the assembly. Evidence includes, but not limited to the publisher of the code, its site and its area. Security policies also determine permissions to grant application domain.

Safety reference:

MS-help: //ms.vscc/ms.msdnvs.2052/cpguide/html/cpconkeyconceptsinsecurity.htm

3. Verification and program

Due to the use of metadata, you can use XCOPY simple replication, and the CLR can read the metadata during the runtime period to ensure that the multi-version program runs in the same process. All version controls of the assembly using the public language runtime are all performed on the assembly level. A specific version of an assembly and the version of the dependent assembly are recorded in the list of this assembly. Unless the explicit version policy override in the configured file (application configuration file, the issuer policy file, and computer configuration files), the default version of the run library is that the application is only used to generate and test them. The program version is running together.

Reference assembly version control:

MS-help: //ms.vscc/ms.msdnvs.2052/cpguide/html/cpconassemblyversion.htm

4. Call and configuration

When the runtime runs to resolve the reference to another, you will start positioning and binding to the process of the assembly. This reference can be static or dynamic. When generating, the compiler records a static reference in the metadata of the assembly list. Dynamic reference is dynamically constructed by calling various methods, such as system.reflection.Assembly.Load methods.

The preferred way to reference the assembly is to use full reference, including assembly names, versions, regional, and public key tags (if present). This information will be used to locate the assembly. Whether the reference is a reference to a static assembly or a reference to the dynamic assembly, the running library uses the same resolution process.

The assembly can also be dynamically referenced by providing only some information about the assembly (for example specified only the assembly name) to the calling method. In this case, search assembly is only in the application directory, and other checks are not performed. You can use any method in different loading assemblies (such as system.reflection.Assembly.Load or AppDomain.Load) to partially reference. If you want to run the library to check the referenced assembly in the global program cache and application directory, you can specify a part reference with the System.Reflection.Assembly.LoadWithPartialName method. Finally, you can use the method such as System.Reflection.Assembly.Load to dynamically reference and only part of the information; then use the element in the application configuration file to limit the reference. This element allows you to provide full reference information in the application configuration file, including name, version, regional, and public key tag (if applicable). If you want to fully qualify a reference to an assembly in addition to the application directory, or if you want to reference the assembly in the global program cache, you want to easily specify a complete reference in the configuration file instead of the code, you can This technology is adopted.

5. GC

A tracking process that delivers sexually tracks all the pointers to the current object to find all objects that can be referenced, and then reuse any stacks that are not found during this tracking process. The public language runtime garbage collector also compresses the memory in use to reduce the workspace required for the heap.

The basic algorithm of the garbage collector is simple:

● Mark all managed memory as garbage

● Find the memory block being used and mark them as valid

● Release all memory blocks that are not used

● Squiring piles to reduce fragmentation

GC reference:

http://www.9cbs.net/develop/read_article.asp?id=13895

http://www.9cbs.net/develop/Article/13\13896.shtm

6. . NET Framework

Don't say more, I have known it. reference:

MS-Help: //ms.vscc/ms.msdnvs.2052/cpref/html/cpref_start.htm

转载请注明原文地址:https://www.9cbs.com/read-27442.html

New Post(0)