/// <summary>
        /// 获得枚举项的描述信息值,若没有定义该附加属性,则返回空串
        /// </summary>
        /// <param name="enumItem">枚举项</param>
        /// <returns>枚举项的描述信息值,若没有定义该枚举项附加属性,则返回空串</returns>
        /// <remarks>获得枚举项的描述信息值,若没有定义该枚举项附加属性,则返回空串
        /// <code source="..\Framework\TestProjects\DeluxeWorks.Library.Test\Core\EnumItemDescriptionAttributeTest.cs" region = "GetDescriptionTest" lang="cs" title="得到枚举项的描述信息属性" />
        /// </remarks>
        public static string GetDescription(System.Enum enumItem)
        {
            string result = string.Empty;

            EnumItemDescriptionAttribute attr = GetAttribute(enumItem);

            if (attr != null)
            {
                result = attr.Description;
            }

            return(result);
        }
        private void FillDescriptionAttributeInfo(EnumItemDescriptionAttribute attr)
        {
            if (attr != null)
            {
                this.Description = attr.description;
                this.ShortName   = attr.ShortName;
                this.Category    = attr.Category;

                if (attr.SortId != -1)
                {
                    this.sortId = attr.SortId;
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// 根据描述找到枚举对象的值
        /// </summary>
        /// <param name="enumType"></param>
        /// <param name="description"></param>
        /// <returns></returns>
        public static int GetEnumValue(Type enumType, string description)
        {
            EnumItemDescriptionList enumList = EnumItemDescriptionAttribute.GetDescriptionList(enumType);

            EnumItemDescription found = null;

            foreach (EnumItemDescription item in enumList)
            {
                if (item.Description == description)
                {
                    found = item;
                    break;
                }
            }

            if (found == null)
            {
                throw new ArgumentException(string.Format("无法在类型为{0}的枚举中找到描述为{1}的枚举对象", enumType.Name, description));
            }

            return(found.EnumValue);
        }
Exemple #4
0
        /// <summary>
        /// 得到某个枚举项的描述
        /// </summary>
        /// <param name="enumItem"></param>
        /// <returns></returns>
        public static string ToDescription(this System.Enum enumItem)
        {
            enumItem.NullCheck("enumItem");

            return(EnumItemDescriptionAttribute.GetDescription(enumItem));
        }
Exemple #5
0
        /// <summary>
        /// 根据值找到枚举对象的描述
        /// </summary>
        /// <param name="enumType"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetEnumDescription(Type enumType, int value)
        {
            Enum item = (Enum)Enum.ToObject(enumType, value);

            return(EnumItemDescriptionAttribute.GetDescription(item));
        }