mirror of
https://github.com/Prowlarr/Prowlarr.git
synced 2025-09-17 17:14:18 +02:00
Add GetAttribute for enums
This commit is contained in:
17
src/NzbDrone.Common/Extensions/EnumExtensions.cs
Normal file
17
src/NzbDrone.Common/Extensions/EnumExtensions.cs
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user