获取枚举类型的Description

publicenumDictOrderStatus
    {
        /// <summary>
        /// 运行中
        /// </summary>
        [Description("运行中")]
        Running = 1,
        /// <summary>
        /// 已关闭
        /// </summary>
        [Description("已关闭")]
        Stopped = 0
    }
 
 
    publicstaticclassEnumHelper
    {
        /// <summary>
        /// 获取指定枚举的Description信息
        /// </summary>
        /// <param name="e">枚举值</param>
        /// <returns>Description</returns>
        publicstaticstringGetEnumDescription<TEnum>(TEnum e)
        {
            FieldInfo fielInfo = e.GetType().GetField(e.ToString());
            object[] objs = fielInfo.GetCustomAttributes(true);
            if(objs.Length <= 0)
                returnstring.Empty;
            DescriptionAttribute desAttr = objs[0] asDescriptionAttribute;
            returndesAttr.Description;
        }
    }
  //调用
  intorderStatus = 1;
  string statusString = EnumHelper.GetEnumDescription((DictOrderStatus) orderStatus);
posted @ 2010-11-25 09:50  TNTZWC  阅读(365)  评论(0编辑  收藏  举报