Esempio n. 1
0
        /// <summary>获取枚举类型的所有字段注释</summary>
        /// <param name="enumType"></param>
        /// <returns></returns>
        public static Dictionary <Int32, String> GetDescriptions(Type enumType)
        {
            var dic = new Dictionary <Int32, String>();

            foreach (FieldInfo item in enumType.GetFields(BindingFlags.Public | BindingFlags.Static))
            {
                if (!item.IsStatic)
                {
                    continue;
                }

                // 这里的快速访问方法会报错
                //FieldInfoX fix = FieldInfoX.Create(item);
                //PermissionFlags value = (PermissionFlags)fix.GetValue(null);
                Int32 value = Convert.ToInt32(item.GetValue(null));

                String des = item.Name;

                DisplayNameAttribute dna = AttributeX.GetCustomAttribute <DisplayNameAttribute>(item, false);
                if (dna != null && !String.IsNullOrEmpty(dna.DisplayName))
                {
                    des = dna.DisplayName;
                }

                DescriptionAttribute att = AttributeX.GetCustomAttribute <DescriptionAttribute>(item, false);
                if (att != null && !String.IsNullOrEmpty(att.Description))
                {
                    des = att.Description;
                }
                dic.Add(value, des);
            }

            return(dic);
        }
Esempio n. 2
0
        /// <summary>获取枚举字段的注释</summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static String GetDescription(this Enum value)
        {
            Type      type = value.GetType();
            FieldInfo item = type.GetField(value.ToString(), BindingFlags.Public | BindingFlags.Static);

            DescriptionAttribute att = AttributeX.GetCustomAttribute <DescriptionAttribute>(item, false);

            if (att != null && !String.IsNullOrEmpty(att.Description))
            {
                return(att.Description);
            }

            return(null);
        }