Exemple #1
0
        public static EnumTitleAttribute GetEnumTitleAttribute(Enum e, Enum language = null)
        {
            if (e == null)
            {
                return(null);
            }
            string[]           valueArray = e.ToString().Split(ENUM_TITLE_SEPARATOR.ToArray(), StringSplitOptions.RemoveEmptyEntries);
            Type               type       = e.GetType();
            EnumTitleAttribute ret        = null;

            foreach (string enumValue in valueArray)
            {
                System.Reflection.FieldInfo fi = type.GetField(enumValue.Trim());
                if (fi == null)
                {
                    continue;
                }
                EnumTitleAttribute[] attrs = fi.GetCustomAttributes(typeof(EnumTitleAttribute), false) as EnumTitleAttribute[];
                if (attrs != null && attrs.Length > 0)
                {
                    ret = attrs[0];
                    break;
                }
            }
            return(ret);
        }
Exemple #2
0
        public static Dictionary <T, EnumTitleAttribute> GetItemAttributeList <T>(Enum language = null) where T : struct
        {
            if (!typeof(T).IsEnum)
            {
                throw new Exception("参数必须是枚举!");
            }
            Dictionary <T, EnumTitleAttribute> ret = new Dictionary <T, EnumTitleAttribute>();

            Array array = typeof(T).GetEnumValues();

            foreach (object t in array)
            {
                EnumTitleAttribute att = GetEnumTitleAttribute(t as Enum, language);
                if (att != null)
                {
                    ret.Add((T)t, att);
                }
            }

            return(ret);
        }