Example #1
0
 public static T GetAttribute <T>(object attributeProvider, bool inherit) where T : Attribute
 {
     T[] attributes = ReflectionUtils.GetAttributes <T>(attributeProvider, inherit);
     if (attributes == null)
     {
         return(default(T));
     }
     return(((IEnumerable <T>)attributes).FirstOrDefault <T>());
 }
Example #2
0
        public static Attribute[] GetAttributes(
            object attributeProvider,
            Type attributeType,
            bool inherit)
        {
            ValidationUtils.ArgumentNotNull(attributeProvider, nameof(attributeProvider));
            object obj  = attributeProvider;
            Type   type = obj as Type;

            if (type != null)
            {
                Attribute[] array =
                    (attributeType != null
            ? (IEnumerable)type.GetCustomAttributes(attributeType, inherit)
            : (IEnumerable)type.GetCustomAttributes(inherit)).Cast <Attribute>().ToArray <Attribute>();
                if (inherit && type.BaseType != null)
                {
                    array = ((IEnumerable <Attribute>)array)
                            .Union <Attribute>(
                        (IEnumerable <Attribute>)ReflectionUtils.GetAttributes((object)type.BaseType, attributeType, inherit))
                            .ToArray <Attribute>();
                }
                return(array);
            }

            Assembly element1 = obj as Assembly;

            if (element1 != null)
            {
                if (attributeType == null)
                {
                    return(Attribute.GetCustomAttributes(element1));
                }
                return(Attribute.GetCustomAttributes(element1, attributeType));
            }

            MemberInfo element2 = obj as MemberInfo;

            if (element2 != null)
            {
                if (attributeType == null)
                {
                    return(Attribute.GetCustomAttributes(element2, inherit));
                }
                return(Attribute.GetCustomAttributes(element2, attributeType, inherit));
            }

            Module element3 = obj as Module;

            if (element3 != null)
            {
                if (attributeType == null)
                {
                    return(Attribute.GetCustomAttributes(element3, inherit));
                }
                return(Attribute.GetCustomAttributes(element3, attributeType, inherit));
            }

            ParameterInfo element4 = obj as ParameterInfo;

            if (element4 != null)
            {
                if (attributeType == null)
                {
                    return(Attribute.GetCustomAttributes(element4, inherit));
                }
                return(Attribute.GetCustomAttributes(element4, attributeType, inherit));
            }

            ICustomAttributeProvider attributeProvider1 = (ICustomAttributeProvider)attributeProvider;

            return(attributeType != null
        ? (Attribute[])attributeProvider1.GetCustomAttributes(attributeType, inherit)
        : (Attribute[])attributeProvider1.GetCustomAttributes(inherit));
        }
Example #3
0
 public static T[] GetAttributes <T>(object attributeProvider, bool inherit) where T : Attribute
 {
     Attribute[] attributes = ReflectionUtils.GetAttributes(attributeProvider, typeof(T), inherit);
     return(attributes as T[] ?? attributes.Cast <T>().ToArray <T>());
 }