Пример #1
0
 public EnumHelperT(Type enumType)
 {
     this._EnumParameterList = null;
     this._EnumParameterList = new List <EnumParameter>();
     foreach (string str in Enum.GetNames(enumType))
     {
         foreach (Attribute attribute in enumType.GetField(str).GetCustomAttributes(typeof(EnumAttribute), false))
         {
             EnumAttribute attribute2 = attribute as EnumAttribute;
             if (attribute2 != null)
             {
                 EnumParameter item = new EnumParameter {
                     Key         = str,
                     Description = attribute2.Description
                 };
                 if (attribute2.Value == -2147483648)
                 {
                     item.Value = Convert.ToInt32(Enum.Parse(enumType, str));
                 }
                 else
                 {
                     item.Value = attribute2.Value;
                 }
                 this._EnumParameterList.Add(item);
                 break;
             }
         }
     }
 }
Пример #2
0
        public static List <EnumParameter> GetEnumMembers(this Type type)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type", "输入的枚举类型为空");
            }
            List <EnumParameter> list = new List <EnumParameter>();

            foreach (string str in Enum.GetNames(type))
            {
                foreach (Attribute attribute in type.GetField(str).GetCustomAttributes(typeof(EnumAttribute), false))
                {
                    EnumAttribute attribute2 = attribute as EnumAttribute;
                    if (attribute2 != null)
                    {
                        EnumParameter item = new EnumParameter {
                            Key         = str,
                            Description = attribute2.Description
                        };
                        if (attribute2.Value == -2147483648)
                        {
                            item.Value = Convert.ToInt32(Enum.Parse(type, str));
                        }
                        else
                        {
                            item.Value = attribute2.Value;
                        }
                        list.Add(item);
                        break;
                    }
                }
            }
            return(list);
        }
Пример #3
0
        public static string GetEnumDescription(this string typeName, string assemblyName, string objEnumKey)
        {
            EnumParameter parameter = typeName.GetEnumMembers(assemblyName).FirstOrDefault <EnumParameter>(p => p.Key == objEnumKey);

            if (parameter != null)
            {
                return(parameter.Description);
            }
            return(string.Empty);
        }
Пример #4
0
        public static string GetEnumDescription(this Type type, int objEnumValue)
        {
            EnumParameter parameter = type.GetEnumMembers().FirstOrDefault <EnumParameter>(p => p.Value == objEnumValue);

            if (parameter != null)
            {
                return(parameter.Description);
            }
            return(string.Empty);
        }
Пример #5
0
        public static string GetEnumDescription(this Enum objEnum)
        {
            EnumParameter parameter = objEnum.GetType().GetEnumMembers().FirstOrDefault <EnumParameter>(p => p.Key == objEnum.ToString());

            if (parameter != null)
            {
                return(parameter.Description);
            }
            return(string.Empty);
        }
Пример #6
0
        public static int GetEnumValue(this string typeName, string assemblyName, string objEnumKey)
        {
            EnumParameter parameter = typeName.GetEnumMembers(assemblyName).FirstOrDefault <EnumParameter>(p => p.Key == objEnumKey);

            if (parameter != null)
            {
                return(parameter.Value);
            }
            return(-2147483648);
        }
Пример #7
0
        public string GetEnumDescription(string objEnumKey)
        {
            EnumParameter parameter = this._EnumParameterList.FirstOrDefault <EnumParameter>(p => p.Key == objEnumKey);

            if (parameter != null)
            {
                return(parameter.Description);
            }
            return(string.Empty);
        }
Пример #8
0
 private static string GetShowText(EnumParameter objEnumParameter, ShowEnumType show)
 {
     if (show == ShowEnumType.Key)
     {
         return(objEnumParameter.Key);
     }
     if (show == ShowEnumType.Description)
     {
         return(objEnumParameter.Description);
     }
     return(objEnumParameter.Value.ToString());
 }
Пример #9
0
        public static List <EnumParameter> GetEnumMembers(this string typeName, string assemblyName)
        {
            Type enumType = assemblyName.FindType(typeName);

            if (enumType == null)
            {
                throw new ArgumentNullException("typeName", "输入的类型系统无法找到请重新输入");
            }
            List <EnumParameter> list = new List <EnumParameter>();

            foreach (string str in Enum.GetNames(enumType))
            {
                foreach (Attribute attribute in enumType.GetField(str).GetCustomAttributes(typeof(EnumAttribute), false))
                {
                    EnumAttribute attribute2 = attribute as EnumAttribute;
                    if (attribute2 != null)
                    {
                        EnumParameter item = new EnumParameter {
                            Key         = str,
                            Description = attribute2.Description
                        };
                        if (attribute2.Value == -2147483648)
                        {
                            item.Value = Convert.ToInt32(Enum.Parse(enumType, str));
                        }
                        else
                        {
                            item.Value = attribute2.Value;
                        }
                        list.Add(item);
                        break;
                    }
                }
            }
            return((from s in list
                    orderby s.Value
                    select s).ToList <EnumParameter>());
        }