using System;
using System.ComponentModel;
using System.Reflection;
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 value of the or the result of the source.
internal static string GetDescription(this System.Enum source)
=> source.GetAttribute()?.Description ?? source.ToString();
///
/// Gets the attribute of type T.
///
/// The enum value to get the attribute from
/// The generic attribute type
/// The .
private static T? GetAttribute(this System.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;
}
}