. NET Framework

Source: Internet
Author: User
Tags mscorlib
    • 6.3.1 Bcl-base class library
    • 6.3.2 FCL-framework class library
    • 6.6.1 Assembly Overview
    • 6.6.2 run the Assembly
. NET Framework

This book is a book that explains. NET technology. The target reader group is also developed under the. NET Framework.ProgramPersonnel, so the question we cannot avoid is: what is the. NET Framework? What does it contain? What support does it provide for development programs? Many of my friends may first respond to this kind of problem through the huge class libraries and programming provided by the. NET Framework.CodeThe C # language is actually far more than that.

Description. net Framework will naturally encounter a series of technical terms and abbreviations related to it. I believe you have seen many of them, such: CLI, CEL, CTS, CLS, CLR, JIT, BCl, FCL, module, assembly, and so on are enough to make many people confused and discouraged. I will not sort the terms one by one in alphabetical order, because it is hard to understand. Let's start with what everyone is most familiar!

6.1 intro

Imagine: Write the following simple display: "Hello, world !" Which of the following steps is required to run the console program and run the program?

Using system;
Class program {
Static void main (string [] ARGs ){
String text = "Hello, world! ";
Console. writeline (text );

These steps include opening Visual Studio, creating a C # console application project (named consoleapp here), writing code, compiling the program, and running it.AlthoughEveryone will write such a program, but if you think about it more, you will find that although it is a very small program, it has introduced several important aspects of the. NET Framework.

If you create a VB. Net project to implement the same functions as the C # project above, what is the difference between the files generated after compilation?

Compile a console application to output characters to the screen. You must call the console. writeline () method. Where does this console type come from?

How does the generated file run in the system? Is its mechanism the same as that of executable files generated using traditional VC ++?

In fact, the answer to each of the above questions includes the support provided by the. NET Framework. Here it is divided into three parts:

    • There are many standards in. net for the format and content of the files generated after compilation. Program languages that comply with these specifications are also called. Net-oriented languages. All files generated after compilation can be executed during. Net running, which is well-known. Net multi-language support.
    • In the development phase,. NET provides a large class library that allows developers to quickly develop various applications and also supports Programming Language designers to develop their language compilers.
    • In the program execution stage,. NET provides an environment for running programs. This runtime environment helps us manage memory, compile programs in real time, perform security checks, and execute garbage collection.

Next we will give a detailed description of the above content.

6.2-common intermediate language

The first thing to understand is what kind of file the C # program source code will get after compilation. As you know, the executable files generated using VC ++ in the past have been precompiled, compiled, compiled, and linked, the final executable file contains the native code of the processor. Only the operating system and the local machine instruction set can be run. So what is the file generated using the C # compiler? Now we need to introduce the concept of Assembly: executable program ). Before introducing this concept, the word "file" was used to describe the Assembly mentioned above (in the previous section.

The definition of the Assembly is just a slightly formal name for the file generated after compilation. It is not very helpful to explain the question of "What is it. To further understand the Assembly, let's try again, using VB.. Net creates a console application project (consoleappvb) and generates an assembly. The code function is the same as the project created in C.

Module Program
Sub main ()
Dim text asstring = "Hello, world! "
Console. writeline (text)

Now, you need a tool to view the content of this Assembly and compare it with the Assembly generated by the C # project. Fortunately, Microsoft has provided a powerful tool, il dasm (IL disassembler er, il disassembly Program), to help developers view assembly information. If Visual Studio is installed, il dasm will be installed along with Visual Studio. Choose Start> Microsoft Visual Studio 2010> Microsoft Windows SDK tools> il disassembly Program (IL dasm) to start il dasm.

Open il dasmand select leleappvb.exe from the vb.netproject. The page shown in 6-1 is displayed.

Figure 6-1 IL dasm running interface

This part contains a lot of content, which will be specifically described in the next chapter "assembly". It is skipped for the moment. The leleappvb. program type in Figure 6-1 is displayed. Double-click the main () method. Another window is displayed, showing the code in Figure 6-2, which looks a bit like assembly language. Here we can see the familiar string text variable declaration and "Hello, world !".

Figure 6-2 Description of the method body using the pencil language (VB. NET)

Click Next and open the leleapp.exe file generated by the cworkflow project to perform the same operation. After you open the main () method, you will find that the code is almost the same as that in Figure 6-2, as shown in Figure 6-3.

Figure 6-3 the description of the method body in the pencil language (C #)

So far, we can get a preliminary conclusion: whether it is VB. net or C #, the compiled assembly can be opened with IL dasm, so the formats of the Assembly they generate are the same. When the functions implemented by the program are the same, the Assembly contains the same template code.

Now, we will introduce the Assembly-like languages in the above Assembly, that is, the common intermediate language in the title of this section ). Since it was first released by Microsoft with. net, it was also known as Microsoft intermediate language (msil). later it was standardized and then named as "pencil. In some books or articles, the same thing exists in the form of "Il. To avoid confusion, this book uses the abbreviation "pencil.

We can represent the above process in Figure 6-4.

Figure 6-4 compile the source program as an assembly

Next, let's take a deeper look at the meanings of the common intermediate language term.

    • Public. Because both C # and VB. net Language, C ++/CLI language, or even a re-developed language named after its abbreviation, as long as the target platform it expects to run is. net, after compilation by the appropriate compiler, the generated assembly is described by the code in the language "pencil.
    • Center. This word is also very profound. Why is it not called the common machine language or the common native language )? This is just a higher level language than we use, such as C #. It is not a local machine language that the CPU can directly execute. This language is also required. net Runtime (. net runtime) Environment Support, before execution, a secondary compilation process called just-in-time (Real-Time), can be converted into a computer can recognize the instruction. We will introduce the. NET runtime and detailed process later. As long as you know, the. NET code contained in this file is not the instruction code that the machine can directly execute.
    • Language. It is just a programming language, but it is a lower-level language than C. From the code in Figure 6-2, we can see that the WordPress is a stack-based language. At the same time, it provides class, interface, inheritance, polymorphism, and many other object-oriented language features, so it is a fully object-oriented language. If you want to, you can even directly write the template code, and use the Il ASM (IL assembler) tool of the template to compile it. However, like most low-level languages, this method will make the development efficiency very low. Note that the spelling of il asm and IL dasm is different.

To help you better understand the situation, let's take a test: compile a simple section of the Code, and use the Il asmtool to compile and compile the new leleapp.exe program.

1) Open the Notepad program, enter the following code, and save it in D: \ consoleapp. il.

