Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you specify different dependencies and symbols when multi targeting a .NET Standard class library?

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.

like image 274
Gigi Avatar asked Oct 14 '25 03:10

Gigi


1 Answers

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.

like image 137
svick Avatar answered Oct 17 '25 19:10

svick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!