0

I have a Net7.0 project that can run on Windows or Linux. I'd like it to reference a Net7.0-windows nuget package, and make the decision on whether to use the assembly/types it contains at runtime: e.g.

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
    // use Net7.0-windows package functionality
}
else
{
    // can't use it - do something else
}

So the Windows-only package would always be deployed (copied to bin), but only "executed" on Windows (ignoring questions about JIT, static initializers, etc).

Is there any way to do this using the standard tooling?

I thought I could add the PackageReference and ignore the NU1701. Unfortunately nuget helpfully resolves to the NetFramework version of the package, instead of the Net6.0-windows version. Even if I set <AssetTargetFallback>net6.0-windows</AssetTargetFallback>

I suspect the only sane answer is to multi-target myproject to be net6.0;net6.0-windows (pushing the decision about the package to compile-time) but for various reasons I'd prefer not to do that.

Rob
  • 4,327
  • 6
  • 29
  • 55
  • Which NuGet package are you referencing? – Dai Mar 26 '23 at 19:50
  • @Dai It's a company internal package, but one I have little control over. Obviously if the package targeted net6.0 (perhaps throwing PNSE on Linux) it would be easy. – Rob Mar 27 '23 at 07:45

0 Answers0