src/Mimp.SeeSharper.Reflection/TypeExtensions.Attribute.cs
/// <summary>
/// Return the <typeparamref name="TAttribute"/> or throw a <see cref="InvalidOperationException"/>.
/// </summary>
/// <typeparam name="TAttribute"></typeparam>
/// <param name="inherit"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
/// <exception cref="InvalidOperationException">If no or more than one attribute exists</exception>
Return the  or throw a .
Return the or throw a .
public static TAttribute GetSingleCustomAttribute<TAttribute>(this Type type, bool inherit)
            where TAttribute : Attribute
        {
            if (type is null)
                throw new ArgumentNullException(nameof(type));
            var attrs = type.GetCustomAttributes<TAttribute>(inherit).GetEnumerator();
            if (!attrs.MoveNext())
                throw new InvalidOperationException($"No {typeof(TAttribute)} found.");
            var result = attrs.Current;
            if (attrs.MoveNext())
                throw new InvalidOperationException($"More than one {typeof(TAttribute)} found.");
            return result;
        }
public static TAttribute GetSingleCustomAttribute < TAttribute > ( this Type type , bool inherit ) where TAttribute : Attribute { if ( type is null ) throw new ArgumentNullException ( nameof ( type ) ) ; var attrs = type . GetCustomAttributes < TAttribute > ( inherit ) . GetEnumerator ( ) ; if ( ! attrs . MoveNext ( ) ) throw new InvalidOperationException ( $" No  { typeof ( TAttribute ) }  found. " ) ; var result = attrs . Current ; if ( attrs . MoveNext ( ) ) throw new InvalidOperationException ( $" More than one  { typeof ( TAttribute ) }  found. " ) ; return result ; }
Return the  or throw a .
Return the or throw a .
None
None
If no or more than one attribute exists
If no or more than one attribute exists
Type
this
inherit
bool
None
typeparam
None