using System; namespace SabreTools.RedumpLib.Legacy.Attributes { public static class AttributeHelper { /// /// Get the HumanReadableAttribute from a supported value /// /// Value to use /// HumanReadableAttribute attached to the value public static HumanReadableAttribute? GetHumanReadableAttribute(T value) { // Null value in, null value out if (value is null) return null; // Current enumeration type var enumType = typeof(T); if (Nullable.GetUnderlyingType(enumType) is not null) enumType = Nullable.GetUnderlyingType(enumType); // If the value returns a null on ToString, just return null string? valueStr = value?.ToString(); if (string.IsNullOrEmpty(valueStr)) return null; // Get the member info array var memberInfos = enumType?.GetMember(valueStr); if (memberInfos is null) return null; // Get the enum value info from the array, if possible var enumValueMemberInfo = Array.Find(memberInfos, m => m.DeclaringType == enumType); if (enumValueMemberInfo is null) return null; // Try to get the relevant attribute var attributes = enumValueMemberInfo.GetCustomAttributes(typeof(HumanReadableAttribute), true); if (attributes is null || attributes.Length == 0) return null; // Return the first attribute, if possible return attributes[0] as HumanReadableAttribute; } } }