当前位置:网站首页>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