简体   繁体   English

使用.NET 4.5构建的应用程序可以在.NET 4.0上运行吗?

[英]Can application built with .NET 4.5 run on .NET 4.0?

My project is targeting to .NET 4.5. 我的项目是针对.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.5方法,所以它实际上只在安装了.NET 4.0的机器上正常工作。

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". 然后当我在4.0机器上运行这个.NET 4.5程序时,它失败了“System.TypeLoadException:无法从程序集mscorlib加载类型'System.Runtime.CompilerServices.ExtensionAttribute'”。 The famous ExtensionAttribute program that has been documented well here . 着名的ExtensionAttribute程序已在此处记录良好。

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. 然后程序将仅在.NET 4.0上运行时抛出异常。

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

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). 例如,ILMerge(当使用链接中记录的正确/ targetplatform选项时)实际上将ExtensionAttribute从mscorlib更改为System.Core(如果项目是针对.NET 4.0(安装了4.5))。 But it doesn't seem to work on the project targeted to .NET 4.5. 但它似乎不适用于针对.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. 但只是想知道是否有人有其他想法,因为它是如此接近。

Thanks. 谢谢。

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. 在某些情况下确实有效因为4.5是4.0的替代品,但它一般不会起作用。 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. 反射类型不是4.5中移动的唯一类型。

My project is targeting to .NET 4.5. 我的项目是针对.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.5方法,所以它实际上只在安装了.NET 4.0的机器上正常工作。

If this is the case, you could just change your application to target .NET 4.0. 如果是这种情况,您可以将应用程序更改为目标.NET 4.0。 This should allow it to run safely on a machine with only .NET 4 installed. 这应该允许它在只安装了.NET 4的机器上安全运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 允许使用.NET 2.0构建的C#应用​​程序在.NET 4.0 / 4.5上运行 - Allow C# application built with .NET 2.0 to run on .NET 4.0/4.5 将.net 4.5应用程序降级到4.0 - downgrade .net 4.5 application to 4.0 .NET 4.5到.NET 4.0上的WPF应用程序 - Wpf application on .NET 4.5 to .NET 4.0 EDMX .NET 4.5到4.0? - EDMX .NET 4.5 to 4.0? 在具有.net 4.0的计算机上-是否可以测试某个应用程序将在.net 3.5上运行? - On a computer with .net 4.0 - Can one test that an application will run on .net 3.5? .NET 4.5应用程序在.NET 4.0系统上的功能特定兼容性 - Feature-specific compatibility of a .NET 4.5 application on a system with .NET 4.0 c#在.net 4.0应用程序中使用.net 4.5编译的dll - c# use .net 4.5 compiled dll, in a .net 4.0 application 如何使应用程序与.NET 4.0和.NET 4.5兼容 - How to make an application compatible with .NET 4.0 and .NET 4.5 如果将框架替换为.NET 4.5,对于使用.NET 4.0的应用程序是否安全? - Is it safe for an application using .NET 4.0 if the framework is replaced with .NET 4.5? Asp.NET MVC应用程序是使用.net Framework 4.5版构建的,我想在更高的.net框架4.7.2上运行该应用程序 - An Asp.NET MVC application is built with .net framework version 4.5, I want to run the application on a higher .net framework that is 4.7.2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM