From 72a4b19f976cae8eba244f896b19bfd9136075b5 Mon Sep 17 00:00:00 2001 From: Jeffery Thompson Date: Tue, 15 May 2018 11:04:40 -0400 Subject: [PATCH] feat: add ext methods to get interface attributes --- .../Extensions/ReflectionExtensions.cs | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/ICD.Common.Utils/Extensions/ReflectionExtensions.cs b/ICD.Common.Utils/Extensions/ReflectionExtensions.cs index 7986791..d299843 100644 --- a/ICD.Common.Utils/Extensions/ReflectionExtensions.cs +++ b/ICD.Common.Utils/Extensions/ReflectionExtensions.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using ICD.Common.Properties; #if SIMPLSHARP using Crestron.SimplSharp.Reflection; #else @@ -83,5 +84,45 @@ namespace ICD.Common.Utils.Extensions return extends.GetCustomAttributes(inherits).First(); } + +#if NETSTANDARD + /// + /// Returns the custom attributes attached to the member. + /// + /// + /// + /// + /// + public static IEnumerable GetCustomAttributesIncludingBaseInterfaces(this Type extends) + where T : Attribute + { + return extends.GetCustomAttributes(true) + .Union(extends.GetInterfaces() + .SelectMany(interfaceType => interfaceType + .GetCustomAttributes(true))) + .Distinct(); + } + + /// + /// Returns the custom attributes attached to the member. + /// + /// + /// + /// + /// + public static IEnumerable GetCustomAttributesIncludingBaseInterfaces(this MemberInfo extends) + where T : Attribute + { + return extends.GetCustomAttributes(true) + .Union(extends.DeclaringType? + .GetInterfaces() + .SelectMany(interfaceType => interfaceType + .GetMember(extends.Name) + .FirstOrDefault()? + .GetCustomAttributes(true) ?? Enumerable.Empty())? + .Except(null) ?? Enumerable.Empty()) + .Distinct(); + } +#endif } }