// Copyright (c) 2023- schick Informatik // Description: Enum description string conversion helper using System.ComponentModel; namespace brecal.model { public static class EnumHelper { public static string Description(this Enum eValue) { var nAttributes = eValue.GetType().GetField(eValue.ToString())?.GetCustomAttributes(typeof(DescriptionAttribute), false); if (nAttributes != null) { if (nAttributes.Any() && nAttributes.First() != null && (nAttributes.First() is DescriptionAttribute da)) { return da.Description; } } return eValue.ToString(); } public static IEnumerable> GetAllValuesAndDescription(Type t) { if (!t.IsEnum) throw new ArgumentException("t must be an enum"); return from e in Enum.GetValues(t).Cast() select new KeyValuePair(e.ToString(), e.Description()); } } }