コード例 #1
0
        /// <summary>
        /// 获取枚举项描述
        /// </summary>
        /// <typeparam name="T">枚举类型</typeparam>
        /// <param name="enumItem">枚举项</param>
        /// <returns>枚举项描述</returns>
        public static string GetDescription <T>(int enumItem)
        {
            EnumItemDescription item = GetEnumItemDescription <T>(enumItem);

            if (item != null)
            {
                return(item.Description);
            }

            return(string.Empty);
        }
コード例 #2
0
        /// <summary>
        /// 构造枚举项信息
        /// </summary>
        /// <param name="fi">FieldInfo</param>
        /// <param name="enumType">枚举类型</param>
        /// <returns>枚举项信息</returns>
        private static EnumItemDescription BuilderEnumItemInfo(FieldInfo fi, Type enumType)
        {
            EnumItemDescription resultItem = new EnumItemDescription();

            resultItem.Name  = fi.Name;
            resultItem.Value = (int)fi.GetValue(enumType);

            DescriptionAttribute descAttr = fi.GetCustomAttribute <DescriptionAttribute>();

            if (descAttr != null)
            {
                resultItem.Description = descAttr.Description;
            }

            return(resultItem);
        }