Can an application built with .NET 4.5 run on .NET 4.0?

My project targets .NET 4.5. It does not use any new methods 4.5, so in fact it works fine on a computer with .NET 4.0 installed.

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

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

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

I am wondering if there is a way around this. For example, ILMerge (when using the correct / targetplatform option, as indicated in the link) actually changes the ExtensionAttribute from mscorlib to System.Core if the project targets .NET 4.0 (with 4.5 installed). But it does not work in a project oriented to .NET 4.5.

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

Thanks.

+6
source share
1 answer

In general, this will not work. It works in some cases, since 4.5 is a replacement for 4.0, but it will not work at all. I personally saw problems with types that moved to different assemblies, and the bindings are not configured correctly, as you can see. Reflection types are not the only types that have been moved to 4.5.

My project targets .NET 4.5. It does not use any new methods 4.5, so in fact it works fine on a computer with .NET 4.0 installed.

If so, you can simply change your application to target .NET 4.0. This should allow you to work safely on a computer with .NET 4 installed.

+14
source

All Articles