[英]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.