public static MyEnumInfo <TEnumValue> GetMyEnumInfo <TEnum, TEnumValue>(string key) { MyEnumInfo <TEnumValue> info = new MyEnumInfo <TEnumValue>(); Type t = typeof(TEnum); FieldInfo[] pis = t.GetFields(); foreach (var item in pis) { if (item.FieldType.Name == t.Name) { if (item.Name == key) { TEnumValue val1 = (TEnumValue)item.GetValue(t); info.Value = val1; info.Name = item.Name; MyEnumAttrAttribute attr = item.GetCustomAttribute <MyEnumAttrAttribute>(); if (attr != null) { info.Description = attr.Description; } } } } return(info); }
/// <summary> /// Get MyEnumInfo List /// </summary> /// <typeparam name="TEnum"></typeparam> /// <typeparam name="TEnumValue"></typeparam> /// <returns></returns> public static IList <MyEnumInfo <TEnumValue> > GetMyEnumInfoList <TEnum, TEnumValue>() { IList <MyEnumInfo <TEnumValue> > ls = new List <MyEnumInfo <TEnumValue> >(); Type t = typeof(TEnum); FieldInfo[] pis = t.GetFields(); foreach (var item in pis) { if (item.FieldType.Name == t.Name) { MyEnumInfo <TEnumValue> info = new MyEnumInfo <TEnumValue>(); info.Name = item.Name; info.Value = (TEnumValue)item.GetValue(t); MyEnumAttrAttribute attr = item.GetCustomAttribute <MyEnumAttrAttribute>(); if (attr != null) { info.Description = attr.Description; } ls.Add(info); } } return(ls); }