public static TAttr TryGetAttribute <TAttr>(TEnum value) where TAttr : Attribute { EnumFieldInfo <TEnum> field = TryGetField(value); return(field?.GetAttribute <TAttr>()); }
static EnumInfo() { Type = typeof(TEnum); UnderlyingType = Enum.GetUnderlyingType(Type); UnderlyingTypeSize = Marshal.SizeOf(UnderlyingType); IsUInt64 = UnderlyingType == typeof(ulong); if (!typeof(TEnum).IsEnum) { throw new ArgumentException($"{Type.Name} is not an enum!", nameof(TEnum)); } nameLookup = new Dictionary <string, EnumFieldInfo <TEnum> >(); valueLookup = new Dictionary <TEnum, EnumFieldInfo <TEnum> >(); foreach (FieldInfo field in Type.GetFields(BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic)) { var info = new EnumFieldInfo <TEnum>(field, IsUInt64, UnderlyingTypeSize); nameLookup.Add(info.Name, info); if (!valueLookup.ContainsKey(info.Value)) { valueLookup.Add(info.Value, info); } } }