. Assembly extern mscorlib {}
. Assembly consoleapp {}
. Module consoleapp.exe
. Class public auto ANSI program extends system. Object
. Method public static void main ()
. Entrypoint
Ldstr "Hello, world! "
Call void [mscorlib] system. Console: writeline (string)

2) Open the Visual Studio 2010 command line tool and enter:

D: \> ilasm consoleapp. Il

After 3rd success, we will see the leleapp.exe program. Its execution results are exactly the same as those written in C # above.

Since an assembly is described by the Perl language, it is also called the assembly language ). Because the. NET Assembly needs to be loaded at. Net runtime before it can be run, it can be viewed as being managed at. Net runtime. Therefore, the. NET code is also called managed code ). The code that can be executed without the. NET Runtime is called the unmanaged code ).

Now that we know the existence of the pencil, we 'd better establish two models or two perspectives in our minds from now on: one is the perspective of the source program based on C # or other advanced languages, and the other is the perspective of the Assembly based on the link language. C # The source program is independent of C # after being compiled into an assembly, so the Assembly can be called by other languages. At the same time, because the Assembly does not contain commands from local machines, therefore, it is separated from the specific machine type and can be installed.. NET Framework.

6.3 Bcl and fcl6.3.1 Bcl-base class library

Let's take a look at an interesting phenomenon: Open the C # console Project (consoleapp) created earlier, and then open the "Reference" folder under the solution panel, if Visual Studio 2010 is used and the target framework is.. Net 4.0, the references shown in 6-5 are displayed.

Figure 6-5 "Reference" folder in Solution

If no additional operations are performed during project creation, these references are automatically added during project creation. For the convenience of beginners, I would like to explain a little here: to use (in fact, I think the word consume is more appropriate) the types designed by other developers, you need to reference the assembly of this type in the project. These assembly references are commonly used by Microsoft. They are used in almost every project. Therefore, they are automatically added during project creation, so developers do not need to add them manually.

