using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Reflection; namespace VCommon.Reflection { public static class AttributeExtensionMethod { public static T[] GetCustomAttributeIncludeInterface(this Type findFrom) where T : Attribute { var ofType = findFrom.GetCustomAttributes(true); var ifaces = findFrom.GetInterfaces().SelectMany(i => i.GetCustomAttributes(true)); return ofType.Concat(ifaces).Distinct().ToArray(); } public static string GetDescriptionAttributeValue(this MemberInfo me) => me.GetCustomAttribute()?.Description; public static bool IsDefinedIncludeInterface(this Type me, Type attributeType) => me.IsDefined(attributeType, true) || me.GetInterfaces().Any(p => p.IsDefined(attributeType, true)); public static bool IsDefinedIncludeInterface(this PropertyInfo me, Type attributeType) => me.IsDefined(attributeType, true) || LookupInterfaceDefines(me, attributeType); public static T[] GetCustomAttributeIncludeInterface(this PropertyInfo me) { var lstResult = new List(); lstResult.AddRange(me.GetCustomAttributes().OfType()); var interfaces = me.DeclaringType?.GetInterfaces(); if (null == interfaces) return lstResult.ToArray(); foreach (var iface in interfaces) { var propertyInfo = iface.GetProperty(me.Name); if (null != propertyInfo) lstResult.AddRange(propertyInfo.GetCustomAttributes(true).OfType()); } return lstResult.ToArray(); } private static bool LookupInterfaceDefines(PropertyInfo me, Type attributeType) { var interfaces = me?.DeclaringType?.GetInterfaces(); return null != interfaces && interfaces.Any(p => true == p.GetProperty(me.Name)?.IsDefined(attributeType, true)); } } }