当前位置:网站首页>. net type transfer

. net type transfer

2022-04-23 17:04:00 begeneral

We all know , General frameworks can achieve downward compatibility , How is this compatibility realized ?

First we use .Net Framework 3.5 Create a console program , The code is as follows :

static void Main(string[] args)
        {
            Console.WriteLine(typeof(string).Assembly.FullName);
            Console.WriteLine(typeof(Func<>).Assembly.FullName);

            Console.ReadKey();
        }

This code outputs string The type and Func Information about the assembly in which the type resides , The output is as follows :

You can see string The assembly of type is mscorlib, The version is 2.0;Func The assembly of type is System.Core, The version is 3.5.

We use .Net 3.5 Compilation of , Logically speaking mscorlib The version of should be the same 3.5, Why 2.0 Well ?

because .Net Framework 2.0、3.0、3.5 Runtime (CLR) All are 2.0,.Net Framework from 2.0 Upgrade to 3.5 when , The core library mscorlib There is no upgrade , Still in use 2.0 Version of mscorlib. and Func The type is 3.5 Version introduced , Put in assembly System.Core in .

The output above is in CLR by 2.0 The output result of , Then if I put the program CLR Change it to 4.0, What will happen ?

Create a new one APP.config The configuration file ( If it already exists, there is no need to create ), Modify the configuration as follows :

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0"/>
  </startup>
</configuration>

By setting supportedRuntime Of version=4.0, Set the runtime of the current program to 4.0. Take another look at the output :

Both types of assemblies become mscorlib, The versions have become 4.0. Why is this so ?

because .NET When looking for an assembly, it is based on the current runtime , Not based on the current .NET Framework To find the version of .

But there is a problem ,Func Types are indeed defined in System.Core In the assembly , Why when the runtime is set to 4.0 when , The assembly in which it is located becomes mscorlib What about it ? As for how to look at Func Type defines in which assembly , Let's switch to Func The definition of type , Look at the top assembly information to know .

We find .Net Framework 4.0 Below System.Core.dll Assembly , The path of this assembly on my computer is :C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0. open ildasm.exe, This is a .NET Its own decompilation tool software , The path on my computer is :C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools.

Use this software to open 4.0 Version System.Core.dll, double-click MANIFEST, Search for System.Func, You can see the following code :

.class extern forwarder System.Func`1
{
  .assembly extern mscorlib
}

This code means , When you want to find System.Func Type , To go to mscorlib Find in the assembly . This is equivalent to a redirect , So when you get its assembly , Go first System.Core Assembly lookup , Then go to mscorlib assembly .

Add here ,.Net Framework 4.x The runtime is CLR4.0.

 

 

 

 

 

 

版权声明
本文为[begeneral]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230554082387.html