DKNet

Read enum display metadata

Part of DKNet.Fw.Extensions. Use EnumExtensions and EnumInfo to build display labels from enum fields.

using System;
using System.ComponentModel.DataAnnotations;
using DKNet.Fw.Extensions.Enums;

Console.WriteLine(State.Ready.GetAttribute<DisplayAttribute>()?.Name); // Ready to run
foreach (var info in EnumExtensions.GetEnumInfos<State>())
    Console.WriteLine($"{info.Key}: {info.Name}");
Console.WriteLine(State.Ready.GetEnumInfo()?.Description); // Can start

public enum State
{
    Unknown,
    [Display(Name = "Ready to run", Description = "Can start", GroupName = "Active")]
    Ready
}

GetEnumInfos reads public static enum fields, retrieves DisplayAttribute, and falls back to the field name when Display.Name is absent.

EnumInfo is a record with required Key and Name, plus nullable Description and GroupName. GetEnumInfos<T>() enumerates public static fields, using their names as keys. The instance GetAttribute<T>() looks up a field by the value’s text; GetEnumInfo() uses that attribute’s name, description and group without a name fallback.

⚠️ Gotchas & limits