When linking a .NET 2.0 Managed Assembly from a .NET 4.0 Application, which framework is used?

AlexWilson picture AlexWilson · Jul 16, 2010 · Viewed 9.2k times · Source

If I have a 2.0 CLR assembly (pure managed code, no mixed mode issues) that I need to link to from a 4.0 CLR Application, does the 2.0 code run on the 2.0 CLR or 4.0.

Basically, is there any risk of 4.0 breaking changes affecting the 2.0 code?

Answer

Jason Short picture Jason Short · Oct 8, 2010

The answer above is incorrect. You do get side by side with the full frameworks. A .Net 2 APPLICATION (note that means EXE, not library) will not auto promote to .Net 4.

But if a .Net 4 application loads a .Net 2 assembly it is loaded into the same runtime (otherwise how could they share information). The .Net 2 assembly is loaded into the .net 4 runtime using a compatibility mode that is supposed to minimize breakage in changes (mostly for security changes in .Net 4).

A .Net 2 assembly cannot reference a .Net 4 assembly because it would not have features.

The ONLY exception to this that I know of is if you load a .Net assembly from a C++ app. The C++ application can load and host two runtimes. It could have a .Net 2 assembly and a .Net 4 assembly loaded, but they would not be able to talk to each other directly. This is how CLR Procs work in SQL Server. You can have a .Net 2 CLR Proc and a .Net 4 CLR Proc that do not communicate, but are both loaded on the server.

There was a great article on MSDN Magazine about hosting the .Net framework recently, but I can't find it now. Maybe someone else can post the link.

So you should be able to load just about any .Net 2 assembly into a .Net 4 executable without much problem. The only problems I have seen are with security permissions.