2

I have a MVC 3 application that targets .net 4.0 It has a dozen or so projects also targeting .net 4.0

I have created a new project for the solution targeting .net 4.5 that uses reflection (there are a couple of nice bits of functionality in 4.5 that I want to use). When I try to compile I get an error saying that the 'type or namespace (new 4.5 proj) could not be found, are you missing a directive or an assembly reference'.

Is it possible to have a reference to an assembly targeting .net 4.5 in a project that targets 4.0?

atreeon
  • 21,799
  • 13
  • 85
  • 104
  • In theory I would say you could, because .Net 4.5 is a drop in replacement for .Net 4.0 so the runtimes are the same. I would recommend against it because obviously you're running the risk of calling code that doesn't exist in your 4.5 project. Is it a BCL feature you want in 4.5 or a language feature? – CodingGorilla Sep 09 '13 at 14:21
  • http://stackoverflow.com/questions/16856474/upgrade-to-net-4-5-causes-assembly-to-fail – Vladimir Sep 09 '13 at 14:21
  • The compiler is applying the two-by-four to your forehead. If you think it is wrong then do try to imagine what would happen when your code calls a method that's only available in .NET 4.5 but runs on a machine that has 4.0 installed. Smack. – Hans Passant Sep 09 '13 at 14:30
  • CodingGorilla - it's a BCL feature, reflection of attributes seems to be a bit nicer and has a few more helper functions – atreeon Sep 09 '13 at 14:31

3 Answers3

1

Is it possible to have a reference to an assembly targeting .net 4.5 in a project that targets 4.0?

No. It shouldn't be. There could be some features that are used in .Net 4.5 assembly which are not part of .Net 4.0, In that particular case, it should fail.

user2711965
  • 1,795
  • 2
  • 14
  • 34
  • 1
    This seems to contradict what's explained here: stackoverflow.com/a/12631425/881111 – marknuzz Feb 12 '14 at 03:13
  • @Nuzzolilo, these are two different things, one is using an assembly developed in 4.5 in a project of 4.0, The other is running 4.0 on a machine which has 4.5 – user2711965 Feb 12 '14 at 14:05
1

I guess you could just set your project to .net 4.5, but if you would like to keep it as 4.0, take a look at this link might help you

Community
  • 1
  • 1
Rômulo Spier
  • 231
  • 1
  • 11
0

Yes, it is possible. Just open the .csproj file for the .Net 4.0 assembly giving the assembly load error in a text editor and set this for the .Net 4.5 assembly reference:

      <SpecificVersion>True</SpecificVersion>
Berend Engelbrecht
  • 1,420
  • 15
  • 11