Sam Sam - 9 months ago 92
C# Question

Using System.ComponentModel in a .NET Core Class Library

I'm now playing with .NET Core Class Library projects and testing some code that I already have in a regular class library project targeting .NET Framework.

I installed the

System.ComponentModel
NuGet package to use annotations in an
enum
. Looks like it's not recognizing the annotation.

Here's what the enum looks like:

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 error I'm seeing is:


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


The using
System.ComponentModel
reference at the top is grayed out and looks like it's not being used at all.

Two questions:


  1. I suspect I'll run into many similar issues. What's the best way for me to figure out any differences between .NET Framework and .NET Core class library references?

  2. What's the solution to being able to use annotations in a .NET Core Class Library?



P.S. The code is working perfectly in my class library project targeting .NET Framework 4.6.2. So I'm only transferring code that I know to be working into my new .NET Core Class Library project.

Answer Source

For DescriptionAttribute you need to use the System.ComponentModel.Primitives package (version 4.1 and above).

Here's a handy website that you can look up the packages / support for APIs: https://packagesearch.azurewebsites.net/?q=descriptionattribute