Install nuget packages based on compiler flags / Target framework

Viewed 647

I am trying to provide my library for multiple framework versions of .NET (4.0 is relevant for this question).

My library requires async/await support and therefore the nuget package "Microsoft.Bcl.Async" is required. But i only want to reference it in .NET 4.0 builds, but not in >= .NET 4.5

Are there any easy ways to accomplish this goal?

1 Answers

Install nuget packages based on compiler flags / Target framework

To resolve this issue, you could use the PackageReference instead of packages.config in project files.

Tools->Options->NuGet Package Manager->General->Package Management->PackageReference

Then add the nuget package to the project, we could add a PackageReference condition to the nuget package, like:

<ItemGroup>
    <!-- ... -->
    <PackageReference Include="Microsoft.Bcl.Async" Version="1.0.168" Condition="'$(TargetFramework)' == 'net40'" />
    <!-- ... -->
</ItemGroup>

Check the document Adding a PackageReference condition for some more details.

Related