From a4a50b880c660b5be0b9fef838ba8b1c3b0d2783 Mon Sep 17 00:00:00 2001 From: Bogdan Date: Tue, 18 Jul 2023 03:28:31 +0300 Subject: [PATCH] Add GetAttribute for enums --- .../Extensions/EnumExtensions.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/NzbDrone.Common/Extensions/EnumExtensions.cs diff --git a/src/NzbDrone.Common/Extensions/EnumExtensions.cs b/src/NzbDrone.Common/Extensions/EnumExtensions.cs new file mode 100644 index 000000000..fcc550d99 --- /dev/null +++ b/src/NzbDrone.Common/Extensions/EnumExtensions.cs @@ -0,0 +1,17 @@ +using System; +using System.Linq; + +namespace NzbDrone.Common.Extensions +{ + public static class EnumExtensions + { + public static T GetAttribute(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().SingleOrDefault(); + } + } +}