22
zongzhibin
2024-11-24 5ff68c7d3a3ced4a9e1fbce1d739b545c0f28196
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
 
using iWareModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
namespace iWareCommon.Utils
{
    /// <summary>
    /// 枚举类
    /// </summary>
    public class EnumberHelper
    {
        /// <summary>
        /// 获取转换枚举值的字符串
        /// </summary>
        /// <param name="mode"></param>
        /// <returns></returns>
        public static string GetEnumName<T>(int mode) where T : struct
        {
            T t = default(T);
            bool isRight = EnumberHelper.GetEnumObject<T>(mode.ToString(), out t);
            if (isRight)
            {
                return t.ToString();
            }
            return "";
        }
 
        ///// <summary>
        ///// 获取转换枚举值的字符串
        ///// </summary>
        ///// <param name="mode"></param>
        ///// <returns></returns>
        //public static T GetEnum<T>(int mode) where T : struct
        //{
        //    T t = default(T);
        //    bool isRight = EnumberHelper.GetEnumObject<T>(mode.ToString(), out t);
        //    if (isRight)
        //    {
        //        return t;
        //    }
        //    throw new Exception("转换失败");
        //}
 
        /// <summary>
        /// 根据枚举值转换枚举
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        public static bool GetEnumObject<T>(string enumValue, out T t) where T : struct
        {
            bool right = Enum.TryParse(enumValue.ToString(), out t);
            if (right)
            {
                // 需要通过此方法再次确定是否是枚举实际定义的内容
                if (!Enum.IsDefined(typeof(T), t))
                {
                    return false;
                }
                return true;
            }
            else
            {
                return false;
            }
        }
 
        /// <summary>
        /// 枚举转集合
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<EnumberEntity> EnumToList<T>()
        {
            List<EnumberEntity> list = new List<EnumberEntity>();
 
            foreach (var e in Enum.GetValues(typeof(T)))
            {
                EnumberEntity m = new EnumberEntity();
                object[] objArr = e.GetType().GetField(e.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (objArr != null && objArr.Length > 0)
                {
                    DescriptionAttribute da = objArr[0] as DescriptionAttribute;
                    m.Desction = da.Description;
                }
                m.EnumValue = Convert.ToInt32(e);
                m.EnumName = e.ToString();
                list.Add(m);
            }
            return list;
        }
 
        /// <summary>
        /// 获取枚举的描述属性
        /// </summary>
        /// <param name="enumValue">枚举值</param>
        /// <returns>枚举的描述属性</returns>
        public static string GetEnumDescription(Enum en)
        {
            Type type = en.GetType();   //获取类型
            MemberInfo[] memberInfos = type.GetMember(en.ToString());   //获取成员
            if (memberInfos != null && memberInfos.Length > 0)
            {
                DescriptionAttribute[] attrs = memberInfos[0].GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];   //获取描述特性
 
                if (attrs != null && attrs.Length > 0)
                {
                    return attrs[0].Description;    //返回当前描述
                }
            }
            return en.ToString();
        }
 
    }
}