From 1ccb8b24578040741d873bb0529301f05e48ef97 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Tue, 21 Apr 2020 04:16:46 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Add=20ReflectionExtensions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - `GetDefaultValue` and `GetDefaultValue` - gets the value stored in the member's `DefaultValue` attribute - `GetDescription`- gets the value stored in the member's `Description` attribute - `SelectFromCustomAttribute` - Internally calls `GetCustomAttribute` and passes it to a `Func` so that specific members may be selected --- X10D/src/ReflectionExtensions.cs | 113 +++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 X10D/src/ReflectionExtensions.cs diff --git a/X10D/src/ReflectionExtensions.cs b/X10D/src/ReflectionExtensions.cs new file mode 100644 index 0000000..25fbc54 --- /dev/null +++ b/X10D/src/ReflectionExtensions.cs @@ -0,0 +1,113 @@ +namespace X10D +{ + using System; + using System.ComponentModel; + using System.Reflection; + + /// + /// Extension methods for various reflection types. + /// + public static class ReflectionExtensions + { + /// + /// Gets the value set in this member's annotated , or + /// if none exists. + /// + /// The member. + /// Returns an representing the value stored in this member's + /// . + /// is . + public static object GetDefaultValue(this MemberInfo member) + { + if (member is null) + { + throw new ArgumentNullException(nameof(member)); + } + + if (!(member.GetCustomAttribute() is { } attribute)) + { + return default; + } + + return attribute.Value; + } + + /// + /// Gets the value set in this member's annotated , or + /// if none exists. + /// + /// The type to which the value should cast. + /// The member. + /// Returns an instance of representing the value stored in this member's + /// . + /// is . + public static T GetDefaultValue(this MemberInfo member) + { + if (member is null) + { + throw new ArgumentNullException(nameof(member)); + } + + return (T)member.GetDefaultValue(); + } + + /// + /// Gets the value set in this member's annotated , or + /// if none exists. + /// + /// The member. + /// Returns an instance of representing the value stored in this member's + /// . + /// is . + public static string GetDescription(this MemberInfo member) + { + if (member is null) + { + throw new ArgumentNullException(nameof(member)); + } + + if (!(member.GetCustomAttribute() is { } attribute)) + { + return null; + } + + return attribute.Description; + } + + /// + /// 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. + /// + /// The attribute type. + /// The return type of the delegate. + /// The member. + /// The selector delegate. + /// Returns an instance of as provided from + /// . + /// is + /// -or- + /// is . + public static TReturn SelectFromCustomAttribute( + this MemberInfo member, + Func 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() is { } attribute)) + { + return default; + } + + return selector(attribute); + } + } +}