| | 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> |
| | 15 | | public TagAttribute(object? tag, object? value) |
| | 16 | | { |
| | 17 | | Tag = tag; |
| | 18 | | Value = value; |
| | 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) |
| 46 | 33 | | { |
| 46 | 34 | | if (memberInfo is null) throw new ArgumentNullException(nameof(memberInfo)); |
| 46 | 35 | | return Find(memberInfo.GetCustomAttributes<TagAttribute>(), tag); |
| 46 | 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) |
| 4 | 46 | | { |
| 4 | 47 | | if (parameterInfo is null) throw new ArgumentNullException(nameof(parameterInfo)); |
| 4 | 48 | | return Find(parameterInfo.GetCustomAttributes<TagAttribute>(), tag); |
| 4 | 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 |
| 6 | 61 | | { |
| 6 | 62 | | if (enumValue is null) throw new ArgumentNullException(nameof(enumValue)); |
| 6 | 63 | | return Find(enumValue.GetEnumAttributes<TagAttribute>(), tag); |
| 6 | 64 | | } |
| | 65 | |
|
| | 66 | | internal static (bool Found, object? Value) Find(System.Collections.Generic.IEnumerable<TagAttribute> enumerable, obje |
| 56 | 67 | | { |
| 56 | 68 | | bool found = false; |
| 56 | 69 | | object? value = default; |
| 291 | 70 | | foreach (TagAttribute valueAttribute in enumerable) |
| 63 | 71 | | { |
| 63 | 72 | | if (ReferenceEquals(tag, valueAttribute.Tag) || (tag is not null && tag.Equals(valueAttribute.Tag))) |
| 33 | 73 | | { |
| 33 | 74 | | if (found) |
| 3 | 75 | | { |
| 3 | 76 | | return (false, default); |
| | 77 | | } |
| 30 | 78 | | found = true; |
| 30 | 79 | | value = valueAttribute.Value; |
| 30 | 80 | | } |
| 60 | 81 | | } |
| 53 | 82 | | return (found, value); |
| 56 | 83 | | } |
| | 84 | | } |