public static TAttribute[] GetCustomAttributesBasedOn <TAttribute>([NotNull] this MemberInfo element) where TAttribute : Attribute { Fail.IfArgumentNull(element, nameof(element)); Fail.IfArgumentNull(element.DeclaringType, nameof(element.DeclaringType)); object[] memberAttributes = element.GetCustomAttributes(inherit: true); MemberInfo[] interfaceMembers = CustomAttributeExtensions.GetInterfaceMembersImplementedBy(element); object[] interfaceAttributes = interfaceMembers.SelectMany(m => m.GetCustomAttributes(inherit: true)) .ToArray(); IEnumerable <object> allAttributes = memberAttributes.Union(interfaceAttributes).ToList(); TAttribute[] attributes = CustomAttributeExtensions.GetInheritedAttributes <TAttribute>(allAttributes, element.Name); return(attributes); }
public static TAttribute[] GetCustomAttributesBasedOn <TAttribute>([NotNull] this ParameterInfo parameter) where TAttribute : Attribute { Fail.IfArgumentNull(parameter, nameof(parameter)); //Fail.IfArgumentNull(parameter.DeclaringType, nameof(parameter.DeclaringType)); object[] memberAttributes = parameter.GetCustomAttributes(inherit: true); MemberInfo[] interfaceMembers = CustomAttributeExtensions.GetInterfaceMembersImplementedBy(parameter.Member); ParameterInfo[] methodArguments = parameter.Member.CastOrFail <MethodInfo>().GetParameters(); var parameterIndex = Array.IndexOf(methodArguments, parameter); object[] interfaceAttributes = interfaceMembers.SelectMany(m => m.CastOrFail <MethodInfo>() .GetParameters()[parameterIndex] //.FailIfNull("Method {0}.{1}() should have argument {2}", m.DeclaringType, m.Name, parameter.Name) .GetCustomAttributes(inherit: true)) .ToArray(); IEnumerable <object> allAttributes = memberAttributes.Union(interfaceAttributes); TAttribute[] attributes = CustomAttributeExtensions.GetInheritedAttributes <TAttribute>(allAttributes, parameter.Name); return(attributes); }