ILDASM,mscorlib程序和System.Runtime反编译的差异取决于目录反编译、差异、目录、程序

2023-09-03 17:38:03 作者:峩想要旳未来、

我一直在玩弄ILDASM并且已经注意到:

I have been playing around with ILDasm and have noticed that:

反编译 C:\ WINDOWS \ Microsoft.NET \框架\ v4.0.30319 \ System.Runtime.dll(36KB)只返回一个清单文件。反编译 C:\ Program Files文件(x86)的\参考大会\微软\框架\ .NETCore \ V4.5 \ System.Runtime.dll(114KB)返回清单和所有类型的组件。

Decompiling C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Runtime.dll (36KB) simply returns a manifest file. Decompiling C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.dll (114KB) returns the manifest and all types in the assembly.

反编译 C:\ Program Files文件(x86)的\参考大会\微软\框架\ .NETCore \ V4.5 \ mscorlib.dll中(38KB)简单返回一个清单文件和反编译 C:\ WINDOWS \ Microsoft.NET \框架\ v4.0.30319 \ mscorlib.dll中(5171KB)返回清单和所有类型的组件。

Decompiling C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\mscorlib.dll (38KB) simply returns a manifest file and decompiling C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll (5171KB) returns a manifest and all types in the assembly.

我找不到为什么组件是建立在这样一种方式的任何信息。

I cannot find any information on why the assemblies are built in such a way.

什么是这两个组件目录的差异,为什么对文件系统的两个副本?为什么重复这两个组件类型?无论System.Runtime和mscorlib中包含了许多相同类型的。

What are the differences in the two assembly directories and why have two copies on the filesystem? Why are types duplicated in both assemblies? Both System.Runtime and mscorlib contain most of the same types.

推荐答案

在C中发现的组件:\ Program Files文件(x86)的\引用程序集是的引用程序集的。他们是比较特殊的.NET 4.0及以上,它们不包含任何code。它是从装配由Microsoft用于建造这些组件的专用工具剥离。没关系,编译器只使用元数据,在这样的组装编译code。在运行时,你得到的非常的不同组件,它从海关总署获得。

The assemblies you found in C:\Program Files (x86)\Reference Assemblies are reference assemblies. They are rather special in .NET 4.0 and up, they don't contain any code. It was stripped from the assembly by a special tool that Microsoft uses to build these assemblies. Does not matter, a compiler only uses the metadata in such an assembly to compile your code. At runtime you get a very different assembly, it is retrieved from the GAC.

请注意,你会发现的许多的System.Runtime.dll在该目录下复制,特别是.NETPortable目录中有很多的配置文件,每一个都有自己的参考汇编副本。

Do note that you'll find many copies of System.Runtime.dll in that directory, the .NETPortable directory in particular has lots of profiles, each with their own copy of that reference assembly.

