参考程序集文件夹和具有相同版本的不同程序集 [英] Reference Assemblies folder and different assemblies with the same version

查看:21
本文介绍了参考程序集文件夹和具有相同版本的不同程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 System.Runtime.Serialization 程序集的项目.我正在使用该程序集中的 DataContractSerializer 类型,但我遇到了问题.有两个程序集:

I have a project that uses System.Runtime.Serialization assembly. I am using the type DataContractSerializer from that assembly, but I have a problem. There are two assemblies:

C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.0System.Runtime.Serialization.dll

C:Program Files (x86)Reference AssembliesMicrosoftFramework.NETFrameworkv4.0System.Runtime.Serialization.dll

C:WindowsMicrosoft.netFrameworkv4.0.30319System.Runtime.Serialization.dll

C:WindowsMicrosoft.netFrameworkv4.0.30319System.Runtime.Serialization.dll

它们都具有相同的版本 - v4.0.30319.第一个大小为 429kb,第二个大小为 1037kb.我使用反射器查看类列表,第一个没有我需要的类 (DataContractSerializerSettings).但是,第二个确实有.

Both of them have the same version - v4.0.30319. The first one have 429kb size, and the second one 1037kb. I used reflector to see the list of classes, and the first one doesn't have the class that I need (DataContractSerializerSettings). However, the second one does have it.

为什么那个程序集的大小和类有很大的不同?如果我使用第二个而不是第一个可以吗?

Why are there some big difference in size and classes for that assembly? Will it be ok, if I use the second one, instead of the first?

推荐答案

.NET 4.0 版对框架引用程序集的完成方式进行了重大更改.以前,引用程序集是运行时程序集的简单副本,即存储在 GAC 中的程序集.然而,这引起了一些痛苦的问题.值得注意的是 WaitHandle.WaitOne(int) 重载,它是在 .NET 2.0 Service Pack 2 更新(又名 .NET 3.5)中添加的.程序员使用它时没有注意到它是一个添加方法,mscorlib 程序集版本号仍然是 2.0.0.0.但是后来发现他们的程序在未打补丁的 .NET 2.0 版本上运行时失败了.非常讨厌的 kaboom,MissingMethodException 没有提示为什么会丢失这样一个常用方法.

.NET version 4.0 made a big change in the way framework reference assemblies are done. Previously, the reference assembly was a simple copy of the runtime assembly, the one stored in the GAC. That however caused some painful problems. Notable is the WaitHandle.WaitOne(int) overload, it was added in the .NET 2.0 Service Pack 2 update (aka .NET 3.5). Programmers used it without noticing that it was an added method, the mscorlib assembly version number was still 2.0.0.0. But then discovered their program failed when running on an unpatched version of .NET 2.0. Very nasty kaboom, MissingMethodException without a hint why such a common method could be missing.

为了防止这种损坏,.NET 4.0 参考程序集在您发现的%programfiles%Reference Assemblies"目录中被分开保存.而且它们是特殊的程序集,它们只包含去掉所有 IL 的元数据.这就是为什么组件要小得多.

To prevent this kind of breakage, the .NET 4.0 reference assemblies are kept separate, in the "%programfiles%Reference Assemblies" directory as you found out. And they are special assemblies, they only contain the metadata with all the IL stripped out. Which is why the assembly is so much smaller.

Microsoft 现在可以改进 .NET 4 代码并添加公共类和方法,而不会造成这种破坏.自 4.0 原始版本发布以来,4.01、4.02 和 4.03 更新已经发布.

Microsoft now can improve the .NET 4 code and add public classes and methods without causing this kind of breakage. And have done so profusely, updates 4.01, 4.02 and 4.03 have shipped since the original 4.0 release.

您在使用 DataContractSerializerSetting 类时遇到问题的原因很容易解释,只是它没有出现在参考程序集中.它被添加了,可能是在这些增量更新之一中.你不应该尝试,你的程序会在没有更新的机器上中断.您应该等到 .NET 4.5,即将其添加到参考程序集的版本.如果您真的愿意,您可以调用 DLL Hell.

The reason you are having trouble with the DataContractSerializerSetting class is thus easily explained, it just doesn't appear in the reference assembly. It got added, probably in one of those incremental updates. And you should not try, your program will break on a machine that doesn't have the update. You should wait until .NET 4.5, the version that added it to the reference assembly. You can invoke DLL Hell if you really want to.

这篇关于参考程序集文件夹和具有相同版本的不同程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