简体   繁体   中英

Can I use a .NET Standard 2.0 package in a classic .NET 4.6.1 application?

I'm a bit confusing how to share .net-standard 2 nuget package between .net core and .net-framework applications. For instance, I created .net-standard 2.0 package with only <TargetFramework>netstandard2.0</TargetFramework> . So I have only ../libs/netstandard2.0 folder in package. Can I install it as is into .net-framework 4.6.1. application (according to compatibility matrix I can)? Or should I make this package multi target <TargetFrameworks>netstandard2.0;net461</TargetFrameworks> ? What advantages here?

Yes, indeed. You can just reference the .NET Standard 2.0 package from within a .NET Framework 4.6.1 project (a framework version lower will not work with .NET Standard 2.0). There is no need to have separate targets or rebuilds.

Also read Referencing .NET Standard Assemblies from both .NET Core and .NET Framework .

About the benefits of using multi-targeting: there is none unless you need to. What can be such a case? According to the documentation :

You may need to target older versions of the .NET Framework when your project supports both the .NET Framework and .NET Core. In this scenario, if you want to use newer APIs and language constructs for the newer targets, use #if directives in your code. You also might need to add different packages and dependencies for each platform you're targeting to include the different APIs needed for each case.

So you would only really need that if you have incompatibility in code or references. If you don't have that problem, you don't need multi-targeting.

There is a sample given how to include references based on the build target, which might give some insight:

<ItemGroup Condition="'$(TargetFramework)' == 'net40'">
  <Reference Include="System.Net" />
</ItemGroup>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM