using System; using System.Collections.Generic; using System.Linq; using System.Reflection; namespace VCommon.Reflection { public abstract class EnumBind { public static IReadOnlyDictionary BindedTypes { get; } public static IReadOnlyDictionary InvertMap { get; } private static readonly HashSet Fields; public static bool Contains(T value) => Fields.Contains(value); static EnumBind() { if (false == typeof(T).IsEnum) throw new VCommonException("Generic type argument must be Enum."); BindedTypes = typeof(T).GetFields().Where(p => p.FieldType == typeof(T)) .ToDictionary( k => (T)k.GetRawConstantValue(), p => p.GetCustomAttributes(typeof(EnumBindAttribute)) .OfType() .Select(t => t.BindType) .Single(t => typeof(TBase).IsAssignableFrom(t)) ); InvertMap = BindedTypes.ToDictionary(p => p.Value, p => p.Key); Fields = new HashSet(BindedTypes.Keys); } } }