6

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.

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.

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);

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.

Thanks.

4

1 回答 1

14

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

我的项目针对.NET 4.5。它不使用任何新的 4.5 方法,因此它实际上可以在仅安装 .NET 4.0 的机器上正常工作。

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

于 2013-04-18T17:50:33.023 回答