Force NuGet to reference a .net framework 4.0 assembly in a 4.5 project?

679 views Asked by At

I have two projects in my solution--one targets .net 4.0, the other targets .net 4.5. They both reference the same NuGet package which contains both 4.0 and 4.5 binaries.

How can I get NuGet to reference the same version--4.0--in both projects?

1

There are 1 answers

0
Josef Ginerman On

You can exclude the folder of the framework that you don't want to use (ExcludeAssets), and edit the PackageTargetFallback in your project.

In your project's csproj file:

<PackageTargetFallback Condition="'$(TargetFramework)'=='net45'">
    $(PackageTargetFallback);net40
</PackageTargetFallback >

When referencing the package:

<PackageReference Include={package-ID} Version={version} ExcludeAssets="lib/$(TargetFramework)"/>

This way the package won't bring the binaries that you don't want, and the ones you want will be compatible.

Note: Taking dlls with a different target framework is not recommended.