Add GetAttribute for enums

This commit is contained in:
Bogdan 2023-07-18 03:28:31 +03:00
parent 79361d92cb
commit a4a50b880c

View file

@ -0,0 +1,17 @@
using System;
using System.Linq;
namespace NzbDrone.Common.Extensions
{
public static class EnumExtensions
{
public static T GetAttribute<T>(this Enum value)
where T : Attribute
{
var enumType = value.GetType();
var name = Enum.GetName(enumType, value);
return name == null ? null : enumType.GetField(name)?.GetCustomAttributes(false).OfType<T>().SingleOrDefault();
}
}
}