コード例 #1
0
        public static TAttributeType GetCustomAttribute <TAttributeType>(this MemberInfo mi, bool inherit)
            where TAttributeType : Attribute
        {
            if (mi == null)
            {
                throw new ArgumentNullException("mi", "MemberInfo: mi as parameter in extension method must not be null.");
            }

            var attriList = mi.GetCustomAttributes(typeof(TAttributeType), inherit) as IEnumerable <TAttributeType>;

            if (attriList.Count() > 1)
            {
                throw new AmbiguousMatchException("More than one attribute of Type: " + typeof(TAttributeType).ToString() + " found on MemberInfo: " + mi.ToString() + ".");
            }

            return(attriList.FirstOrDefault());
        }
コード例 #2
0
        public static TAttributeType GetCustomAttribute <TAttributeType>(this MemberInfo mi, bool inherit)
            where TAttributeType : Attribute
        {
            if (mi == null)
            {
                throw new ArgumentNullException(nameof(mi), "MemberInfo: mi as parameter in extension method must not be null.");
            }

            TAttributeType[] attriList = (mi.GetCustomAttributes(typeof(TAttributeType), inherit) as IEnumerable <TAttributeType>).ToArray();

            //TODO: Do we really want to throw?
            if (attriList.Count() > 1)
            {
                throw new AmbiguousMatchException($"More than one attribute of Type: {typeof(TAttributeType)} found on MemberInfo: {mi.ToString()}.");
            }

            return(attriList.FirstOrDefault());
        }