获取枚举值

调用:

 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; }

 

posted @ 2020-12-17 20:05  阿旭92312  阅读(257)  评论(0编辑  收藏  举报