Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does adding a reference to project targeting .NET Framework 4.0 fail?

We have two projects that are both class libraries. Project 1 is a VS 2008 project and targets the .NET Framework 3.5. Project 2 is a VS 2010 (release candidate) project that targets the .NET Framework 4.0. When I try to add a reference to Project 2 in Project 1, it fails with a less than informative error message. I know that if I change the target Framework for Project 2 to 3.5, then adding the reference will work. My question is, if I don't change the target frameworks, but convert Project 1 to VS 2010, will the referencing work? Stated another way, is there some inherent incompatiblity between class libraries targeting different framework versions, or is it failing for me because VS 2008 doesn't know about the 4.0 framework?

like image 327
Malcolm Post Avatar asked Apr 01 '10 03:04

Malcolm Post


Video Answer


2 Answers

Yes it is a problem. Much like you can't add a reference to a .NET 2.0 assembly from a .NET 1.0 or 1.1 project. The .NET 4.0 is a new runtime version of the framework (CLR). The .NET 3.5 and 3.0 releases both use the 2.0 runtime version of the framework (CLR).

You can always add a reference from an assembly built with a newer runtime to an assembly with an older runtime, but not the other way around.

From .NET 2.0 (3.0 and 3.5):
* Can reference .NET 2.0 (3.0 and 3.5) assemblies
* Can NOT reference .NET 4.0 assemblies

From .NET 4.0:
* Can reference .NET 4.0 assemblies
* Can reference .NET 2.0 (3.0 and 3.5) assemblies

like image 119
Adam Sills Avatar answered Sep 27 '22 19:09

Adam Sills


Framework versions 2, 3.0, 3.5 run on CLR version 2, while framework version 4 runs on CLR version 4: the version 2 CLR would not load an assembly that states it requires CLR 4, so Visual Studio is trying (badly, it sounds like) to tell you that. So, to answer your question, yes, if you update your project 1 to VS 2010 and change the target framework to version 4, (it does this by default for C++ projects, but not C#/VB.NET), it will be able to reference project 2.

like image 43
Simon Buchan Avatar answered Sep 27 '22 21:09

Simon Buchan