C# 是否可以在 XP 上运行 .NET 4.5 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17499351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:39:34  来源:igfitidea点击:

Is it possible to run a .NET 4.5 app on XP?

c#.net.net-4.0compatibility.net-4.5

提问by Justin Pihony

First, I have read the following:

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

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.

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

Is there no possibility of going to .NET 4.5 if we want to support XP?

如果我们想支持 XP,就没有可能去 .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.

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

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

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

采纳答案by Hans Passant

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.

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

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.

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

.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:

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

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

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.

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

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 answershows a sample postbuild event.

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

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 与 .NET 4.0 不太兼容。到目前为止,最大的障碍是类从一个程序集转移到另一个程序集。最值得注意的是,这发生在 [Extension] 属性上。以前在 System.Core.dll 中,它在 .NET 4.5 中移至 Mscorlib.dll。如果您声明自己的扩展方法,那在 XP 上会很麻烦,您的程序说要在 Mscorlib 中查找该属性,该属性由 System.Core 参考程序集的 .NET 4.5 版本中的 [TypeForwardedTo] 属性启用。但是当你在 .NET 4.0 上运行你的程序时它不存在

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

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

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.

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

回答by Oztaco - Reinstate Monica C.

Sadly, no, you can't run 4.5 programs on XP.

遗憾的是,不,您不能在 XP 上运行 4.5 程序。

And the relevant post from that Connect page:

以及来自该 Connect 页面的相关帖子:

Posted by Microsoft on 23/03/2012 at 10:39
Thanks for the report. This behavior is by design in .NET Framework 4.5 Beta. The minimum supported operating systems are Windows 7, Windows Server 2008 SP2 and Windows Server 2008 R2 SP1. Windows XP is not a supported operating system for the Beta release.

Microsoft 于 23/03/2012 于 10:39 发表
感谢您的报告。此行为是 .NET Framework 4.5 Beta 中的设计使然。支持的最低操作系统是 Windows 7、Windows Server 2008 SP2 和 Windows Server 2008 R2 SP1。Windows XP 不是 Beta 版支持的操作系统。

回答by luiseduardohd

Try mono:

尝试单声道:

http://www.go-mono.com/mono-downloads/download.html

http://www.go-mono.com/mono-downloads/download.html

This download works on all versions of Windows XP, 2003, Vista and Windows 7.

此下载适用于所有版本的 Windows XP、2003、Vista 和 Windows 7。

回答by guest

The Mono project dropped Windows XP support and "forgot"to mention it. Although they still claim Windows XP SP2 is the minimum supported version, it is actually Windows Vista.

Mono 项目放弃了对 Windows XP 的支持并“忘记”提及它。尽管他们仍然声称 Windows XP SP2 是最低支持版本,但它实际上是 Windows Vista。

The last version of Mono to support Windows XP was 3.2.3.

支持 Windows XP 的 Mono 的最后一个版本是 3.2.3。

回答by Sandy

Last version to support windows XP (SP3) is mono-4.3.2.467-gtksharp-2.12.30.1-win32-0.msi and that doesnot replace .NET 4.5 but could be of interest for some applications.

支持 Windows XP (SP3) 的最新版本是 mono-4.3.2.467-gtksharp-2.12.30.1-win32-0.msi,它不会取代 .NET 4.5,但可能对某些应用程序感兴趣。

see there: https://download.mono-project.com/archive/4.3.2/windows-installer/

请参阅:https: //download.mono-project.com/archive/4.3.2/windows-installer/