public static IDictionary <TEnum, TArrtibute> GetEnumDict <TEnum, TArrtibute>(params TEnum[] rankEnumKeys) where TArrtibute : System.Attribute { if (rankEnumKeys == null) { throw new ArgumentNullException("rankEnumKeys"); } List <TEnum> list = rankEnumKeys.ToList <TEnum>(); Type typeFromHandle = typeof(TEnum); Dictionary <TEnum, TArrtibute> dictionary = new Dictionary <TEnum, TArrtibute>(); FieldInfo[] fields = typeFromHandle.GetFields(); for (int i = 0; i < fields.Length; i++) { FieldInfo fieldInfo = fields[i]; if (fieldInfo.FieldType.IsEnum) { TEnum tEnum = (TEnum)((object)fieldInfo.GetValue(typeFromHandle)); if (list.Contains(tEnum)) { object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(TArrtibute), false); if (customAttributes.Length != 0) { TArrtibute value = customAttributes[0] as TArrtibute; dictionary.Add(tEnum, value); } } } } return(dictionary); }
public static IDictionary <TEnum, TArrtibute> GetEnumDict <TEnum, TArrtibute>() where TArrtibute : System.Attribute { Type typeFromHandle = typeof(TEnum); Dictionary <TEnum, TArrtibute> dictionary = new Dictionary <TEnum, TArrtibute>(); FieldInfo[] fields = typeFromHandle.GetFields(); for (int i = 0; i < fields.Length; i++) { FieldInfo fieldInfo = fields[i]; if (fieldInfo.FieldType.IsEnum) { TEnum key = (TEnum)((object)fieldInfo.GetValue(typeFromHandle)); object[] customAttributes = fieldInfo.GetCustomAttributes(typeof(TArrtibute), false); if (customAttributes.Length != 0) { TArrtibute value = customAttributes[0] as TArrtibute; dictionary.Add(key, value); } } } return(dictionary); }