在 .NET Core Class 库中使用 System.ComponentModel

Using System.ComponentModel in a .NET Core Class Library

我现在正在玩 .NET Core Class 库项目并测试一些我已经在针对 .NET Framework 的常规 class 库项目中已有的代码。

我安装了 System.ComponentModel NuGet 包以在 enum 中使用注释。看起来它没有识别注释。

枚举如下所示:

using System.ComponentModel;
public enum UserRole
{
   [Description("Undefined")]
   Undefined = 0,
   [Description("Super User")]
   SuperUser = 1,
   [Description("Administrator")]
   Admin = 2,
   [Description("Regular User")]
   RegUser = 4
}

我看到的错误是:

The type or namespace name 'DescriptionAttribute' could not be found (are you missing a using directive or assembly reference?)

顶部的 using System.ComponentModel 参考是灰色的,看起来根本没有被使用。

两个问题:

  1. 我怀疑我会 运行 遇到许多类似的问题。找出 .NET Framework 和 .NET Core class 库引用之间的差异的最佳方法是什么?
  2. 能够在 .NET Core Class 库中使用注释的解决方案是什么?

P.S。该代码在我针对 .NET Framework 4.6.2 的 class 库项目中运行良好。因此,我只将我知道可以用于我的新 .NET Core Class 库项目的代码传输。

对于 DescriptionAttribute,您需要使用 System.ComponentModel.Primitives 包(4.1 版及更高版本)。

这是一个方便的网站,您可以在其中查找 API 的包/支持: https://packagesearch.azurewebsites.net/?q=descriptionattribute