Example #1
0
        public static IList <EnumDescription> GetFieldTexts(Type enumType, EnumDescription.SortType sortType)
        {
            if (!EnumDescription.EnumDescriptionCache.ContainsKey(enumType.FullName))
            {
                FieldInfo[]             fields = enumType.GetFields();
                IList <EnumDescription> list   = new List <EnumDescription>();
                FieldInfo[]             array  = fields;
                for (int i = 0; i < array.Length; i++)
                {
                    FieldInfo fieldInfo        = array[i];
                    object[]  customAttributes = fieldInfo.GetCustomAttributes(typeof(EnumDescription), false);
                    if (customAttributes.Length == 1)
                    {
                        EnumDescription enumDescription = (EnumDescription)customAttributes[0];
                        enumDescription.m_fieldIno = fieldInfo;
                        list.Add(enumDescription);
                    }
                }
                EnumDescription.EnumDescriptionCache.Add(enumType.FullName, list);
            }
            IList <EnumDescription> list2 = EnumDescription.EnumDescriptionCache[enumType.FullName];

            if (list2.Count <= 0)
            {
                throw new NotSupportedException("枚举类型[" + enumType.Name + "]未定义属性EnumValueDescription");
            }
            if (sortType != EnumDescription.SortType.Default)
            {
                for (int j = 0; j < list2.Count; j++)
                {
                    for (int k = j; k < list2.Count; k++)
                    {
                        bool flag = false;
                        switch (sortType)
                        {
                        case EnumDescription.SortType.DisplayText:
                            if (string.Compare(list2[j].Description, list2[k].Description) > 0)
                            {
                                flag = true;
                            }
                            break;

                        case EnumDescription.SortType.Rank:
                            if (list2[j].EnumRank > list2[k].EnumRank)
                            {
                                flag = true;
                            }
                            break;
                        }
                        if (flag)
                        {
                            EnumDescription value = list2[j];
                            list2[j] = list2[k];
                            list2[k] = value;
                        }
                    }
                }
            }
            return(list2);
        }
Example #2
0
        public static IList <EnumDescription> GetFieldTexts(Type enumType, SortType sortType)
        {
            if (!EnumDescriptionCache.ContainsKey(enumType.FullName))
            {
                FieldInfo[]             fields = enumType.GetFields();
                IList <EnumDescription> list   = new List <EnumDescription>();
                foreach (FieldInfo info in fields)
                {
                    object[] customAttributes = info.GetCustomAttributes(typeof(EnumDescription), false);
                    if (customAttributes.Length == 1)
                    {
                        EnumDescription item = (EnumDescription)customAttributes[0];
                        item.m_fieldIno = info;
                        list.Add(item);
                    }
                }
                EnumDescriptionCache.Add(enumType.FullName, list);
            }
            IList <EnumDescription> list2 = EnumDescriptionCache[enumType.FullName];

            if (list2.Count <= 0)
            {
                throw new NotSupportedException("枚举类型[" + enumType.Name + "]未定义属性EnumValueDescription");
            }
            if (sortType != SortType.Default)
            {
                for (int i = 0; i < list2.Count; i++)
                {
                    for (int j = i; j < list2.Count; j++)
                    {
                        bool flag = false;
                        switch (sortType)
                        {
                        case SortType.DisplayText:
                            if (string.Compare(list2[i].Description, list2[j].Description) > 0)
                            {
                                flag = true;
                            }
                            break;

                        case SortType.Rank:
                            if (list2[i].EnumRank > list2[j].EnumRank)
                            {
                                flag = true;
                            }
                            break;
                        }
                        if (flag)
                        {
                            EnumDescription description2 = list2[i];
                            list2[i] = list2[j];
                            list2[j] = description2;
                        }
                    }
                }
            }
            return(list2);
        }
Example #3
0
        public static string GetFieldText(Type enumType, object enumValue)
        {
            List <EnumDescription> list = EnumDescription.GetFieldTexts(enumType) as List <EnumDescription>;

            if (CollectionHelper.IsNullOrEmpty <EnumDescription>(list))
            {
                return(string.Empty);
            }
            EnumDescription enumDescription = list.Find((EnumDescription item) => item.EnumValue == Convert.ToInt32(enumValue));

            if (enumDescription == null)
            {
                return(string.Empty);
            }
            return(enumDescription.Description);
        }
Example #4
0
        public static string GetFieldText(object enumValue)
        {
            List <EnumDescription> list = EnumDescription.GetFieldTexts(enumValue.GetType()) as List <EnumDescription>;

            if (CollectionHelper.IsNullOrEmpty <EnumDescription>(list))
            {
                return(string.Empty);
            }
            EnumDescription enumDescription = list.Find((EnumDescription item) => item.m_fieldIno.Name.Equals(enumValue.ToString()));

            if (enumDescription == null)
            {
                return(string.Empty);
            }
            return(enumDescription.Description);
        }
Example #5
0
        public static string GetFieldText(object enumValue)
        {
            List <EnumDescription> fieldTexts = GetFieldTexts(enumValue.GetType()) as List <EnumDescription>;

            if (CollectionHelper.IsNullOrEmpty <EnumDescription>(fieldTexts))
            {
                return(string.Empty);
            }
            EnumDescription description = fieldTexts.Find(item => item.m_fieldIno.Name.Equals(enumValue.ToString()));

            if (description == null)
            {
                return(string.Empty);
            }
            return(description.Description);
        }
Example #6
0
        public static bool ExistsEnumValue(Type enumType, int enumValue)
        {
            List <EnumDescription> list = EnumDescription.GetFieldTexts(enumType) as List <EnumDescription>;

            return(!CollectionHelper.IsNullOrEmpty <EnumDescription>(list) && list.Exists((EnumDescription item) => item.EnumValue == enumValue));
        }
Example #7
0
 public static IList <EnumDescription> GetFieldTexts(Type enumType)
 {
     return(EnumDescription.GetFieldTexts(enumType, EnumDescription.SortType.Default));
 }