实用技巧——通用枚举状态转换

获取枚举类型的Description,用于转换状态

 

   public enum DictOrderStatus
    {
        /// <summary>
        /// 运行中
        /// </summary>
        [Description("运行中")]
        Running = 1,
        /// <summary>
        /// 已关闭
        /// </summary>
        [Description("已关闭")]
        Stopped = 0
    }


    public static class EnumHelper
    {
        /// <summary>
        /// 获取指定枚举的Description信息
        /// </summary>
        /// <param name="e">枚举值</param>
        /// <returns>Description</returns>
        public static string GetEnumDescription<TEnum>(TEnum e)
        {
            FieldInfo fielInfo = e.GetType().GetField(e.ToString());
            object[] objs = fielInfo.GetCustomAttributes(true);
            if (objs.Length <= 0)
                return string.Empty;
            DescriptionAttribute desAttr = objs[0] as DescriptionAttribute;
            return desAttr.Description;
        }
    }
  //调用
  int orderStatus = 1;
  string statusString = EnumHelper.GetEnumDescription((DictOrderStatus) orderStatus);
posted @ 2010-07-01 13:32  曼阳  阅读(1429)  评论(0编辑  收藏  举报