using System;
using System.Reflection;
using RGB.NET.Devices.Novation.Attributes;
namespace RGB.NET.Devices.Novation
{
///
/// 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 of the source.
internal static string? GetDeviceId(this Enum source) => source.GetAttribute()?.Id;
///
/// Gets the value of the .
///
/// The enum value to get the description from.
/// The value of the of the source.
internal static NovationColorCapabilities GetColorCapability(this Enum source) => source.GetAttribute()?.Capability ?? NovationColorCapabilities.None;
///
/// Gets the value of the .
///
/// The enum value to get the description from.
/// The value of the of the source.
internal static LedIdMappings GetLedIdMapping(this Enum source) => source.GetAttribute()?.LedIdMapping ?? LedIdMappings.Current;
///
/// 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;
}
}
}