1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-10 04:55:42 +00:00

Remove Type.HasCustomAttribute and Type.SelectFromCustomAttribute

Since Type inherits MemberInfo, these methods are implicitly available via MemberInfoExtensions
This commit is contained in:
Oliver Booth 2022-04-26 10:32:02 +01:00
parent 38ae5b0b7e
commit f7b5ef49c8
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634

View File

@ -8,59 +8,6 @@ namespace X10D;
/// </summary>
public static class TypeExtensions
{
/// <summary>
/// Returns a value indicating whether or not the current type has been decorated with a specified attribute.
/// </summary>
/// <param name="type">The type whose attributes to check.</param>
/// <typeparam name="T">The attribute type.</typeparam>
/// <returns>
/// <see langword="true" /> if the current type has been decorated with a specified attribute, or
/// <see langword="false" /> otherwise.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="type" /> is <see langword="null" />.</exception>
public static bool HasCustomAttribute<T>(this Type type)
where T : Attribute
{
if (type is null)
{
throw new ArgumentNullException(nameof(type));
}
return type.HasCustomAttribute(typeof(T));
}
/// <summary>
/// Returns a value indicating whether or not the current type has been decorated with a specified attribute.
/// </summary>
/// <param name="type">The type whose attributes to check.</param>
/// <param name="attribute">The attribute type.</param>
/// <returns>
/// <see langword="true" /> if the current type has been decorated with a specified attribute, or
/// <see langword="false" /> otherwise.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="type" /> is <see langword="null" />.</exception>
public static bool HasCustomAttribute(this Type type, Type attribute)
{
if (type is null)
{
throw new ArgumentNullException(nameof(type));
}
if (attribute is null)
{
throw new ArgumentNullException(nameof(attribute));
}
if (!attribute.Inherits<Attribute>())
{
throw new ArgumentException(
string.Format(CultureInfo.CurrentCulture, ExceptionMessages.TypeDoesNotInheritAttribute, attribute),
nameof(attribute));
}
return type.GetCustomAttribute(attribute) is not null;
}
/// <summary>
/// Returns a value indicating whether the current type implements a specified interface.
/// </summary>
@ -155,37 +102,4 @@ public static class TypeExtensions
return value.IsSubclassOf(type);
}
/// <summary>
/// Retrieves a custom attribute that is decorated by the current type, and projects it into to a new form.
/// </summary>
/// <typeparam name="TAttribute">The attribute type.</typeparam>
/// <typeparam name="TReturn">The return type of the <paramref name="selector" /> delegate.</typeparam>
/// <param name="type">The type.</param>
/// <param name="selector">A transform function to apply to the attribute.</param>
/// <returns>
/// An instance of <typeparamref name="TReturn" /> as provided from <paramref name="selector" />.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="type" /> is <see langword="null" />
/// -or-
/// <paramref name="selector" /> is <see langword="null" />.
/// </exception>
public static TReturn? SelectFromCustomAttribute<TAttribute, TReturn>(this Type type, Func<TAttribute, TReturn> selector)
where TAttribute : Attribute
{
if (type is null)
{
throw new ArgumentNullException(nameof(type));
}
if (selector is null)
{
throw new ArgumentNullException(nameof(selector));
}
return type.GetCustomAttribute<TAttribute>() is { } attribute
? selector(attribute)
: default;
}
}