A brief analysis of. NET Core Composition System

Source: Internet
Author: User
Tags dotnet reflection

This article describes in detail the composition of the. NET Core Framework and the main functions of each module, and how to implement Cross-platform.

The above illustration depicts the system structure of. NET Core, the top tier is the application layer, the development of a framework based on UI applications, including ASP.net Core (for creating web Apps), and UWP (for creating the Windows10 app).

The middle tier is a common library (COREFX) that implements the. NET Standard Library, which includes common system-level operations such as files, networks, and so on. (Microsoft defines a high-level interface called. NET Standard Libray, which is an interface, not a concrete implementation.) This interface has its own implementations on different operating system platforms, such as Windows and Macs, because the libray of these implementations is obtained by NuGet, so when using NuGet, the server will return the corresponding version to you based on the system information contained in the request you sent over. )

Under Corefx, the. NET Core contains two runtime (CORECLR, Corert), CORECLR is a runtime based on the Just-in-time compiler (Just in time Compiler,jit), It uses a cross-platform open source compiler Ryujit, while Corert uses the runtime of the advance compiler (ahead of time Compiler,aot), which can either use Ryujit for AOT compilation or other AOT compilers. Because the AOT compiles IL into machine code in advance, it also has better start-up speed and energy saving on mobile devices.

Finally, mention an open source cross-platform source compiler Roslyn, which is different from just two compilers, the JIT and AOT compilers are primarily used to compile the Il Machine code, and Roslyn is to compile C # or vb.net code into a program intermediate language (intermediate Language,il). (In simple terms, that's it.) For example, we currently click on the Compile button under VS, the whole procedure is C # code =>il is over. But using Corert mode, the whole process is C # code =>il=>native code. Both modes include the transition to IL, and the compiler is Roslyn. Roslyn Compiler

The Roslyn compiler is used to compile C # or vb.net code into an assembly (assembly), and its compilation process is a pipe-style process that consists of 4 steps, as shown in the following figure.

A. Parser (interpretation)

Source code is parsed based on syntax.

B. Declaration (statement)

Generates metadata for code (metadata), which is a collection of data tables that describes the data types and members defined in the current code, and also describes the types and members of references.

C. Bind (Binding)

Binds the generated IL code with the metadata that describes it, generating the Managed module (managed module).

D. Emit (generated)

Merges one or more managed modules to build an assembly (assembly). Ryujit Compiler

To execute a method in a program run, you first need to convert the already compiled IL to the machine code of this machine, and this task is given to Ryujit. It is a new generation of JIT compilers that first implemented the AMD64 architecture, Ryujit able to generate code faster than JIT64 (the previous compiler) to improve program efficiency (test Details link). CoreCLR & Corert

. NET core Runtime (CORECLR) and. NET Core Runtime (Corert) are all the. NET Core runtime (Runtime).
They provide core functionality similar to the. NET Framework CLR (memory management, assembly loading, security, exceptions, thread management, and so on) and can be used by all languages that are oriented to the runtime.

Unlike Corert and CORECLR, Corert offers a set of
AOT mechanism, you can compile a. NET core program into native code and run on the host machine without relying on the. NET Runtime. In addition, most of the functional code for the two runs is shared, such as GC. AOT Optimization offers a number of benefits:

Compiled into a single file that contains all the dependencies, including Corert, without installing the framework

Machine code is started, no machine code needs to be generated, and the JIT compiler is not loaded

You can use other tuning compilers, including LLILC, IL to CPP

Corert has two ways to generate machine code, the first use is to compile IL into machine code directly, by default, Ryujit compiles IL into machine code as an AOT compiler, and another way is to compile C # code into C + + code, and then invoke the C + + compiler of the corresponding platform to optimize the compilation into machine code. (Corert first requires Rolsyn to compile the C # code into IL, and then the difference between compiling IL into machine code or compiling it into C + + code.) )

Compiling machine code using RYUJIT

dotnet restoredotnet build--native--ilcpath <repo_root>\bin\product\windows_nt.x64.debug\packaging\publish1

Compiling and generating C + + code

dotnet restoredotnet build--native--cpp--ilcpath <repo_root>\bin\product\windows_nt.x64.debug\packaging\ Publish1--CPPCOMPILERFLAGS/MTD

Corert also has its drawbacks, it needs to be compiled for different platforms once; however, it allows engineers to not publish to platforms they do not want to support (for example, a game only supports desktops and does not support mobile phones).

Note: These two names are not available in the. NET core RC2 version, which is officially said to have been removed from the current release, and will not be known until the official version of June 27 is issued COREFX (. NET core libraries)

Corefx mainly includes several public libraries, such as System.Collections, System.IO, System.Xml, etc. The COREFX is an implementation of the. NET Standard library, and the same. NET Framework 4.6.3 is based on the. NET Standard Library. They are now based on the. NET Standard Library1.6 version, as shown in the following table:

. NET Core code Development, deployment, running process

From the figure above you can see that using JIT compilation and using AOT to compile source code and run programs are two different processes.

If you deploy a program using the JIT compiler, you only need to package the program as Il assemblies, before the first execution of the method the compiler compiles IL to target machine code (Native code), and AOT compilation compiles the source code directly into the target machine code at compile time.

The AOT compiles the source code into machine code and has the following characteristics:

Replace reflection with static code, for example if a value type (value) does not override the Valuetype.equals equals method, the default is equal, and reflection is used to find the filedinfo to determine whether the type is equal. Then compare whether value is equal. In AOT compilation, you can only compare the equality of value because it replaces reflection.

The dependent Third-party class libraries and. NET libraries are packaged into the final compiled program.

The packaged program runs on a thin version of the runtime (Corert), which includes the garbage collector, and the runtime is packaged in the app file.

Although the compilation will replace the reflection code, but when the dynamic reflection code is powerless, the runtime in case of dynamic reflection call will be unable to find the corresponding metadata and implementation and throw an exception. The workaround is to configure the runtime instruction file (Runtime Directive file) before compiling to specify the assembly that needs to be used. Summary

This section describes the structure of the. NET core, including the addition of multiple compilers and the Corefx of the. NET Standard Library, and overall the. NET core compared to the previous. NET Framework has greatly improved performance and development efficiency. The key is to achieve it for the first time. NET's complete cross-platform capabilities of the base technology stack.

. NET core is based on cross-platform capabilities and does not migrate GUI-highly-related APIs into. NET core, so that Windows Forms or Windows Presentation Foundation (WPF) do not migrate to. N The ET core.. NET core supports the console application (console application) and the type of class libraries (classes Library) project.

However, Microsoft uses. NET Core in its universal Windows Platform (UWP) development platform and uses the. NET Native technology to elevate its performance to very close to the native code speed.

asp.net core is a console application that drives its managed environment Kestrel Server to support the ASP.net Core program.

Author: The origin of the Handsome worm: http://www.cnblogs.com/vipyoumay/p/5613373.html

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.