From c0c41326fc843dff8fe8c09ea149b7e2f294deaf Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Fri, 29 Apr 2022 11:13:59 +0100 Subject: [PATCH] Add defaultValue overload for SelectFromCustomAttribute --- X10D/src/Reflection/MemberInfoExtensions.cs | 27 +++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/X10D/src/Reflection/MemberInfoExtensions.cs b/X10D/src/Reflection/MemberInfoExtensions.cs index cbc90f1..1de6866 100644 --- a/X10D/src/Reflection/MemberInfoExtensions.cs +++ b/X10D/src/Reflection/MemberInfoExtensions.cs @@ -1,4 +1,4 @@ -using System.Globalization; +using System.Globalization; using System.Reflection; namespace X10D.Reflection; @@ -78,6 +78,29 @@ public static class MemberInfoExtensions public static TReturn? SelectFromCustomAttribute(this MemberInfo member, Func selector) where TAttribute : Attribute + { + return member.SelectFromCustomAttribute(selector, default); + } + + /// + /// Retrieves a custom attribute that is decorated by the current member, and projects it into to a new form. + /// + /// The attribute type. + /// The return type of the delegate. + /// The member. + /// A transform function to apply to the attribute. + /// The default value to return when the specified attribute is not found. + /// + /// An instance of as provided from . + /// + /// + /// is + /// -or- + /// is . + /// + public static TReturn? SelectFromCustomAttribute(this MemberInfo member, + Func selector, TReturn? defaultValue) + where TAttribute : Attribute { if (member is null) { @@ -91,6 +114,6 @@ public static class MemberInfoExtensions return member.GetCustomAttribute() is { } attribute ? selector(attribute) - : default; + : defaultValue; } }