【问题标题】:Nuget package compatibility for .net core and .net framework 4.6.net core 和 .net framework 4.6 的 Nuget 包兼容性
【发布时间】:2018-04-13 06:13:27
【问题描述】:

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

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

这就是我定位框架的方式

<TargetFrameworks>netcoreapp2.0;netstandard2.0;net45;net46</TargetFrameworks>

【问题讨论】:

  • 顺便说一句,您肯定需要以不同的方式定位 net45 和 net46 吗?同上 netcoreapp2.0 和 netstandard2.0?如果您以 net45 为目标,则该程序包仍可与 net46 一起使用 - 同样,如果您以 netstandard2.0 为目标,该程序包将与 netcoreapp2.0 一起使用。如果您想使用 net46 或 netcoreapp2.0 特定的功能,通常只需要定位所有这些。
  • 现在可能值得删除屏幕截图,因为它不再相关。

标签: c# .net-core nuget-package .net-framework-version


【解决方案1】:

只需使用将根据每个目标自动提供的条件编译符号。例如:

// 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

或者,您可以像这样排除,而不是包括

#if !NET45 && !NET46

请参阅cross-platform library tutorial 了解在每个环境中定义的条件编译符号列表。 (我认为有版本中性的“NETCORE”、“NETSTANDARD”和“NETFULL”符号或类似符号会很好,但我不相信这些存在。你可以在你的项目文件中指定它们如果你愿意的话。)

【讨论】:

  • Daisy ,添加你的方式后,请在我的问题截图中查看附图。仍然出现构建错误
  • @Diana:你把它放在using 指令之后,这意味着如果这些命名空间不可用,它将失败。将它移到文件开头的右侧,假设您不需要文件中的任何内容用于 netstandard/netcore。
  • @Diana:尽管错误消息表明您可能只是没有依赖于正确的配置和配置扩展包。你有 Microsoft.Configuration.Extensions 的包引用吗?
  • 还需要为using语句添加条件编译符号。以serilog的LogContext为例:github.com/serilog/serilog/blob/…
  • 黛西谢谢。有效。我将您的答案标记为一个很好的解决方案:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2020-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多