コード例 #1
0
        public static T[] GetAttributes <T>(ICustomAttributeProvider attributeProvider, bool inherit) where T : Attribute
        {
            object provider = attributeProvider.UnderlyingObject;

            if (provider is Type)
            {
                return(((Type)provider).GetTypeInfo().GetCustomAttributes <T>(inherit).ToArray());
            }

            if (provider is Assembly)
            {
                return(((Assembly)provider).GetCustomAttributes <T>().ToArray());
            }

            if (provider is MemberInfo)
            {
                return(((MemberInfo)provider).GetCustomAttributes <T>(inherit).ToArray());
            }

            if (provider is Module)
            {
                return(((Module)provider).GetCustomAttributes <T>().ToArray());
            }

            if (provider is ParameterInfo)
            {
                return(((ParameterInfo)provider).GetCustomAttributes <T>(inherit).ToArray());
            }

            throw new Exception("Cannot get attributes from '{0}'.".FormatWith(CultureInfo.InvariantCulture, provider));
        }
コード例 #2
0
        public static T[] GetAttributes <T>(ICustomAttributeProvider attributeProvider, bool inherit) where T : Attribute
        {
            ValidationUtils.ArgumentNotNull(attributeProvider, "attributeProvider");

            object provider;

#if !PORTABLE
            provider = attributeProvider;
#else
            provider = attributeProvider.UnderlyingObject;
#endif

            // http://hyperthink.net/blog/getcustomattributes-gotcha/
            // ICustomAttributeProvider doesn't do inheritance

            if (provider is Type)
            {
                return((T[])((Type)provider).GetCustomAttributes(typeof(T), inherit));
            }

            if (provider is Assembly)
            {
                return((T[])Attribute.GetCustomAttributes((Assembly)provider, typeof(T)));
            }

            if (provider is MemberInfo)
            {
                return((T[])Attribute.GetCustomAttributes((MemberInfo)provider, typeof(T), inherit));
            }

#if !PORTABLE
            if (provider is Module)
            {
                return((T[])Attribute.GetCustomAttributes((Module)provider, typeof(T), inherit));
            }
#endif

            if (provider is ParameterInfo)
            {
                return((T[])Attribute.GetCustomAttributes((ParameterInfo)provider, typeof(T), inherit));
            }

#if !PORTABLE
            return((T[])attributeProvider.GetCustomAttributes(typeof(T), inherit));
#else
            throw new Exception("Cannot get attributes from '{0}'.".FormatWith(CultureInfo.InvariantCulture, provider));
#endif
        }
コード例 #3
0
        public static T GetAttribute <T>(ICustomAttributeProvider attributeProvider, bool inherit) where T : Attribute
        {
            T[] attributes = GetAttributes <T>(attributeProvider, inherit);

            return(attributes.SingleOrDefault());
        }
コード例 #4
0
 public static T GetAttribute <T>(ICustomAttributeProvider attributeProvider) where T : Attribute
 {
     return(GetAttribute <T>(attributeProvider, true));
 }