using System; using System.Collections.Generic; using System.Linq; namespace Vision.Core.Extensions { public static class EnumExtensions { public static IEnumerable GetValues() => Enum.GetValues(typeof(T)).Cast(); public static IEnumerable GetValuesCasted() => Enum.GetValues(typeof(TEnumType)).Cast(); public static T GetValueOrDefault(string name, T @default) where T : struct, IConvertible { var enumType = typeof(T); if (!enumType.IsEnum) { throw new Exception("T must be an Enumeration type"); } return Enum.TryParse(name, true, out T val) ? val : @default; } public static T GetValueOrDefault(byte number, T @default) where T : struct, IConvertible { var enumType = typeof(T); if (!enumType.IsEnum) { throw new Exception("T must be an Enumeration type"); } if (Enum.IsDefined(enumType, number)) { return (T)Enum.ToObject(enumType, number); } return @default; } public static T GetValueOrDefault(int number, T @default) where T : struct, IConvertible { var enumType = typeof(T); if (!enumType.IsEnum) { throw new Exception("T must be an Enumeration type"); } if (Enum.IsDefined(enumType, number)) { return (T) Enum.ToObject(enumType, number); } return @default; } } }