using System; using System.ComponentModel; using System.Reflection; using RGB.NET.Core; namespace RGB.NET.Devices.Wooting.Helper { /// /// Offers some extensions and helper-methods for enum related things. /// internal static class EnumExtension { /// /// Gets the value of the . /// /// The enum value to get the description from. /// The generic enum-type /// The value of the or the result of the source. internal static string GetDescription(this T source) where T : struct { return source.GetAttribute()?.Description ?? source.ToString(); } /// /// Gets the attribute of type T. /// /// The enum value to get the attribute from /// The generic attribute type /// The generic enum-type /// The . private static T GetAttribute(this TEnum source) where T : Attribute where TEnum : struct { FieldInfo fi = source.GetType().GetField(source.ToString()); T[] attributes = (T[])fi.GetCustomAttributes(typeof(T), false); return attributes.Length > 0 ? attributes[0] : null; } } }