针对.net核心和.net框架4.6的Nuget包兼容性

Amn*_*mna 3 c# .net-framework-version nuget-package .net-core

我创建了一个nuget包,它支持.net框架和.net核心.但是其中的一个类只有.net核心可用的引用.我不需要在.net框架中使用该类

是否有任何属性或方法可用,在为.net framweork构建包时排除该类?

这就是我如何定位框架

<TargetFrameworks>netcoreapp2.0;netstandard2.0;net45;net46</TargetFrameworks>
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 6

只需使用将在每个目标基础上自动提供的条件编译符号.例如:

// Put this at the start of the file (if you want some of the file to still be built)
// or class (if you just want to exclude a single class). You could even
// exclude just a single method, or part of a method.
#if NETCOREAPP2_0 || NETSTANDARD2_0

public class NetFrameworkOnlyClass
{
}

// And this at the end
#endif
Run Code Online (Sandbox Code Playgroud)

或者不包括,你可以这样排除:

#if !NET45 && !NET46
Run Code Online (Sandbox Code Playgroud)

请参阅跨平台库教程,以获取在每个环境中定义的条件编译符号的列表.(我认为拥有版本中性的" NETCORE"," NETSTANDARD"和" NETFULL"符号或类似物会很好,但我不相信那些存在.如果你愿意,可以在项目文件中指定它们.)