From 72a4b19f976cae8eba244f896b19bfd9136075b5 Mon Sep 17 00:00:00 2001 From: Jeffery Thompson Date: Tue, 15 May 2018 11:04:40 -0400 Subject: [PATCH 1/2] 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 } } From 567ee4a1ae0767ca908128671297d0be153aa49f Mon Sep 17 00:00:00 2001 From: Jeffery Thompson Date: Tue, 15 May 2018 11:41:11 -0400 Subject: [PATCH 2/2] fix: only bind to members of the same member type --- ICD.Common.Utils/Extensions/ReflectionExtensions.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/ICD.Common.Utils/Extensions/ReflectionExtensions.cs b/ICD.Common.Utils/Extensions/ReflectionExtensions.cs index d299843..a1ccc8a 100644 --- a/ICD.Common.Utils/Extensions/ReflectionExtensions.cs +++ b/ICD.Common.Utils/Extensions/ReflectionExtensions.cs @@ -117,7 +117,10 @@ namespace ICD.Common.Utils.Extensions .Union(extends.DeclaringType? .GetInterfaces() .SelectMany(interfaceType => interfaceType - .GetMember(extends.Name) + .GetMember( + extends.Name, + extends.MemberType, + BindingFlags.Instance) .FirstOrDefault()? .GetCustomAttributes(true) ?? Enumerable.Empty())? .Except(null) ?? Enumerable.Empty())