当前位置:网站首页>Net standard
Net standard
2022-04-23 17:04:00 【begeneral】
.NET Standard Is a set of protocol specifications , Unlike .NET Framework, The latter is a complete technical framework , The former just defines a set of specifications , To abide by this set of norms .NET Standard Class libraries can be different .NET Frame reference , such as .NET Core The project and .NET Framework Projects can reference this class library .
Standard This word is the standard 、 Normative meaning ,.NET Standard To establish a set of standards , Solve the difference .NET Compatibility between frameworks .
About .NET Standard The concept of and the knowledge supported by the version , Please refer to MSDN Introduction to :https://docs.microsoft.com/zh-cn/dotnet/standard/net-standard
Now let's use .NET Core and .NET Framework Frame as an example .NET Standard Why can we solve different problems .NET Compatibility between frameworks
stay .NET Core In the project , On the surface, the assembly of the class we use is similar to .NET Framework It's exactly the same , Since they use exactly the same assembly , Why can the former cross platform , The latter cannot ? On this question , Please read my other article :.NET Framework Type shift .
Speaking of this , I have a question , Why does Microsoft use type transfer technology ? Don't you just use the corresponding assembly of the corresponding framework directly . I think so : because .NET Framework The frame takes longer than .NET Core The framework came out much earlier , Microsoft in order for everyone to adapt .NET Core, So the assembly of each class remains unchanged , But actually , Microsoft uses type shifting technology to redirect the original assembly to .NET Core On assembly . This operation is transparent , So we don't feel it .
Let's talk in code
We build a new one .NET Standard Class library of , Version is 2.0. And then create a new one Utils Class , The code is as follows :
public class Utils
{
public static void PrintAssemblyNames()
{
Console.WriteLine(typeof(Dictionary<,>).Assembly.FullName);
Console.WriteLine(typeof(SortedDictionary<,>).Assembly.FullName);
}
}
We output the assembly of these two types .
Then we create two new projects , One .NET Framework 4.7.2, One .NET Core 3.1, Both projects are console programs , The code is as follows :
static void Main(string[] args)
{
Console.WriteLine(".NET Framework 4.7.2");
Utils.PrintAssemblyNames();
Console.ReadKey();
}
static void Main(string[] args)
{
Console.WriteLine(".NET Core 3.1");
Utils.PrintAssemblyNames();
Console.ReadKey();
}
Both projects have done only one thing , Output the information of the assembly where the above two classes are located in the current project . Let's take a look at the output :


You can see , The output assemblies are different in different projects . Now let's see why it's different .
First let's look at Dictionary class , stay .NET Standard In the project , go to Dictionary The definition of , At the top of the page, you can see that its assembly is netstandard. because .NET Framework The version is 4.7.2, Runtime is CLR4.0, So we found CLR4.0 Under the netstandard.dll, This dll The path on my computer is :C:\Windows\Microsoft.NET\Framework\v4.0.30319\netstandard.dll, Use... When found ildasm.exe Open this. dll, double-click MANIFEST, Click Find System.Collections.Generic.Dictionary, You can see the following code :
.class extern forwarder System.Collections.Generic.Dictionary`2
{
.assembly extern mscorlib
}
This code means System.Collections.Generic.Dictionary Class is defined in the assembly mscorlib in , So in .NET Framework4.7.2 Under the project ,Dictionary The assembly of the class is mscorlib.
Use the same method , Find the netstandard stay .NET Core The following path :C:\Program Files\dotnet\shared\Microsoft.NETCore.App\3.1.4\netstandard.dll, Also decompile this dll, You can see the following code :
.class extern forwarder System.Collections.Generic.Dictionary`2
{
.assembly extern System.Collections
}
.class extern forwarder System.Collections.Generic.SortedDictionary`2
{
.assembly extern System.Collections
}
Both types are redirected to System.Collections in , But as you can see from the output Dictionary The result set of System.Private.CoreLib, Why is that ? Because it was transferred twice , To verify this problem , We found it in the same directory System.Collections.dll, Use the decompile tool to open this dll, You can see the following code :
.class extern forwarder System.Collections.Generic.Dictionary`2
{
.assembly extern System.Private.CoreLib
}
So in .NET Core In the project Dictionary The assembly of type is System.Private.CoreLib.
In fact, there is a detail that is easy to be ignored , From this detail, we can also see the assembly of these two classes . We are .NET Core and .NET Framework The project also defines a Dictionary class , Then go to its definition , As can be seen in the .NET Core In the project , Its assembly is System.Collections, Because there are two transfers , So the final assembly is System.Private.CoreLib; stay .NET Framework In the project , Its assembly is mscorlib; But in .NET Standard In the project , Its assembly is netstandard.
.NET Standard The reason why class libraries can be compatible with other .NET frame , That's why netstandard.dll. In this project class library, the assemblies of all types are netstandard.dll, And then all of the .NET The framework also implements the types in this assembly , By type transfer ( Also known as shim assembly ), The final called assembly is the runtime assembly of the project that references the class library , This is it. .NET Standard Compatible with other .NET The reason for the framework .
版权声明
本文为[begeneral]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230554082346.html
边栏推荐
- Further optimize Baidu map data visualization
- 1-5 nodejs commonjs specification
- 深入了解3D模型相关知识(建模、材质贴图、UV、法线),置换贴图、凹凸贴图与法线贴图的区别
- ◰GL-阴影贴图核心步骤
- Camtasia2022软件新增功能介绍
- Dancenn: overview of byte self-developed 100 billion scale file metadata storage system
- [registration] tf54: engineer growth map and excellent R & D organization building
- Nifi fast installation and file synchronization
- Multithreaded @ async thread pool
- Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
猜你喜欢

Nodejs installation and environment configuration

Further study of data visualization

Milvus 2.0 détails du système d'assurance de la qualité
![Knowledge points and examples of [seven input / output systems]](/img/5f/759a73836f79ef848f449930dcc1b1.png)
Knowledge points and examples of [seven input / output systems]

oracle 中快速获取表的列名列表

feign报400处理
![[PROJECT] small hat takeout (8)](/img/54/0187eeb637f4dcd4ad3969b00e2b77.png)
[PROJECT] small hat takeout (8)

Quick install mongodb

∑GL-透视投影矩阵的推导

Grpc gateway based on Ocelot
随机推荐
Shortcut keys (multiline)
◰GL-阴影贴图核心步骤
聊一聊浏览器缓存控制
SPC introduction
Multithreaded @ async thread pool
Bytevcharts visual chart library, I have everything you want
Idea of batch manufacturing test data, with source code
groutine
ByteVCharts可视化图表库,你想要的我都有
How does flash cache data in memory?
[pimf] openharmony paper Club - what is the experience of wandering in ACM survey
1-3 nodejs installation list configuration and project environment
Handwritten event publish subscribe framework
杂文 谈谈古典的《拆掉思维里的墙》
Production environment——
Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
Pseudo Distributed installation spark
Nacos + aspnetcore + Ocelot actual combat code
Wiper component encapsulation
How much do you know about the process of the interview