调用:
typeof(EnterPriseState).GetDescriptionByEnmuAndValue(item.IsAudit.ParseToInt())
方法:
public static string GetDescriptionByEnmuAndValue(this Type enumType,int value) { var StateList = enumType.EnumToDictionary(); var stateEnmu = StateList.First(es => es.Key == value); return stateEnmu.Value; }
public static Dictionary<int, string> EnumToDictionary(this Type enumType) { Dictionary<int, string> dictionary = new Dictionary<int, string>(); Type typeDescription = typeof(DescriptionAttribute); FieldInfo[] fields = enumType.GetFields(); int sValue = 0; string sText = string.Empty; foreach (FieldInfo field in fields) { if (field.FieldType.IsEnum) { sValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null)); object[] arr = field.GetCustomAttributes(typeDescription, true); if (arr.Length > 0) { DescriptionAttribute da = (DescriptionAttribute)arr[0]; sText = da.Description; } else { sText = field.Name; } dictionary.Add(sValue, sText); } } return dictionary; }