在C中发现的组件:\ WINDOWS \ Microsoft.NET \框架\ v4.0.30319大致的那些在GAC副本。大致来说,它们可以是4.0,4.5或4.5.1组件的副本。你应该永远使用这些组件的任何东西。虽然许多组件仍然有一个[的AssemblyVersion(4.0.0.0),其内容是完全不同的,特别是4.0〜4.5。你可以看到这回的文件中,在ExtensionAttribute类是一个很好的例子。在.NET 4.0中它生活在System.Core.dll的,4.5及以上,现在住在mscorlib.dll。更多类型这样。这些副本不应该是不在了,不幸的是传统的工具取决于它们在那里。在C ++ / CLI编译器特别#using指令以及作为前一阵子得了这个毛病很多私人定制构建系统。非常,非常麻烦。

The assemblies you found in C:\Windows\Microsoft.NET\Framework\v4.0.30319 are roughly a copy of the ones in the GAC. Very roughly, they can be a copy of the 4.0, 4.5 or 4.5.1 assembly. You should never use these assemblies for anything. While many assemblies there still have an [AssemblyVersion("4.0.0.0")], their content is dramatically different, particularly between 4.0 and 4.5. You can see this back in the documentation, the ExtensionAttribute class is a good example. In .NET 4.0 it lives in System.Core.dll, in 4.5 and up it now lives in mscorlib.dll. Many more types like this. These copies should not be around anymore, unfortunately legacy tooling depends on them being there. In particular the #using directive in the C++/CLI compiler as well as many private custom build systems that got this wrong a while ago. Very, very troublesome.

所以看在GAC大会知道的真正的发生在运行时。一个大的变化出现,以及因为.NET 4.0,现在住在另一个目录。 pviously $ P $在C:\ WINDOWS \组装,现在在C:\ WINDOWS \ microsoft.net \组装。而最明显的变化,它不再具有外壳扩展,从导航到文件,在该目录拦你。您可以直接浏​​览GAC文件夹结构。这是一丁点令人费解,因为包含非托管code(如mscorlib.dll中)组件都存储在一个单独的目录。一看,看,你会不会有大问题搞清楚这个计划。

So look at the assemblies in the GAC to know what really happens at runtime. A big change there as well since .NET 4.0, it now lives in another directory. Previously in c:\windows\assembly, now in c:\windows\microsoft.net\assembly. And the most visible change, it no longer has the shell extension that stopped you from navigating to files in that directory. You can directly navigate the GAC folder structures. It is a wee bit convoluted since assemblies that contain unmanaged code (like mscorlib.dll) are stored in a separate directory. Have a look-see, you'll have little trouble figuring out the scheme.

本C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Runtime\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Runtime.dll组装你会发现确实是有相当空。你可能错过了最重要的细节在清单不过。它包含的很多的属性[TypeForwardedTo。剪断的那些,你会发现有:

The C:\Windows\Microsoft.NET\assembly\GAC_MSIL\System.Runtime\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Runtime.dll assembly you'll find there is indeed rather empty. You probably missed the most important detail in the manifest however. It contains a lot of [TypeForwardedTo] attributes. A snip of the ones you'll find there:

[assembly: TypeForwardedTo(typeof(Action))]
[assembly: TypeForwardedTo(typeof(Action<>))]
[assembly: TypeForwardedTo(typeof(Action<,,,,,,,,,>))]
[assembly: TypeForwardedTo(typeof(Action<,,,,,,,,,,>))]
[assembly: TypeForwardedTo(typeof(Action<,,,,,,,,,,,>))]
[assembly: TypeForwardedTo(typeof(Action<,,,,,,,,,,,,>))]
[assembly: TypeForwardedTo(typeof(Action<,,,,,,,,,,,,,>))]
[assembly: TypeForwardedTo(typeof(Action<,,,,,,,,,,,,,,>))]
[assembly: TypeForwardedTo(typeof(Action<,,,,,,,,,,,,,,,>))]
// etc, many more

也许你可以看看现在发生了什么事情,System.Runtime.dll不包含任何code在所有。这是一个适配器转发类型从一个组件到另一个。 .NET的桌面版转发类型MSCORLIB.DLL,System.dll中,System.ComponentModel.Composition和System.Core程序。

Maybe you can see what's going on now, System.Runtime.dll does not contain any code at all. It is an adapter that forwards types from one assembly to another. The desktop version of .NET forwards types to mscorlib.dll, System.dll, System.ComponentModel.Composition and System.Core.

在previous一句桌面版是解释为什么这样做的关键。还有的许多的.NET框架版本,它们在System.Runtime不同的转发器。特别是一个大问题,你在商店或手机应用程序使用的框架版本。这是由其中的语言凸起的实现的主要机制。哪里System.String不必是.NET字符串在所有的,也可以是的HSTRING ,WinRT中的本地字符串类型。

"The desktop version" in the previous sentence is the key that explains why this was done. There are many .NET Framework versions, they have different forwarders in System.Runtime. Particularly a big deal for the framework version you use in a Store or Phone app. This is the primary mechanism by which the language projection is implemented. Where System.String doesn't have to be a .NET string at all, it can be an HSTRING, the native string type of WinRT.

而长话短说,这些适配器组件购买微软间接额外的水平。它可以帮助你编写.NET code,它与平台无关,并运行相同的无无论您是在桌面上,一个商店的应用程序中执行它,在使用Silverlight浏览器,在Xbox游戏机上,在手机上的变化。便携式类库项目模板是主要的受益者。

Rather long story short, these adapter assemblies buys Microsoft an extra level of indirection. It helps you to write .NET code that is platform agnostic and runs the same without changes whether you execute it on the desktop, a Store app, in the browser with Silverlight, on the XBox game console, on a phone. The Portable Class Library project template is the principal beneficiary.