using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace ReZero.SuperAPI { public static class EnumAttributeExtractor { /// /// Gets the attribute values of the specified enum type. /// /// The enum type. /// A list of EnumAttributeValues. public static List GetEnumAttributeValues() where T : Enum { var values = Enum.GetValues(typeof(T)).Cast().ToList(); List attributeValuesList = new List(); foreach (var value in values) { var enumType = typeof(T); var fieldInfo = enumType.GetField(value.ToString()); var chineseTextAttribute = GetCustomAttribute(fieldInfo); var englishTextAttribute = GetCustomAttribute(fieldInfo); var textGroupAttribute = GetCustomAttribute(fieldInfo); var attributeValues = new EnumAttributeValues { Value=Convert.ToInt64(value), Text = App.Language == Language.CN ? chineseTextAttribute?.Text:englishTextAttribute?.Text, TextGroup = App.Language == Language.CN ? textGroupAttribute?.cnText : textGroupAttribute?.enText }; attributeValuesList.Add(attributeValues); } return attributeValuesList; } private static T GetCustomAttribute(FieldInfo fieldInfo) where T : Attribute { return (T)Attribute.GetCustomAttribute(fieldInfo, typeof(T)); } /// /// Represents the values of an enum attribute. /// public class EnumAttributeValues { /// /// Gets or sets the Chinese text. /// public string? Text { get; set; } /// /// Gets or sets the text group. /// public string? TextGroup { get; set; } /// /// Enum value /// public long Value { get; set; } } } }