18

How can i make a 4.0 project have a 4.5 reference. In the unit tests, i cant build the solution and it's giving me this warning.

Warning 2 The primary reference "PR.Wallet" could not be resolved because it was built against the ".NETFramework,Version=v4.5.1" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.0". PR.Wallet.Tests

DevEstacion
  • 1,947
  • 1
  • 14
  • 28
  • 2
    Only thing you can do is upgrade the unit test project to 4.5, you will not be able to reference backwards. – Fedor Hajdu May 22 '14 at 12:53
  • Possible duplicate of [Error: This assembly is built by a runtime newer than the currently loaded runtime](https://stackoverflow.com/questions/5138735/error-this-assembly-is-built-by-a-runtime-newer-than-the-currently-loaded-runti) – Liam Feb 21 '19 at 15:40

4 Answers4

27

.Net frameworks (v2.0 or higher) are not forward compatible. . You can't reference a .Net 4.5 assembly in .Net 4.0 project.

See: Version Compatibility in the .NET Framework

You may also see: Version Compatibility

The degree of .NET Framework support for backward and forward compatibility is version-specific. The .NET Framework supports both backward and forward compatibility for applications created using version 1.1 only. It does not support forward compatibility in applications created using version 2.0. In the context of the .NET Framework, backward compatibility means that an application created using an early version of the .NET Framework will run on a later version. Conversely, forward compatibility means that an application created using a later version of the .NET Framework will run on an earlier version.

The .NET Framework provides a high degree of support for backward compatibility. For example, most applications created using version 1.0 will run on version 1.1 and applications using version 1.1 will run on version 2.0. The .NET Framework also supports forward compatibility for version 1.1 only. However, for forward compatibility you might need to modify an application so that the application runs as expected. Applications created with version 2.0 will not run on earlier versions of the .NET Framework. For both backward and forward compatibility, a change to the .NET Framework that helps improve security, correctness, or functionality might also raise compatibility issues.

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
7

Sounds like you need to change the framework of the library. And since it is only a unit tests project, I don't see why you wouldn't.

In Visual Studio:

  • Right-click on your project
  • Select Properties
  • Select the Application tab
  • Change the Target Framework to the desired framework

If you are not seeing .NET Framework 4.5.1 as an option there, ensure you have it installed.

Tim Sneed
  • 490
  • 3
  • 10
  • 1
    well if it doesnt work on the test, it wouldnt work on the actual right? – DevEstacion May 22 '14 at 14:00
  • The way I read the error is that your PR.Wallet is targeting 4.5.1 and the PR.Wallet.UnitTests is targeting 4.0. If you retarder the tests project to 4.5.1, you *should* be able to load the library. – Tim Sneed May 22 '14 at 14:02
7

You aren't able to reference a 4.5.1 assembly in a project that targets 4.0 . But ... you can call the method of a 4.5.1 assembly in a project that targets 4.0 by calling it dynamically, assuming 4.5.1 is installed:

var assembly= Assembly.LoadFrom(...);
var type = assembly.GetType(...);
var method = type.GetMethod(...);
var res = method.Invoke(null, args);

Note that there may be limitations to this approach, but I found it useful for calling Roslyn routines while still using VS2010.

JoelC
  • 3,664
  • 9
  • 33
  • 38
0

There may be exceptions. Based on my own experience, for example, some libraries like TagLib-Sharp 2.2.0 (which you can download from NuGet) perfectly allowed me to reference its .NET 4.5 DLL just fine from .NET 4.0 (Client of Full Profile) project running on Visual Studio 2010.

Additionally, calling some method from aforementioned referenced .NET 4.5 DLL did not emit any warning or error, build process went fine though, including at runtime. However, some methods/functions have failed, so it's a hit and miss but remember that referencing .NET 4.5 DLL was succeeded. So the answers and general rules above about "forward compatability" context clearly have some exceptions like TagLib-Sharp under particular circumstances.

Onur
  • 3
  • 2