/// <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);
        }
示例#2
0
        private static EnumItemDescriptionList GetDescriptionListFromEnumType(Type enumType)
        {
            List <EnumItemDescription> eidList = new List <EnumItemDescription>();

            FieldInfo[] fileds = enumType.GetFields();

            for (int i = 0; i < fileds.Length; i++)
            {
                FieldInfo fi = fileds[i];

                if (fi.IsLiteral && fi.IsStatic)
                {
                    eidList.Add(EnumItemDescription.CreateFromFieldInfo(fi, enumType));
                }
            }

            eidList.Sort(delegate(EnumItemDescription x, EnumItemDescription y)
            {
                return(Math.Sign(x.SortId - y.SortId));
            }
                         );

            return(new EnumItemDescriptionList(eidList));
        }