獲取根據枚舉名稱獲取枚舉描述


     /// <summary>
        /// 獲取根據枚舉名稱獲取枚舉描述
        /// </summary>
        /// <typeparam name="T">枚舉類型</typeparam>
        /// <param name="enumName">枚舉名稱</param>
        /// <returns></returns>
        public static string GetEnumDescription<T>(string enumName){

            string result = string.Empty;
            System.Reflection.FieldInfo field = typeof(T).GetField(enumName);
            if (field != null)
            {
                object[] objs = field.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
                if (objs == null || objs.Length == 0)
                    result = enumName;
                else
                {
                    System.ComponentModel.DescriptionAttribute da = (System.ComponentModel.DescriptionAttribute)objs[0];
                    result = da.Description;
                }
            }
            else { result = enumName; }
            return result;
        }


        /// <summary>
        /// 獲取枚舉的description
        /// </summary>
        /// <param name="enumSubitem">枚舉值</param>
        /// <returns>枚舉對應的介紹</returns>
        public static string GetEnumDescription(object enumSubitem)
        {
            enumSubitem = (Enum)enumSubitem;
            string strValue = enumSubitem.ToString();

            FieldInfo fieldinfo = enumSubitem.GetType().GetField(strValue);

            if (fieldinfo != null)
            {

                Object[] objs = fieldinfo.GetCustomAttributes(typeof(DescriptionAttribute), false);

                if (objs == null || objs.Length == 0)
                {
                    return strValue;
                }
                else
                {
                    DescriptionAttribute da = (DescriptionAttribute)objs[0];
                    return da.Description;
                }
            }
            else
            {
                return "不限";
            }

        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM