Beispiel #1
0
        public static List <EnumObject> GetEnumInt32List(Type type)
        {
            List <EnumObject> lst = new List <EnumObject>();
            //DataTable dtbl = new DataTable();
            //dtbl.Columns.Add(EnumValueField, typeof(Int32));
            //dtbl.Columns.Add(EnumDescriptionField, typeof(String));
            //dtbl.Columns.Add(EnumNameField, typeof(String));
            EnumObject drow;

            FieldInfo[] fields = type.GetFields();
            Int32       count  = fields.Length;

            for (Int32 i = 0; i < count; i++)
            {
                drow = new EnumObject();
                if (fields[i].Name == "value__")
                {
                    continue;
                }
                drow = new EnumObject();
                FieldInfo field = fields[i];
                //值列
                try
                {
                    drow.Value = ((Int32)Enum.Parse(type, field.Name)).ToString();
                }
                catch
                {
                    drow.Value = Int32.MinValue.ToString();
                }
                //Enum.t
                //enum字段名称
                drow.Name = field.Name;

                object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (objs == null || objs.Length == 0)
                {
                    drow.Description = field.Name;
                }
                else
                {
                    DescriptionAttribute da = (DescriptionAttribute)objs[0];
                    drow.Description = da.Description;
                }

                lst.Add(drow);
            }
            return(lst);
        }
Beispiel #2
0
        public static ICollection <EnumObject> GetEnumObject(Type type)
        {
            ICollection <EnumObject> lst = new List <EnumObject>();

            if (type == null)
            {
                return(lst);
            }
            FieldInfo[] fields = type.GetFields();
            Int32       count  = fields.Length;

            for (Int32 i = 0; i < count; i++)
            {
                if (fields[i].Name == "value__")
                {
                    continue;
                }
                EnumObject enumObject = new EnumObject();
                FieldInfo  field      = fields[i];
                //值列
                try
                {
                    enumObject.Value = ((Int32)Enum.Parse(type, field.Name)).ToString();
                }
                catch
                {
                    enumObject.Value = Int32.MinValue.ToString();
                }
                //Enum.t
                //enum字段名称
                enumObject.Name = field.Name;

                object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);
                if (objs == null || objs.Length == 0)
                {
                    enumObject.Description = field.Name;
                }
                else
                {
                    DescriptionAttribute da = (DescriptionAttribute)objs[0];
                    enumObject.Description = da.Description;
                }

                lst.Add(enumObject);
            }
            return(lst);
        }