Add ReflectionExtensions

- `GetDefaultValue` and `GetDefaultValue<T>` - gets the value stored in
the member's `DefaultValue` attribute
- `GetDescription`- gets the value stored in the member's `Description`
attribute
- `SelectFromCustomAttribute<T1, T2>` - Internally calls
`GetCustomAttribute<T1>` and passes it to a `Func<T1, T2>` so that
specific members may be selected
This commit is contained in:
Oliver Booth 2020-04-21 04:16:46 +01:00
parent 36d3014573
commit 1ccb8b2457
No known key found for this signature in database
GPG Key ID: 0D7F2EF1C8D2B9C0
1 changed files with 113 additions and 0 deletions

View File

@ -0,0 +1,113 @@
namespace X10D
{
using System;
using System.ComponentModel;
using System.Reflection;
/// <summary>
/// Extension methods for various reflection types.
/// </summary>
public static class ReflectionExtensions
{
/// <summary>
/// Gets the value set in this member's annotated <see cref="DefaultValueAttribute"/>, or
/// <see langword="default"/> if none exists.
/// </summary>
/// <param name="member">The member.</param>
/// <returns>Returns an <see cref="object"/> representing the value stored in this member's
/// <see cref="DefaultValueAttribute"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="member"/> is <see langword="null"/>.</exception>
public static object GetDefaultValue(this MemberInfo member)
{
if (member is null)
{
throw new ArgumentNullException(nameof(member));
}
if (!(member.GetCustomAttribute<DefaultValueAttribute>() is { } attribute))
{
return default;
}
return attribute.Value;
}
/// <summary>
/// Gets the value set in this member's annotated <see cref="DefaultValueAttribute"/>, or
/// <see langword="default"/> if none exists.
/// </summary>
/// <typeparam name="T">The type to which the value should cast.</typeparam>
/// <param name="member">The member.</param>
/// <returns>Returns an instance of <typeparamref name="T"/> representing the value stored in this member's
/// <see cref="DefaultValueAttribute"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="member"/> is <see langword="null"/>.</exception>
public static T GetDefaultValue<T>(this MemberInfo member)
{
if (member is null)
{
throw new ArgumentNullException(nameof(member));
}
return (T)member.GetDefaultValue();
}
/// <summary>
/// Gets the value set in this member's annotated <see cref="DescriptionAttribute"/>, or
/// <see langword="null"/> if none exists.
/// </summary>
/// <param name="member">The member.</param>
/// <returns>Returns an instance of <typeparamref name="T"/> representing the value stored in this member's
/// <see cref="DescriptionAttribute"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="member"/> is <see langword="null"/>.</exception>
public static string GetDescription(this MemberInfo member)
{
if (member is null)
{
throw new ArgumentNullException(nameof(member));
}
if (!(member.GetCustomAttribute<DescriptionAttribute>() is { } attribute))
{
return null;
}
return attribute.Description;
}
/// <summary>
/// Retrieves a custom attribute of a specified type that is applied to the specified member, and passes it
/// to a selector delegate in order to select one or more the members in the attribute.
/// </summary>
/// <typeparam name="TAttribute">The attribute type.</typeparam>
/// <typeparam name="TReturn">The return type of the <paramref name="selector"/> delegate.</typeparam>
/// <param name="member">The member.</param>
/// <param name="selector">The selector delegate.</param>
/// <returns>Returns an instance of <typeparamref name="TReturn"/> as provided from
/// <paramref name="selector"/>.</returns>
/// <exception cref="ArgumentNullException"><paramref name="member"/> is <see langword="null"/>
/// -or-
/// <paramref name="selector"/> is <see langword="null"/>.</exception>
public static TReturn SelectFromCustomAttribute<TAttribute, TReturn>(
this MemberInfo member,
Func<TAttribute, TReturn> selector)
where TAttribute : Attribute
{
if (member is null)
{
throw new ArgumentNullException(nameof(member));
}
if (selector is null)
{
throw new ArgumentNullException(nameof(selector));
}
if (!(member.GetCustomAttribute<TAttribute>() is { } attribute))
{
return default;
}
return selector(attribute);
}
}
}