1、定义枚举
/// <summary> /// 订单状态 /// </summary> public enum OrderState { 待支付 = 1, 待处理 = 2, 配货中 = 3, 已发货 = 4, 已取消 = 5, 已签收 = 6, 配送失败 = 7 }
public enum ImageFoderType { [Description("Temp")] 临时图片 = 0, [Description("GoodsImg")] 商品图片 = 1, [Description("GoodsColorImg")] 颜色图片 = 2, [Description("banner")] 广告图片 = 3, [Description("ShowImg")] 晒单图片 = 4 }
2、根据枚举值获取枚举名称
public static string GetOrderState(int OrderStateId) { string _name = string.Empty; int _status = 0; if (OrderStateId > 0) { _status = OrderStateId.ToString().ToInt(); } if (Enum.IsDefined(typeof(Enums.OrderState), _status)) { _name = ((Enums.OrderState)_status).ToString(); } return _name; }
3、获取枚举对应说明文字
/// <summary> 获得某个Enum类型的中文,从description中读取 /// </summary> /// <param name="enumType">枚举的类型</param> /// <param name="val">传入的值</param> /// <returns></returns> public static string GetEnumCNNameFromEnum(Type enumType, object val) { string enumvalue = Enum.GetName(enumType, val); if (String.IsNullOrEmpty(enumvalue)) return ""; FieldInfo finfo = enumType.GetField(enumvalue); object[] cAttr = finfo.GetCustomAttributes(typeof(DescriptionAttribute), true); if (cAttr.Length > 0) { DescriptionAttribute desc = cAttr[0] as DescriptionAttribute; if (desc != null) { return desc.Description; } } return enumvalue; }