using System; using System.ComponentModel; using System.Reflection; using RGB.NET.Core; namespace RGB.NET.Devices.CoolerMaster.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 value of the or the result of the source. internal static string? GetDescription(this Enum source) => source.GetAttribute()?.Description ?? source.ToString(); /// /// Gets the value of the . /// /// The enum value to get the description from. /// The value of the or the result of the source. internal static RGBDeviceType GetDeviceType(this Enum source) => source.GetAttribute()?.DeviceType ?? RGBDeviceType.Unknown; /// /// Gets the attribute of type T. /// /// The enum value to get the attribute from /// The generic attribute type /// The . private static T? GetAttribute(this Enum source) where T : Attribute { FieldInfo? fi = source.GetType().GetField(source.ToString()); if (fi == null) return null; T[] attributes = (T[])fi.GetCustomAttributes(typeof(T), false); return attributes.Length > 0 ? attributes[0] : null; } } }