For academic exchange, we have prepared 《. net and VB. net, I am in charge. net Framework and VB. net compilation principles, but I wrote it and found that if you understand. net Framework, VB. the net compilation principle is actually included in this section. Therefore, the writing becomes a topic. This article will introduce it to you from a macro perspective.. net.
Mind Map
From a macro perspective, I drew a mind map.
Specifically, I want to talk about the following aspects:
What is a framework?
Explanation. net, you must first talk about What framework is. It is translated as "framework, architecture, structure, and system". In short, it refers to the macro guidance and solutions for a class of things and problems. Therefore, we can understand the. NET Framework as the guidance and solution for the. NET system. This is a concept. instantiate it, that is, the. NET Framework you installed.
Framework x. x.
. Net objective
Before learning a knowledge, we need to know where it comes from and how to use it. My understanding is that the main reasons for the launch of. NET are as follows:
- Cross-language
- Cross-platform
- Management Mechanism
- Competing with the Java System
The first two points will be highlighted below.
According to the principle that. NET is composed of a large number of details, we need to first look at the composition of. net. If the. Net Diagram is shown on the left below, the teacher should beat me, so I drew the picture on the right, which makes it easier to understand from top to bottom.
FCL
Because this article is only a macro introduction, Baidu stands for it.
FCL represents a variety of classes in. net, such as writing a statement:
SqlConnection con = new SqlConnection(conn);
In fact, the sqlconnection class in FCL is used, because it is a class of the "static" concept, we can understand it. net "attribute", based on top-down dependencies between them, is divided into three layers:
- The base class library consists of most of the Bcl classes. To put it bluntly, it is some basic classes in. net.
- Function libraries, XML class libraries, and GDI + include encapsulated classes for operating system functions, such as I/O operations.
- Application Layer, winform, ASP. NET, and classes unique to each programming language.
Bcl
As mentioned above, it is. the basic part of the net class library, most of which are included in the FCL system class library, is the most basic class library. Let's create a C # project as an example. Open the Object Browser and see mscorlib, it is a member of Bcl and cannot be deleted in the reference.
CLR. net execution engine is responsible for the operation and use of classes in FCL. we can regard it. net "functions, events" (I .e. dynamic), it has two main roles.
- Like java's JVM, it ensures decoupling between applications and underlying operating systems.
- Responsible for automatic management of. net programs, such as resource management, security check, and exception management.
Let's take a look at the common features of CLR:
CTS and CLS
CTS is a set of rules, a protocol, isMacro language specificationsDefines what the language can do, what it can do, and what features it has.
Any advanced language that meets this set of rules can be called a. NET Framework-oriented language (not all), which means that. NET is very scalable and can be added to other advanced languages.
CLS is a subset of CTS, and it is also a set of rule protocols, but unlike CTS, it isSpecification of language details: The minimum features that must be supported by a language targeting the. NET platform, such as case sensitivity, naming rules, identifiers, and how to use constructors ......
From the preceding figure, we can see that CTS and Cls are located between the programming language and Cl. The two serve both the specification and interface so that different programming languages can communicate with FCL.Reduces interoperability issues caused by different languages.
Msil
Microsoft's intermediate language is similar to assembly language: it is more advanced than machine language, but not a high-level language. It is the intermediate language after code compilation. It does not have content that is linked to the CPU or the operating system, so it is decoupled from the CPU. The reason is that it is an intermediate language because. all languages under. NET are compiled with msil.
Because it is an intermediate language and the CPU cannot be executed directly, msil is interpreted by the real-time compiler to form executable machine code, although there is one more step ahead of the previous Vb-like language, it is possible for cross-platform execution.
Metadata and list
It is a group of compiled tables that contain the basic information of the compiled content, for example: specifies which classes and class member information are compiled in the program, and which assembly is relied on ....... It is equivalent to a list and Directory, which extracts some macro content for easy access by the program.
The reason why I speak so many concepts is to prepare for cross-platform and cross-language interpretation.
Cross-language
A good feature in. NET is cross-language. What is cross-language? Yes. different languages can use and even inherit from each other on net. Therefore, a simple demo is written: The main idea is to use VB.. Net writes a class and compiles it into a DLL, references it in C #, and inherits it.
Vbnetclass. VB
A class written in VB. NET is very simple. It only returns a string, indicating that this is a VB. NET class.
Public Class VBNETClass Public Function GetClassType() As String Return "My type is VB.NET" End Function End Class
Compile the DLL and reference it to the C # project:
The vbnet shown in red is the DLL generated by the vbnetclass class above. Other content displayed in red indicates that this is a C # program.
CSHARP. CS
Add a reference to vbnet, create a C # class named CSHARP, and use it to inherit vbnetclass.
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using VBNET;namespace WindowsFormsApplication1{ public class CSharp :VBNETClass { }}
Because of inheritance, the class CSHARP contains the getclasstype () function of vbnetclass.
Run
Write a window program and execute it.
private void button1_Click(object sender, EventArgs e) { CSharp csharp = new CSharp(); MessageBox.Show(csharp.GetClassType().ToString()); }
Running result
Why can I write data in two different languages for mutual reference? The following will be announced soon ......
Cross-platform
The so-called cross-platform refers to the ability to span the operating system or hardware. For example, the examination system compiled in Windows can run in Linux. The following figure shows the common operating system and CPU.
Principles
What is the principle of cross-language and cross-platform?
Do you still remember the msil, CLS and CTS, metadata, and list mentioned above? It is precisely because of the cooperation of these parts that cross-language is realized. The preceding cross-language example shows the process as follows:
- Both VB. NET and C # comply with CTS, so as to ensure that both have consistent Behavior Basis
- The existence of CLS makes the two have the basis for data-type communication. The ing relationship is as follows:
- Communication between the two
Cross-platform
For example, to run a program compiled in Windows in Linux, you must first install the. NET runtime environment mono in Linux. Because of the existence of msil and the cooperation with CLR, the process of interpreting executable code is as follows:
- Copy the compiled DLL to the Linux system and configure the mono Runtime
- Use the mono interpreter to interpret the copied dll as a recognizable Linux code.
- Run the program
Of course, the above is an ideal situation. In actual situations, it is true for other systems.. Net Running environment either does not exist or has limited functions, and windows. net encapsulates windows APIs. How to implement these functions on other systems is also a headache.
Execution Process
To better understand cross-platform and cross-language, let's take a look at the process from code to execution.
The process is as follows:
- Compile. net source code into msil
- CLR instant compiler interprets msil as local code
- Execute Code
From the macro view on the figure, CLS partially explains cross-language, and CLR partially explains cross-platform.
Decompilation of knowledge expansion
Using. Net reflector, You Can decompile the DLL into the source code and decompile the EXE or DLL. This may be different from the real code, but for us, the idea is sufficient, as shown below:
Anti-Compilation
Currently, there is no practical way to prevent decompilation. You can change the class name and class member name to a meaningless string, but The Decompilation process can still be understood, but it takes a lot of time.
Reflection principle
The following is a reflection code that you can see when you check the thin billing system:
The general principle is:
- Load the DLL according to the DLL name
- Load a class based on the class name, metadata, and form (see the preceding figure ).
If you only need to load the class, this is done, but we need to convert it to a class that meets the specified interface, so we have added a forced type conversion statement.
If you are interested, let's take a deeper look. Here is just a macro idea for you.
Summary
The. NET basics are mentioned first. Basically, I have explained it from a macro perspective. net. If there is any error, please correct it.
We recommend that you read the PPT with this article.
Framework.