Step by Step C # technical discussion 3. Common Language Runtime Library (CLR)

Source: Internet
Author: User

Overview

. The core of NET Framework is the execution environment of its Runtime Library, which is called the Common Language Runtime Library (CLR) or. NET Runtime Library. generally, the code running under the control of CLR is called managed code ).

However, you need to compile the source code compiled by CLR (in C # or other languages). In. NET, compilation is divided into two phases:

1. Compile the source code into Microsoft intermediate language (IL ).

2. CLR compiles IL into platform-specific code.

This two-phase compilation process is very important, because Microsoft intermediate language (managed code) is the key to providing many. NET advantages.

 

The overall structure of the. NET platform:

NET Framework is a virtual operating platform based on Windows. You can think of replacing Windows with other operating systems, such as Linux, in the same way, CLS (Common Language Specification, general Language Specification) can be used. NET language, (VB. NET, C #, JScript. to create ASP. NET. NET or Windows Form (may be called Linux Forms) application functions, which are actually the functions to be implemented by Mono. Therefore, in theory, C # is a cross-platform language, which is similar to Java. C # Another thing that is more similar to Java is that it is also a (special) language, just like Java, C # the program code written in C # is first compiled into a special byte code (Microsoft Intermediate Language, MSIL, Microsoft) using the C # compiler, at runtime, It is compiled into machine code by a specific compiler (JIT compiler, Just In tIME, JITer) for execution by the operating system.
Not only C # language, all. NET Language (which will include dozens of common modern coding languages) can be used to write CLR-oriented program code. NET is called Managed Code. All Managed Code runs directly on the CLR and has platform-independent features.

The explanatory language is safe and can be used to grant more functions to it through its operating platform, such as automatic memory management and exception handling.

 

CLR Structure Diagram

C # has many features provided by CLR, such as Type Checker, Garbage Collector, Exception Manager, and COM Marshaler) and so on ,. the CLR on NET provides the following services for developers:
1. platform independence: CLR actually provides a product that uses virtual machine technology. It is built on the operating system and does not require that the running platform of the program be Windows, any system that supports its Runtime Library can run on it.. NET application. Therefore, an application fully composed of managed code can run on any platform that supports. NET once compiled.
2. Cross-language integration: CLR sequential development is developed in any language. Code developed using these languages can be closely and seamlessly called in the CLR environment, for example, you can use VB to declare a base class object, and then directly create a base class derived class in the C # code.
3. Automatic Memory Management: CLR provides a pull collection mechanism to automatically manage the memory. When the lifecycle of an object or variable is completed, the CLR Automatically releases the memory they occupy.
4. Cross-language applications

When programmers write source code in their preferred programming languages, the source code is before it is converted into a media language (IL, it is first compiled into an independent executable unit (PE ). In this way, whether you are a VB. NET programmer, or a C # programmer, or even a C ++ programmer hosted. As long as it is compiled into IL, It is equivalent. First, the exe compiled and output is an intermediate language (IL), Metadata (Metadata) and an additional standard executable file header of the target PLATFORM added by the compiler (for example, the Win32 platform adds a standard Win32 executable file header) (portable executable, but not traditional binary executable files-although they have the same extension. An intermediate language is a set of CPU-independent instruction sets that can be translated by the instant compiler Jitter into local code on the target platform. The intermediate language code makes the C #, VB. NET, and VC. NET languages of all Microsoft. NET platforms independent of each other and implement interoperability between languages. Metadata is a collection of tables embedded in PE files.
5. Version Control

Because metadata is used, you can simply copy it using XCOPY. CLR can also read metadata during runtime to ensure that multiple versions run in the same process. All Version Control of the assembly using the Common Language Runtime Library is performed at the Assembly level. The specific version of an assembly and the version of the dependent assembly are recorded in the assembly list. The default version policy of the Runtime Library is, applications run only with the version of the Assembly they were generated and tested.
6.. NET Security

. NET provides a set of security solutions. Performs code access security checks. Allow us to access protected resources and operations. The code can be trusted to varying degrees only after identity confirmation and source authentication. A security policy is a set of configurable rules. The Common Language Runtime follows this rule when deciding on the operations that allow code execution. The security policy is set by the Administrator and enforced by the Runtime Library. The Runtime Library ensures that the code can only access the resources allowed by the security policy and call the Code allowed by the security policy. Whenever an attempt is made to load an assembly, the runtime uses security policies to determine the permissions granted to the Assembly. After checking the information describing the Assembly identifier (known as evidence), the runtime database uses security policies to determine the trust degree of the Code and the permissions granted to the Assembly. Evidence includes, but is not limited to, the Code publisher, its site, and its region. The security policy also determines the permissions granted to the application domain.
7. Simple component interoperability.
8. self-description component: a self-description component is an execution file that stores all data and code in one file. The self-describing component can greatly simplify system development and configuration, and improve system reliability.

The Common Language Runtime (CLR) was first called the next-generation Windows service Runtime (NGWS Runtime ). it is a virtual environment directly built on the operating system, and the main task is to manage code running. CLR now supports dozens of modern programming languages to write code for it, and then is executed in the form of an Intermediate language (Intermediate Langeoage, IL) code. In addition, CLR provides many features to simplify code development and application configuration, while also improving application reliability. As you know, if the compiler of a language aims at runtime, the code generated by using the language can be found in. NET is called managed code, because such code runs directly on the CLR, so it has platform-independent characteristics.

In. in the NET platform structure, the top of the CLR is. NET base class library. This base class library provides a unified, object-oriented, hierarchical, and scalable programming interface from basic input and output to data access. From. NET platform structure, you can also see that the base class library can be called and extended by various languages, that is, whether it is C #, VB. NET or VC ++. NET, can be freely called ,. NET class library, because C # Only has 77 keywords, And the syntax does not have to work for programmers. BCL, on the contrary, contains more than 4500 classes and countless methods, attributes, which may be used at any time in your C # program to complete their own tasks.

There is also a very important concept that you need to understand. This is the Common Language architecture (CLI ). CLI is a subset of CLR, that is. NET is the most final part of managing the runtime environment of the application compiled into MSIL code. In the CLR structure, the CLI is located in the lower half, mainly including Class Loader and real-time Compiler (IL To Native Compilers) and a runtime environment for garbage collection and code written in any language, through its specific compiler into MSIL code and then run on it, you can even write MSIL on your own and run it on CLI.

9. Call and Configuration

When the Runtime Library tries to parse a reference to another assembly, it begins to locate and bind it to the assembly process. The reference can be static or dynamic. During production, the compiler records static references in the metadata of the assembly list. Dynamic references are dynamically constructed by calling various methods, such as the System. Reflection. Assembly. Load Method. The preferred way to reference an assembly is to use full reference, including the Assembly name, version, culture, and public key tag (if any ). The runtime uses this information to locate the assembly. No matter whether the reference is a reference to a static assembly or a reference to a dynamic assembly, the runtime uses the same parsing process. You can also dynamically reference an assembly by providing only part of information about the assembly to the calling method (for example, specifying only the Assembly name. In this case, only the assembly is searched in the application directory and no other checks are performed. You can use any method in different Assembly loading methods (such as System. Reflection. Assembly. Load or AppDomain. Load) for partial reference. If you want the Runtime Library to check the referenced Assembly in the Global Assembly Cache and application directory, you can use System. Reflection. Assembly. LoadWithPartialName to specify partial references. Finally, you can use methods such as System. Reflection. Assembly. Load to dynamically reference and provide only some information. Then, use the <qualifyAssembly> element in the application configuration file to limit the reference. This element allows you to provide full reference information in the application configuration file rather than in the Code, including the name, version, culture, and public key tag (if applicable ). If you want to fully restrict the reference to an assembly outside the application directory, or if you want to reference the assembly in the Global Assembly Cache, however, this technology can be easily used in the configuration file instead of specifying full reference in the code.

10. GC

A tracing process that tracks all pointers to the currently used objects in a pass-through manner to locate all objects that can be referenced and reuse any heap memory not found during this tracing process. The garbage collector in the Common Language Runtime also compresses the memory used to reduce the workspace required by the heap. The basic algorithm of the garbage collector is simple: ● mark all managed memory as spam ● search for memory blocks being used, and mark them as valid. ● release all unused memory blocks. ● sort the heap to reduce fragments.

 

Basic Features of CLR

1. irrelevant to local code --- MSIL (intermediate language)

2. Let's use the same language-CLR (runtime in public languages)

3. Assembly (Assembly)

4. Run CTS in the same system)

5. After the Big Bang

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.