使用 .NET 4.5 构建的应用程序能否在 .NET 4.0 上运行? [英] Can application built with .NET 4.5 run on .NET 4.0?

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

问题描述

我的项目面向 .NET 4.5.它不使用任何新的 4.5 方法,因此它实际上在仅安装了 .NET 4.0 的机器上运行良好.

My project is targeting to .NET 4.5. It doesn't use any new 4.5 methods, so it actually works fine on the machine with only .NET 4.0 installed.

这一切都很好,直到我添加了一些扩展方法和反射.然后当我在 4.0 机器上运行这个 .NET 4.5 程序时,它失败了System.TypeLoadException:无法从程序集 mscorlib 加载类型‘System.Runtime.CompilerServices.ExtensionAttribute’".著名的 ExtensionAttribute 程序已被很好地记录下来 此处.

This is all good until I added some extension methods and reflection. Then when I ran this .NET 4.5 program on the 4.0 machine, it failed with "System.TypeLoadException: Could not load type 'System.Runtime.CompilerServices.ExtensionAttribute' from assembly mscorlib". The famous ExtensionAttribute program that has been documented well here.

另一种快速测试方法是添加以下行.那么该程序仅在 .NET 4.0 上运行时将抛出异常.

Another quick way to test this is to add the following line. Then the program will throw the exception when running on .NET 4.0 only.

Console.WriteLine(typeof(System.Runtime.CompilerServices.ExtensionAttribute).Assembly.FullName);

我想知道是否有办法解决它.例如,如果项目的目标是 .NET 4.0(安装了 4.5),ILMerge(当使用链接中记录的正确/targetplatform 选项时)实际上将 ExtensionAttribute 从 mscorlib 更改为 System.Core.但它似乎不适用于针对 .NET 4.5 的项目.

I'm wondering if there is a way to work around it. For example, the ILMerge (when using the correct /targetplatform option as documented in the link) actually changes the ExtensionAttribute from mscorlib to System.Core if the project is target to .NET 4.0 (with 4.5 installed). But it doesn't seem to work on the project targeted to .NET 4.5.

我知道这是一个远景.但只是想看看是否有人有其他想法,因为它是如此接近.

I know this is a long shot. But just want to see if anyone has other ideas since it was so close.

谢谢.

推荐答案

一般来说,这行不通.它在某些情况下确实有效,因为 4.5 是 4.0 的就地替代品,但一般情况下它不会起作用.我个人已经看到类型转移到不同程序集中的问题,并且绑定设置不正确,就像您看到的一样.反射类型并不是 4.5 中唯一移动的类型.

In general, this will not work. It does work in some cases since 4.5 is an in place replacement for 4.0, but it's not going to work in general. I've, personally, seen problems with types that have moved into different assemblies, and the bindings aren't setup correctly, just like you're seeing. The reflection types aren't the only types that were moved in 4.5.

我的项目面向 .NET 4.5.它不使用任何新的 4.5 方法,因此它实际上在仅安装了 .NET 4.0 的机器上运行良好.

My project is targeting to .NET 4.5. It doesn't use any new 4.5 methods, so it actually works fine on the machine with only .NET 4.0 installed.

如果是这种情况,您可以将应用程序更改为面向 .NET 4.0.这应该允许它在仅安装了 .NET 4 的机器上安全运行.

If this is the case, you could just change your application to target .NET 4.0. This should allow it to run safely on a machine with only .NET 4 installed.

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

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