Example #1
0
        public static object[] GetCustomAttributes(CustomReflectionContext context, CustomMethodInfo method, Type attributeFilterType, bool inherit)
        {
            IEnumerable <object> attributes = GetFilteredAttributes(context, method.UnderlyingMethod, attributeFilterType);

            if (!inherit)
            {
                return(CollectionServices.IEnumerableToArray(attributes, attributeFilterType));
            }

            CustomMethodInfo baseMember = method.GetBaseDefinition() as CustomMethodInfo;

            if (baseMember == null || baseMember.Equals(method))
            {
                return(CollectionServices.IEnumerableToArray(attributes, attributeFilterType));
            }

            // GetAttributeUsage is expensive and should be put off as much as possible.
            bool isSealed = attributeFilterType.IsSealed;
            bool inherited;
            bool allowMultiple;

            GetAttributeUsage(attributeFilterType, out inherited, out allowMultiple);

            if (isSealed && !inherited)
            {
                return(CollectionServices.IEnumerableToArray(attributes, attributeFilterType));
            }

            // declaredAttributes should have already been filtered by attributeFilterType
            List <object> results = new List <object>(attributes);

            do
            {
                if (isSealed && results.Count > 0 && !allowMultiple)
                {
                    break;
                }

                method = baseMember;

                IEnumerable <object> inheritedAttributes = GetFilteredAttributes(context, method.UnderlyingMethod, attributeFilterType);
                CombineCustomAttributes(results, inheritedAttributes, attributeFilterType, inherited, allowMultiple);

                baseMember = method.GetBaseDefinition() as CustomMethodInfo;
            } while (baseMember != null && !baseMember.Equals(method));

            return(CollectionServices.ConvertListToArray(results, attributeFilterType));
        }