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.
The accepted answer from your previous question which you linked to already has the answer: use <ItemGroup>
s with Condition
s 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With