3

With project.json, it was possible to target different target frameworks, specifying different dependencies and conditional compilation symbols for each target framework.

I need to do the same thing with a .NET Standard class library using the .csproj project format. I know how to target multiple frameworks, but how do you specify different dependencies and conditional compilation symbols for each?

(In case "conditional compilation" is not clear, I need the ability to specify preprocessor directives in code such as #if NET452.)

A good example where this is useful is when dealing with appsettings. With the full .NET Framework, you need to reference System.Configuration.dll and work through ConfigurationManager. .NET Core configuration is a totally different beast.

Community
  • 1
  • 1
Gigi
  • 28,163
  • 29
  • 106
  • 188

1 Answers1

2

The accepted answer from your previous question which you linked to already has the answer: use <ItemGroup>s with Conditions testing for $(TargetFramework). Slightly modified code from that answer:

<ItemGroup Condition="'$(TargetFramework)' == 'net452'">
  <PackageReference Include="Microsoft.Azure.DocumentDB" Version="1.12.0" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard1.6'">
  <PackageReference Include="Microsoft.Azure.DocumentDB.Core" Version="1.1.0" />
</ItemGroup>

For preprocessor directives, you don't need to do anything. Directives like NET452 or NETSTANDARD1_6 are defined automatically.

Community
  • 1
  • 1
svick
  • 236,525
  • 50
  • 385
  • 514