Fixed some code-issues in novation-library

This commit is contained in:
DarthAffe committed 2017-08-16 19:56:15 +02:00
1 parent b642cd31b0
commit 5440f9844f
11 files changed
+106 -42

No files matched your search

@@ -0,0 +1,33 @@
using System;
using System.Reflection;
using RGB.NET.Devices.Novation.Attributes;
namespace RGB.NET.Devices.Novation
{
/// <summary>
/// Offers some extensions and helper-methods for enum related things.
/// </summary>
internal static class EnumExtension
{
/// <summary>
/// Gets the value of the <see cref="DeviceIdAttribute"/>.
/// </summary>
/// <param name="source">The enum value to get the description from.</param>
/// <returns>The value of the <see cref="DeviceIdAttribute"/> of the source.</returns>
internal static string GetDeviceId(this Enum source) => source.GetAttribute<DeviceIdAttribute>()?.Id;
/// <summary>
/// Gets the attribute of type T.
/// </summary>
/// <param name="source">The enum value to get the attribute from</param>
/// <typeparam name="T">The generic attribute type</typeparam>
/// <returns>The <see cref="Attribute"/>.</returns>
private static T GetAttribute<T>(this Enum source)
where T : Attribute
{
FieldInfo fi = source.GetType().GetField(source.ToString());
T[] attributes = (T[])fi.GetCustomAttributes(typeof(T), false);
return attributes.Length > 0 ? attributes[0] : null;
}
}
}