A game about forced loneliness, made by TACStudios
1using System; 2using System.Collections.Generic; 3using System.Linq; 4using System.Reflection; 5 6namespace Unity.VisualScripting 7{ 8 public static class EnumUtility 9 { 10 public static bool HasFlag(this Enum value, Enum flag) 11 { 12 var lValue = Convert.ToInt64(value); 13 var lFlag = Convert.ToInt64(flag); 14 return (lValue & lFlag) == lFlag; 15 } 16 17 public static Dictionary<string, Enum> ValuesByNames(Type enumType, bool obsolete = false) 18 { 19 Ensure.That(nameof(enumType)).IsNotNull(enumType); 20 21 IEnumerable<FieldInfo> fields = enumType.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); 22 23 if (!obsolete) 24 { 25 fields = fields.Where(f => !f.IsDefined(typeof(ObsoleteAttribute), false)); 26 } 27 28 return fields.ToDictionary(f => f.Name, f => (Enum)f.GetValue(null)); 29 } 30 31 public static Dictionary<string, T> ValuesByNames<T>(bool obsolete = false) 32 { 33 IEnumerable<FieldInfo> fields = typeof(T).GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static); 34 35 if (!obsolete) 36 { 37 fields = fields.Where(f => !f.IsDefined(typeof(ObsoleteAttribute), false)); 38 } 39 40 return fields.ToDictionary(f => f.Name, f => (T)f.GetValue(null)); 41 } 42 } 43}