| | | 1 | | using System.Reflection; |
| | | 2 | | |
| | | 3 | | namespace Towel; |
| | | 4 | | |
| | | 5 | | /// <summary>A value-based "tag" attribute.</summary> |
| | | 6 | | [AttributeUsage(AttributeTargets.All, AllowMultiple = true)] |
| | | 7 | | public class TagAttribute : Attribute |
| | | 8 | | { |
| | | 9 | | internal object? Tag; |
| | | 10 | | internal object? Value; |
| | | 11 | | |
| | | 12 | | /// <summary>Creates a new value-based "tag" attribute.</summary> |
| | | 13 | | /// <param name="tag">The tag.</param> |
| | | 14 | | /// <param name="value">The value.</param> |
| | 73 | 15 | | public TagAttribute(object? tag, object? value) |
| | 73 | 16 | | { |
| | 73 | 17 | | Tag = tag; |
| | 73 | 18 | | Value = value; |
| | 73 | 19 | | } |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary>Extension methods for reflection types and <see cref="TagAttribute"/>.</summary> |
| | | 23 | | public static class TagAttributeExtensions |
| | | 24 | | { |
| | | 25 | | /// <summary>Gets a <see cref="TagAttribute"/> on a <see cref="MemberInfo"/>.</summary> |
| | | 26 | | /// <param name="memberInfo">The type to get the <see cref="TagAttribute"/> of.</param> |
| | | 27 | | /// <param name="tag">The tag to get the value of.</param> |
| | | 28 | | /// <returns> |
| | | 29 | | /// - <see cref="bool"/> Found: True if the tag was found; False if not or if multiple tags were found (ambiguous).<br |
| | | 30 | | /// - <see cref="object"/> Value: The value if found or default if not. |
| | | 31 | | /// </returns> |
| | | 32 | | public static (bool Found, object? Value) GetTag(this MemberInfo memberInfo, object? tag) |
| | | 33 | | { |
| | | 34 | | if (memberInfo is null) throw new ArgumentNullException(nameof(memberInfo)); |
| | | 35 | | return Find(memberInfo.GetCustomAttributes<TagAttribute>(), tag); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <summary>Gets a <see cref="TagAttribute"/> on a <see cref="ParameterInfo"/>.</summary> |
| | | 39 | | /// <param name="parameterInfo">The type to get the <see cref="TagAttribute"/> of.</param> |
| | | 40 | | /// <param name="tag">The tag to get the value of.</param> |
| | | 41 | | /// <returns> |
| | | 42 | | /// - <see cref="bool"/> Found: True if the tag was found; False if not or if multiple tags were found (ambiguous).<br |
| | | 43 | | /// - <see cref="object"/> Value: The value if found or default if not. |
| | | 44 | | /// </returns> |
| | | 45 | | public static (bool Found, object? Value) GetTag(this ParameterInfo parameterInfo, object? tag) |
| | | 46 | | { |
| | | 47 | | if (parameterInfo is null) throw new ArgumentNullException(nameof(parameterInfo)); |
| | | 48 | | return Find(parameterInfo.GetCustomAttributes<TagAttribute>(), tag); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <summary>Gets a <see cref="TagAttribute"/> on a <see cref="MemberInfo"/>.</summary> |
| | | 52 | | /// <typeparam name="TEnum">The type of enum to get the atributes on its fields.</typeparam> |
| | | 53 | | /// <param name="enumValue">The type of enum field to get the <see cref="TagAttribute"/> of.</param> |
| | | 54 | | /// <param name="tag">The tag to get the value of.</param> |
| | | 55 | | /// <returns> |
| | | 56 | | /// - <see cref="bool"/> Found: True if the tag was found; False if not or if multiple tags were found (ambiguous).<br |
| | | 57 | | /// - <see cref="object"/> Value: The value if found or default if not. |
| | | 58 | | /// </returns> |
| | | 59 | | public static (bool Found, object? Value) GetTag<TEnum>(this TEnum enumValue, object? tag) |
| | | 60 | | where TEnum : Enum |
| | | 61 | | { |
| | | 62 | | if (enumValue is null) throw new ArgumentNullException(nameof(enumValue)); |
| | | 63 | | return Find(enumValue.GetEnumAttributes<TagAttribute>(), tag); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | internal static (bool Found, object? Value) Find(System.Collections.Generic.IEnumerable<TagAttribute> enumerable, obje |
| | | 67 | | { |
| | | 68 | | bool found = false; |
| | | 69 | | object? value = default; |
| | | 70 | | foreach (TagAttribute valueAttribute in enumerable) |
| | | 71 | | { |
| | | 72 | | if (ReferenceEquals(tag, valueAttribute.Tag) || (tag is not null && tag.Equals(valueAttribute.Tag))) |
| | | 73 | | { |
| | | 74 | | if (found) |
| | | 75 | | { |
| | | 76 | | return (false, default); |
| | | 77 | | } |
| | | 78 | | found = true; |
| | | 79 | | value = valueAttribute.Value; |
| | | 80 | | } |
| | | 81 | | } |
| | | 82 | | return (found, value); |
| | | 83 | | } |
| | | 84 | | } |