Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compile a .NET 4.5 app with a reference to .NET 4.5.2 assembly?

I discovered that an app targeting 4.5 can load and run an assembly targeting 4.5.2 - but I can't compile such a setup!

Question: How can I compile a .NET 4.5 app with a reference to .NET 4.5.2 assembly?

Experiment details:

  • MyLib.csproj - Targets 4.5.2
  • MyApp.csproj - Targets 4.5, has project reference to MyLib.csproj

Compiling in either Visual Studio or MSBuild fails with:

warning MSB3274: The primary reference "(...)\MyLib.dll" could not be resolved because it was built against the ".NETFramework,Version=v4.5.2" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.5".
error CS0246: The type or namespace name 'MyLib' could not be found (are you missing a using directive or an assembly reference?)

I tried to compile MyLib separately and reference its DLL, but got the same compilation errors.

However, I managed to get both to run:

  • Set both projects to 4.5, compile, copy the result to c:\test\MyApp.exe
  • Set both projects to 4.5.2, compile, copy the result to c:\test\MyLib.dll
  • Run MyApp.exe - runs ok!

Background: I'm preparing a transition of our large NuGet-managed multi-project from .NET 4.5 to 4.5.2. The announcement says It is a highly compatible, in-place update to the .NET Framework 4, 4.5 and 4.5.1, So I tried what would happen if we upgraded some of our (NuGet-managed) refs to 4.5.2, and non-upgraded projects tried to consume them.

like image 385
Jonathan Avatar asked Feb 05 '15 16:02

Jonathan


People also ask

Is .NET Framework 4.5 2 still supported?

NET Framework versions 4.5. 2, 4.6, and 4.6. 1 ended on April 26, 2022, so security fixes, updates, and technical support for these versions will no longer be provided.

Is .NET 4.6 backwards compatible?

The . NET Framework 4.5 and later versions are backward-compatible with apps that were built with earlier versions of the . NET Framework. In other words, apps and components built with previous versions will work without modification on the .

Can I install multiple versions of .NET Framework?

Microsoft designed the . NET Framework so that multiple versions of the framework can be installed and used at the same time. This means that there will be no conflict if multiple applications install different versions of the . NET framework on a single computer.

Does .NET 4.8 include 4.7 2?

if you have 4.8 then you have 4.7. 2 automatically.


1 Answers

For backwards compatible you can use multi-targeting. Open your .csproj file and change it like this:

 <PropertyGroup>
    <TargetFrameworks>net462;netstandard1.6</TargetFrameworks>
    <PreserveCompilationContext>true</PreserveCompilationContext>
  </PropertyGroup>

This is an example for .nuget package for two frameworks: net462 and netstandard1.6

At building it will create two folders with libraries:

  • net462
  • netstandard1.6
like image 177
Timur Lemeshko Avatar answered Sep 29 '22 00:09

Timur Lemeshko