From 1ac1ac29651c00c2be3064599f38e5f65a09a6d7 Mon Sep 17 00:00:00 2001 From: Chris Cameron Date: Wed, 7 Feb 2018 16:56:18 -0500 Subject: [PATCH] GetCustomAttribute/s extension methods --- .../Extensions/ReflectionExtensions.cs | 46 +++++++++++++++++++ ICD.Common.Utils/ReflectionUtils.cs | 5 +- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/ICD.Common.Utils/Extensions/ReflectionExtensions.cs b/ICD.Common.Utils/Extensions/ReflectionExtensions.cs index f22b19b..79f344b 100644 --- a/ICD.Common.Utils/Extensions/ReflectionExtensions.cs +++ b/ICD.Common.Utils/Extensions/ReflectionExtensions.cs @@ -14,6 +14,21 @@ namespace ICD.Common.Utils.Extensions /// public static class ReflectionExtensions { + /// + /// Returns the custom attributes attached to the member. + /// + /// + /// + /// + public static IEnumerable GetCustomAttributes(this ICustomAttributeProvider extends) + where T : Attribute + { + if (extends == null) + throw new ArgumentNullException("extends"); + + return extends.GetCustomAttributes(false); + } + /// /// Returns the custom attributes attached to the member. /// @@ -29,5 +44,36 @@ namespace ICD.Common.Utils.Extensions return extends.GetCustomAttributes(typeof(T), inherits).Cast(); } + + /// + /// Returns the custom attribute attached to the member. + /// + /// + /// + /// + public static T GetCustomAttribute(this ICustomAttributeProvider extends) + where T : Attribute + { + if (extends == null) + throw new ArgumentNullException("extends"); + + return extends.GetCustomAttribute(false); + } + + /// + /// Returns the custom attribute attached to the member. + /// + /// + /// + /// + /// + public static T GetCustomAttribute(this ICustomAttributeProvider extends, bool inherits) + where T : Attribute + { + if (extends == null) + throw new ArgumentNullException("extends"); + + return extends.GetCustomAttributes(inherits).First(); + } } } diff --git a/ICD.Common.Utils/ReflectionUtils.cs b/ICD.Common.Utils/ReflectionUtils.cs index 82ab92c..0aaeab0 100644 --- a/ICD.Common.Utils/ReflectionUtils.cs +++ b/ICD.Common.Utils/ReflectionUtils.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using ICD.Common.Properties; +using ICD.Common.Utils.Extensions; using ICD.Common.Utils.IO; #if SIMPLSHARP using Crestron.SimplSharp.CrestronIO; @@ -251,9 +252,6 @@ namespace ICD.Common.Utils if (assembly == null) throw new ArgumentNullException("assembly"); -#if SIMPLSHARP - return assembly.GetCustomAttributes(typeof(T), false).Cast(); -#else try { return assembly.GetCustomAttributes(); @@ -262,7 +260,6 @@ namespace ICD.Common.Utils { return Enumerable.Empty(); } -#endif } ///