是否可以在 XP 上运行 .NET 4.5 应用程序? [英] Is it possible to run a .NET 4.5 app on XP?

查看:61
本文介绍了是否可以在 XP 上运行 .NET 4.5 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我已阅读以下内容:

First, I have read the following:

所以,从最后一个要点开始,我真的认为没有办法解决这个问题,但我必须看看我是否能得到一个明确的答案,因为我的团队想从 .NET 4.0 升级到 .NET 4.5.但是,我们必须支持 XP.

So, from the last bullet, I really think there is no way around this, but I had to see if I could get a definitive answer as my team would like to upgrade from .NET 4.0 to .NET 4.5. However, we have to support XP.

如果我们想支持 XP 就没有可能去 .NET 4.5 吗?

我唯一能想到的是创建两个单独的解决方案,但如果我们使用 .NET 4.5 功能,代码库将不得不分开.

The only thing I could think of is creating two separate solutions, but then the codebases would have to diverge if we used .NET 4.5 features.

因此,我正在寻找一些我找不到而其他人可能已经知道的惊人解决方法.

So, I am looking for some amazing workaround that I could not find and others maybe already know.

推荐答案

我对发布这个答案犹豫不决,它实际上在技术上是可行的,但在实践中效果不佳.CLR 和核心框架程序集的版本号在 4.5 中没有改变.您的目标仍然是 CLR 的 v4.0.30319,并且框架程序集版本号仍然是 4.0.0.0.当您使用像 ildasm.exe 这样的反汇编程序查看程序集清单时,唯一与众不同的是存在 [TargetFramework] 属性,该属性表示需要 4.5,必须对其进行更改.实际上没那么容易,它是由编译器发出的.

I hesitate to post this answer, it is actually technically possible but it doesn't work that well in practice. The version numbers of the CLR and the core framework assemblies were not changed in 4.5. You still target v4.0.30319 of the CLR and the framework assembly version numbers are still 4.0.0.0. The only thing that's distinctive about the assembly manifest when you look at it with a disassembler like ildasm.exe is the presence of a [TargetFramework] attribute that says that 4.5 is needed, that would have to be altered. Not actually that easy, it is emitted by the compiler.

最大的区别不是那么明显,微软对程序集的可执行标头进行了早就应该进行的更改.它指定可执行文件与哪个版本的 Windows 兼容.XP属于上一代Windows,从Windows 2000开始,主版本号5.Vista是本代的开始,主版本号6.

The biggest difference is not that visible, Microsoft made a long-overdue change in the executable header of the assemblies. Which specifies what version of Windows the executable is compatible with. XP belongs to a previous generation of Windows, started with Windows 2000. Their major version number is 5. Vista was the start of the current generation, major version number 6.

.NET 编译器一直将最低版本号指定为 4.00,即 Windows NT 和 Windows 9x 的版本.您可以通过在程序集上运行 dumpbin.exe/headers 来查看这一点.示例输出如下所示:

.NET compilers have always specified the minimum version number to be 4.00, the version of Windows NT and Windows 9x. You can see this by running dumpbin.exe /headers on the assembly. Sample output looks like this:

OPTIONAL HEADER VALUES
             10B magic # (PE32)
            ...
            4.00 operating system version
            0.00 image version
            4.00 subsystem version              // <=== here!!
               0 Win32 version
            ...

.NET 4.5 中的新功能是编译器将该子系统版本更改为 6.00.一个过期的改变很大程度上是因为 Windows 关注这个数字,而不仅仅是检查它是否足够小.它还打开 appcompat 功能,因为它假定该程序是为在旧版本的 Windows 上工作而编写的.这些功能会带来麻烦,尤其是 Windows 对 Aero 中窗口大小的判断方式很麻烦.当它可以看到程序设计为在具有 Aero 的 Windows 版本上运行时,它就停止在 Aero 窗口的宽边框上撒谎.

What's new in .NET 4.5 is that the compilers change that subsystem version to 6.00. A change that was over-due in large part because Windows pays attention to that number, beyond just checking if it is small enough. It also turns on appcompat features since it assumes that the program was written to work on old versions of Windows. These features cause trouble, particularly the way Windows lies about the size of a window in Aero is troublesome. It stops lying about the fat borders of an Aero window when it can see that the program was designed to run on a Windows version that has Aero.

您可以更改该版本号并将其设置回 4.00,方法是使用/subsystem 选项在您的程序集上运行 Editbin.exe.这个答案展示了一个构建后事件示例.

You can alter that version number and set it back to 4.00 by running Editbin.exe on your assemblies with the /subsystem option. This answer shows a sample postbuild event.

然而,好消息到此为止,一个重要的问题是 .NET 4.5 与 .NET 4.0 不太兼容.到目前为止,最大的障碍是类从一个程序集转移到另一个程序集.最值得注意的是,这发生在 [Extension] 属性上.以前在 System.Core.dll 中,它在 .NET 4.5 中移至 Mscorlib.dll.如果你声明你自己的扩展方法,那在 XP 上就是一个问题,你的程序说要在 Mscorlib 中查找属性,由 System.Core 参考程序集的 .NET 4.5 版本中的 [TypeForwardedTo] 属性启用.但是当你在 .NET 4.0 上运行你的程序时它不存在

That's however about where the good news ends, a significant problem is that .NET 4.5 isn't very compatible with .NET 4.0. By far the biggest hang-up is that classes were moved from one assembly to another. Most notably, that happened for the [Extension] attribute. Previously in System.Core.dll, it got moved to Mscorlib.dll in .NET 4.5. That's a kaboom on XP if you declare your own extension methods, your program says to look in Mscorlib for the attribute, enabled by a [TypeForwardedTo] attribute in the .NET 4.5 version of the System.Core reference assembly. But it isn't there when you run your program on .NET 4.0

当然,没有什么可以帮助您停止使用仅在 .NET 4.5 上可用的类和方法.当你这样做时,你的程序将在 4.0 上运行时失败并出现 TypeLoadException 或 MissingMethodException

And of course there's nothing that helps you stop using classes and methods that are only available on .NET 4.5. When you do, your program will fail with a TypeLoadException or MissingMethodException when run on 4.0

只要以 4.0 为目标,所有这些问题就会消失.或者打破这种僵局并停止支持 XP,这是程序员通常无法做出的业务决策,但通过指出它引起的麻烦当然可以鼓励.当然,必须支持古老的操作系统的成本非零,只是测试工作量很大.Windows 兼容性通常不被管理层认可,除非向他们指出,否则它具有传奇色彩.将这笔费用转嫁给客户,他们往往会更快做出正确的决定:) 但我们无法为您提供帮助.

Just target 4.0 and all of these problems disappear. Or break that logjam and stop supporting XP, a business decision that programmers cannot often make but can certainly encourage by pointing out the hassles that it is causing. There is of course a non-zero cost to having to support ancient operating systems, just the testing effort is substantial. A cost that isn't often recognized by management, Windows compatibility is legendary, unless it is pointed out to them. Forward that cost to the client and they tend to make the right decision a lot quicker :) But we can't help you with that.

这篇关于是否可以在 XP 上运行 .NET 4.5 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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