But here these references are not conducive to our understanding of some content, so we delete all these references, as shown in 6-6, and then compile the program again.

Figure 6-6 Delete all project references

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Namespace consoleapp {
Classprogram {
Staticvoid main (string [] ARGs ){
String text = "Hello, world! ";
Console. writeline (text );

Some people may think that, after deleting these references, the compiler will prompt compilation errors unpolitely: the type or namespace "system" could not be found (is the using command or assembly reference missing ?). But in fact, when the above Code is compiled and run, the program will be correctly executed. This is because we have deleted all referenced assemblies, defined only one program type, and did not define the console type. Therefore, the first problem to be faced at this time is: where does the console type come from?

Visual Studio provides a quick way to quickly view the type: Position the cursor on the console, and then press F12 on the keyboard to see the type definition of the console. At the top of the console type definition, you can see its Assembly address: C: \ Program Files \ reference assemblies \ Microsoft \ framework \. netframework \ v4.0 \ mscorlib. dll.

# Region Assembly mscorlib. dll, v4.0.30319
// C: \ Program Files \ reference assemblies \ Microsoft \ framework \. netframework \ v4.0 \ mscorlib. dll
# Endregion
Using system. IO;
Using system. runtime. constrainedexecution;
Using system. Security;
Using system. text;
Namespace system {
Public static class console {
// Omitted in the middle

The console type is from the mscorlib. dll assembly. From the experiment above, we can see that mscorlib. dll assembly is always automatically referenced whether or not we reference it. The class library contained in this Assembly is the BCL (base class library, base class library) in the title of this section ). It can be seen from the name that this class library contains some of the most basic types, which have already been integrated with the Council language to provide basic programming support for the Council language, as a result, this class library has become part of the CLI standard (which will be introduced later, so we can say that the type in the Bcl is the type of the Council language, and can be used by all the language oriented to the Council. We can use the Object Browser (Visual Studio menu → view → Object Browser) to view which namespaces and types are included in the mscorlib. dll assembly, as shown in 6-7.

Figure 6-7 namespace contained in mscorlib. dll

We can see that this Assembly mainly contains the system namespace. A slightly careful reader will find that when creating a project, it also contains the system. DLL assembly, and the types contained are very similar to those in mscorlib.

Figure 6-8 system assembly

Figure 6-9 namespace contained in system. dll

What is the problem? In fact, you just need to open the system namespace and you will find that mscorlib. the types defined under the DLL system namespace and the system. the types defined in the DLL system namespace are completely different, and there is no conflict between them.

Now we understand that bcl provides a type like console to support developers to write programs like the console.

Now that you have thought so much about it, let's take a deeper look at the question: the written statement string text = "Hello, world !", Where does the string come from? Intuitively, string is rendered in dark blue in Visual Studio and belongs to the C # keyword, so it should be a built-in type provided by C. However, when we move the cursor over the string and press F12 to the definition of the string, we can see the following content:

# Region Assembly mscorlib. dll, v4.0.30319
// C: \ Program Files \ reference assemblies \ Microsoft \ framework \. netframework \ v4.0 \ mscorlib. dll
# Endregion
Using system. collections;
Using system. Collections. Generic;
// Some using is omitted to save space
Namespace system {
Public sealed class string: icomparable, icloneable, iconvertible, icomparable <string>, ienumerable <char>, ienumerable, iequatable <string> {
// Omit Definition

Note that at the top of the Assembly address, mscorlib. dll is displayed again, and the string type is the same as the console type, which is located under the system namespace. It can be seen that the string keyword of C # Is just an alias of the system. string type in BCl. Similarly, the string keyword in VB. NET is also an alias of the system. string type in BCl. Therefore, in the. NET Framework, there is not much difference between languages in essence, and more is about syntax. From the example above, we can see that C # and VB. many of the language capabilities of net are not their own, but they are borrowed from the Server Load balancer to ensure that the corresponding types of behaviors in different languages are consistent.

Table 6-1 lists several typical keywords in different languages. I think that understanding is more important than memory, so I only list a few. To understand other basic types, you just need to move the cursor over the type and then press the F12 key.

Table 6-1 Relationship Between Keywords of different languages and the pencil type

Pencil type C # keywords VB. NET keywords
System. byte Byte Byte
Sytem. int16 Short Short
System. int64 Long Long

As shown in Table 6-1 ,. net also supports language developers. if you need to design a language, you can map the keyword of the language to the type in the pencil when developing the compiler, that is, some special symbols (keywords) in your language) perform ing, as if the keywords int and string in C # are the same.

You may have heard of such a special type-primitive type ). As a matter of fact, we should have understood that those that are directly supported by the compiler and convert the keyword type of the language itself to the pencil type are called the primitive type. Obviously, the byte, Int, and string types above are all primitive types. C # does not have a keyword to map to the console, so we think that the console is just a common class type ).

6.3.2 FCL-framework class library

As A. Net programmer, FCL is used every day (framework class library, framework class library ). This section describes BCl, a subset of FCL. Bcl contains the core types that are closely related to the compiler and the language, as well as the types used in common development tasks. FCL contains a lot of content. It is enough to write a book for the sub-database serving only one application scenario. Here, we will simply introduce it.

From the functional perspective, you can divide the FCL framework class library into the following layers.

    • The inmost layer is composed of most of the Bcl and mainly serves on.. NET Framework ,. net runtime and the language itself, such as primitive type, set type, thread processing, application domain, runtime, security, and interoperability.
    • The middle layer contains encapsulation of operating system functions, such as file system, network connection, image, and XML operations.
    • The outermost layer contains various types of applications, such as Windows Forms, ASP. NET, WPF, WCF, and WF.
6.4 CTS -- Public Type System

Suppose we want to develop a new language, such as C # Or VB. net. After compilation, you can also generate the template code.. NET environment, so what do you need first?

According to the content described in section 6.2, we know that the new language to be developed is equivalent to the advanced language version of the pencil, So what to do is not determined by the new language, it is determined by the pencil. Therefore, a set of definitions, rules, or standards of the Server Load balancer are required. This set of rules defines what our language can do, what it can do, and what features it has. This set of rules is called CTS (common type system, common type system ). Any advanced language that meets this set of rules can be called a. NET Framework-oriented language. C # and VB. net is a set of languages developed by Microsoft that comply with CTS. In fact, many organizations or groups have developed such languages, such as Delphi.. NET and FORTRAN.

What specific content does CTS include? Before answering this question, we need to clarify a concept. You can use a piece of C # code to explain the following code:

Public class book {
// Omitted implementation
Book Item1 = New Book ();
Book item2 = New Book ();

The above code is usually described as follows: defines a book class and creates instances Item1 and item2 of two book classes. In fact, this only contains two meanings, as shown in Table 6-2.

Table 6-2 instances of Classes

Class Book
Class instance Item1, item2

After thinking about it, we will find that there is a higher level, that is, the type of the book class, which we call class type. Therefore, the above table can be changed to Table 6-3.

Table 6-3 instance types, classes, and classes

Class type Class
Class Book
Class instance Item1, item2

Similarly, Enum type and struct type are supported. Now we should understand what we want to express here. CTS stipulates that classes, structures, delegates, and Other types can be defined in languages. These rules define higher levels of content in languages. Therefore, in the implementation of C #, we can define the class type or structure type.

Similarly, you can define a field name in the book class and provide a method for showname (). In fact, these are also defined by CTS, which standardizes the types that can contain fields (filed), properties (property), methods (methods), and events (events.

In addition to defining various types, CTS also specifies various types of access, such as private, public, family (protected in C #), and assembly (internal in C), family and assembly (not provided in C #), family or assembly (protected internal in C ).

CTS also defines some constraints. For example, all types are implicitly inherited from the system. Object type, and all types can only inherit from one base class. According to the cts name and public type system, not only must C # meet these constraints, but all. Net-oriented languages must meet these constraints. As we all know, traditional C ++ can inherit from multiple base classes. To enable developers familiar with the c ++ language.. NET Framework.. Net C ++/CLI language (also called hosting C ++). It is a C ++ revised language that complies with CTS. To meet the CTS specification, it is restricted to inherit only one base class.

There are two special notes for the above content:

1) C # does not provide the implementation of family and assembly, and C # does not have a global method ). In other words, C # Only implements some of the CTS functions ., That is to say, CTS regulates all the capabilities that the language can implement, but the implementation of specific languages that comply with the CTS specifications does not have to implement all the functions defined by the CTS specification.

2) C ++/CLI is bound to inherit only one base class. In other words, some functions in C ++ are deleted ., That is to say, any language must comply with CTS, and some of the functions incompatible with CTS must be discarded.

It is clear that since the. NET Runtime is a language that can be understood, it implements all the functions of CTS. Although it is a low-level language, it has more complete functions. The relationship between C # language and pencil can be expressed in Figure 6-10.

Figure 6-10 Relationship between C # And pencil

6.5 CLS-Common Language Specification

Now that we understand that CTS is a rule definition in a set of languages, we can develop a set of languages to comply with CTS. Assume that this language is called N #, And the CTS implemented by it is very limited. Only a few of them are implemented. Its relationship with CTS and C # may be shown in 6-11.

Figure 6-11 Relationship between C #, N #, and pencil

Now there is a question: can an Assembly compiled by C # reference an Assembly compiled by N? The answer is obviously no. Although C # and N # belong to CTS, they do not share the same relationship. Therefore, although a separate N # Or C # program can run perfectly in the. NET Framework, they cannot be referenced from each other. If a developer who uses N # development project does not want a project in other languages to reference his project, however, if the N # project expects that projects in other languages can reference it, the types and functions exposed in N # must meet the features of C #, that is, they must have something in common. Note: One important word in this sentence is "public ). The private, internal, and protected parts in N # are not affected and can be used with unique language features, because these undisclosed parts are not allowed for external access. Therefore, if n # wants to be understood and referenced by C #, the public part of it must meet some C # specifications. At this time, the relationship between CTS and C # is shown in 6-12.

Figure 6-12 relationship between C #, N #, and pencil

If only C # and N # languages are available in the world, extract their common language features and then require that all public types meet these language features, in this way, the C # and N # assembly can be referenced by each other. The problem is that there are hundreds of language types, and. net is designed to implement an open platform, not only can the existing language run after simple modification. on the Net Framework, new languages can be developed in the future, but new languages do not exist at this time. How can we extract their language features? Therefore, a set of standards and standards are required to define common language features that are common to most languages. New languages in the future can be used by assembly in other languages as long as the public part meets these specifications. This specification is called Cls (Common Language Specification, common language specification ). Obviously, CLS is a subset of CTS. Now CLS is introduced, and the graph in Figure 6-12 can be changed to 6-13.

Figure 6-13 Relationship Between Language, CLS, and pencil

If the public part of an Assembly developed by C # only uses the features in CLs, this Assembly is called CLS compatible assembly (clscompliant assembly ). Obviously, for the FCL framework class library mentioned above, the types in the class comply with CLs, and only a few types of members do not comply with CLs, which ensures all the orientation.. NET language can all use the types in the framework class library.

Now, the reader has another question: language features are repeated in the above paragraphs. To meet CLs, the language features must be consistent, so what is the language feature? The following describes the specific language features: Case sensitivity, naming rules for identifiers, and the basic types that can be used, constructor call method (whether the base class constructor will be called), supported access modifiers, and so on.

How can we check whether the Assembly conforms to CLS ?. NET provides a clscompliant feature for us to check whether the Assembly conforms to CLS during compilation. Let's look at the following example:

Using system;
[Assembly: clscompliant (true)]
Public class clstest {
Public string name;
// Warning: the "clstest. Name ()" identifier is not CLS-compliant.
Public string name (){
Return "";
// Warning: the return type of "clstest. getvalue ()" does not match Cls.
Public uint getvalue (){
Return 0;
// Warning: the parameter type "sbyte" does not match CLS
Public void setvalue (sbyte ){}
// The warning identifier "clstest. _ afiled" does not match Cls.
Public String _ myproperty {Get; set ;}

It can be noted that the clscompliant feature is added to the Assembly before the clstest class, indicating that the Assembly is CLS compatible. However, the compiler provides a warning because three of them do not meet this requirement. These three points are:

    • Members cannot be distinguished in upper or lower case. Therefore, the field name and method name () do not conform to Cls.
    • The return type and parameter type of the method must be CLS compatible, and the uint and sbyte types are not CLS compatible. Therefore, the getvalue () and setvalue () methods do not conform to Cls.
    • The name of an identifier cannot start with an underscore (_). Therefore, attribute _ myproperty does not conform to Cls.

It will also be noted that the compiler only provides warning information, not error information, so you can ignore the compiler warning, but this Assembly can only be used by other C # languages.

6.6 CLR-Overview of the 6.6.1 Assembly during Common Language Runtime

As mentioned above, an Assembly contains the template code, which cannot be run directly. net can be converted into machine commands that can be directly executed by the computer after real-time compilation. How is this process implemented?

What we need to know next is the core part of the. NET Framework: CLR (Common Language Runtime), which is also called. Net runtime ). Before learning about CLR, you need to learn more about the Assembly, because the next section will specifically describe the Assembly. Here we will only briefly introduce the concept that the Assembly is helpful for understanding CLR.

From the perspective of intuition, the console application with the suffix ".exe" is a direct executable file, because it does run after you double-click it. The situation here is similar to the inheritance in Object-Oriented Systems: A car is first a motor vehicle, a cat is first an animal, and A. Net assembly is first a Windows executable program.

So what format is a Windows Executable File? This format is called PE/coff (Microsoft Windows portable executable/Common Object File Format), Windows portable executable/common object file format. The Windows operating system can concurrently run .dlland .exe because it can understand the format of PE/COFF Files. Obviously, all programs running on Windows operating systems must comply with this format, including. Net assembly. At this level, the control of the program also belongs to the operating system, and the PE/coff header contains information for the operating system to view and use. In this case, the Assembly can be represented as 6-14.

Figure 6-14 assembly structure 1

As mentioned above, the template contains no computer code that can be directly executed and requires real-time compilation. Before compiling the template code, you need to run the compiled environment first, so the PE/coff header is followed by the CLR header. One of the most important roles of the CLR header is to tell the operating system that the PE/coff file is a. Net assembly, which is different from other types of executable programs.

Figure 6-15 assembly structure 2

After the CLR header, you are familiar with the content. First, the Assembly contains a manifest, which is equivalent to a directory that describes information about the Assembly, such as the Assembly identifier (name, version, culture) the resources contained in the Assembly, the files that make up the assembly, and so on.

Figure 6-16 assembly structure 3

The list is metadata. If the list describes the information of the Assembly, the metadata describes the content contained in the Assembly. These include the module contained in the assembly (which will be introduced in chapter 7th), type, type member, type, and type member visibility. Note that metadata does not contain the type implementation, which is somewhat similar to the. h header file in C ++. In. net, the process of viewing metadata is called reflection ).

Figure 6-17 assembly structure 4

The next step is the program code that has been converted to the pencil, that is, the type implementation in the metadata, including the method body and fields, similar to the. cpp file in C ++.

Figure 6-18 assembly structure

In Figure 6-18, a resource file is added, for example, A. jpg image. From this figure, we can see that the Assembly is self-explanatory and you can fully understand all information about the Assembly without any additional things, such as the registry.

So far, a brief introduction to the Assembly is here first. Next, let's take a look at how the Assembly is executed.

6.6.2 run the Assembly

Now that you have learned about the assembly, and know that the CE code contained in the Assembly cannot be run directly, you also need the support of CLR. Generally speaking, CLR is a software layer or proxy that manages.. Net Assembly execution, including: Managing application domains, loading and running the Assembly, security check, instant compiling of server code into machine code, exception handling, object analysis and garbage collection. Compared with compile time, these processes occur in the process of running the program. Therefore, naming the software layer as runtime does not actually have much to do with time. Some of my friends are entangled in the term runtime when they are new to. net. They always think that what is the relationship with time does not always have a good understanding of CLR. I think it is important to understand what CLR is, rather than focusing too much on its name.

In fact, CLR also has a name, namely, virtual execution system (VES ). As described in the previous section, this name should better describe the role of Clr and may not cause confusion. However, it may be necessary to maintain consistency with terms such as pencil, CTS, and Cls, finally, name it CLR. Here, we know that CLR is just a runtime environment of A. Net assembly, a bit similar to a Java virtual machine. The term ves comes from the CLI and will be described in section 6.7.

Figure 6-19 describes the main role of CLR.

Figure 6-19 main roles of CLR

I have briefly understood the role of CLR, and I will start to learn more. The first problem I encountered was: In what form is CLR located?

Since CLR is used to manage managed code, it is written by unmanaged code. It is not an Assembly that contains managed code, nor can it be viewed using IL dasm. It is located at c: \ % SystemRoot % \ Microsoft. net \ framework \ version, depending on the installed machine has two versions, one is the workstation version of mscorwks. DLL. One is mscorsvr of the server version. DLL. Wks and SVR represent the work station and server respectively.

Next, let's take a look at how CLR runs. Although the. NET Framework has been pre-installed in the operating system since Windows Server 2003, it is not yet integrated as part of the operating system. When the operating system tries to open a pipeline (.exe), it first checks the PE Header and creates a proper process based on the PE Header.

Next, we will further check whether there is a CLR header. If so, mscoree. dll will be loaded immediately. This library file is one of the core components of the. NET Framework. Note that it is not an assembly either. Mscoree. dll is located in the C: \ % SystemRoot % \ system32 \ System Folder. This file is available to all computers with. NET Framework installed. You may have noticed that this library is installed in the System32 system folder, and is not stored in c: \ % SystemRoot % \ microsoft according to the version number as other core components or class libraries.. Net \ framework \ folder. Here there is another "zombie problem": different versions of CLR are loaded according to different assembly information, so there should be only one component to load CLR, the version of the CLR component cannot be determined based on the CLR version.

Mscoree. dll is a very fine software layer. After mscoree. dll is loaded, the _ corexemain () function is called, which loads the appropriate version of CLR. After the CLR is run, the execution right of the program is handed over to the CLR. CLR will find the entry point of the program, usually the main () method, and then execute it. The following process is included:

    1. Load type. Before executing the main () method, you must first find the type that owns the main () method and load this type. A component in CLR named class loader is responsible for this work. It searches for this type from the GAC, configuration file, and assembly metadata, and then loads its type information to the data structure in the memory. After the class loader finds and loads the type, its type information will be cached, so that the same process does not need to be performed again. After this class is loaded, a stub (stub) is inserted for each of its methods ).
    2. Verify. In CLR, a verification program (verifier) also exists, which ensures that the code is type-safe at runtime. It mainly verifies two aspects. One is that the metadata is correct, the other is that the template code must be type-safe, and the other is that the type signature must be correct.
    3. Instant compilation. This step is the process of compiling the managed server code into executable machine code, which is completed by the CLR real-time Compiler (JIT complier. Instant compilation only occurs when the method is called for the first time. Recall that the type loader inserts a stub for each method. When calling a method, CLR checks the method's stubs. If the stub is empty, the JIT compilation process is executed, and write the locally compiled machine code address of the method into the method stub. When you call the same method for the second time, the stub is checked again. If you find that the address of the local machine code is saved, the local machine code is directly redirected to the Local Machine for execution, you do not need to perform JIT compilation again.

We can see that one benefit of adopting this architecture is that ,.. Net assembly can run on any platform, whether it is windows, UNIX, or other operating systems, as long as the platform has.. NET Framework.. Net assembly.

6.7 cli-common language basics

CLI is an international standard, standardized by ECMA and ISO, all called common language infrastructure (public language basics ). It is just a concept and a summary. In fact, each section in this chapter is part of this standard. CLI includes the following elements: pencil, CTS, CLS, ves, metadata, and basic framework.

Many people may feel a little strange here. Why is the content of CLI and. NET Framework so similar? What is the relationship between them? In short, CLI is a standard, and the. NET Framework is the specific implementation of this standard. In CLI, there is no CLR concept, only ves, and CLR is the specific implementation of Ves in the. NET Framework. Since CLI is only a standard, and the. NET Framework is its specific implementation on the Windows platform, is it only the. NET Framework CLI implementation? Obviously not. The Mono Project is another Implementation of the CLI standard. The goal of the Mono Project is to implement multi-platform. NET framework so that it can run on various platforms, including Mac OS and Linux.

For details about the CLI, refer.

6.8 summary of this Chapter

This chapter provides a systematic introduction. the underlying knowledge of the. NET Framework contains almost all common terms, such as assembly, pencil, CTS, CLS, CLR, etc, they also introduced how they work together to build the entire process.. NET platform. I believe that after studying in this chapter, you will have a better overall understanding of the. NET Framework.

Thank you for reading this article. I hope this article will help you.


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.