static ReflectionCache() { foreach (FieldInfo field in typeof(T).GetFields(BindingFlags.Public | BindingFlags.Static)) { // Get attribute ReflectionCacheAttribute attribute = field.GetCustomAttribute <ReflectionCacheAttribute>(); if (attribute == null) { continue; } // Append to cache dictionary.Add((T)field.GetValue(null), attribute.Type); } }
/// <summary> /// Constructor /// </summary> /// <typeparam name="EnumType">Enum type</typeparam> public static ReflectionCache <T> CreateFromEnum <EnumType>() where EnumType : struct, IConvertible { TR.Enter(); Type enumType = typeof(EnumType); if (!enumType.GetTypeInfo().IsEnum) { throw new ArgumentException("K must be an enumerated type"); } // Cache all types ReflectionCache <T> r = new ReflectionCache <T>(); foreach (object t in Enum.GetValues(enumType)) { // Get enumn member MemberInfo[] memInfo = enumType.GetMember(t.ToString()); if (memInfo == null || memInfo.Length != 1) { throw (new FormatException()); } // Get attribute ReflectionCacheAttribute attribute = memInfo[0].GetCustomAttributes(typeof(ReflectionCacheAttribute), false) .Cast <ReflectionCacheAttribute>() .FirstOrDefault(); if (attribute == null) { throw (new FormatException()); } // Append to cache r.Add((T)t, attribute.Type); TR.Exit(); } return(r); }