jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
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
118
119
120
121
namespace HW.Utility.Enums
{
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
 
    public static class EnumHelper
    {
        public static void BindEnumToComboBox<T>(ComboBox cb) where T: struct
        {
            BindEnumToComboBox<T>(cb, null, null);
        }
 
        public static void BindEnumToComboBox<T>(ComboBox cb, string firstItemText, string firstItemValue) where T: struct
        {
            List<KeyValuePair<string, string>> list = LoadEnum<T>().ConvertAll<KeyValuePair<string, string>>(delegate (KeyValuePair<T, EnumMapperAttribute> _input) {
                return new KeyValuePair<string, string>(_input.Key.ToString(), (_input.Value != null) ? _input.Value.Description : _input.Key.ToString());
            });
            if ((firstItemText != null) && (firstItemValue != null))
            {
                KeyValuePair<string, string> item = new KeyValuePair<string, string>(firstItemValue, firstItemText);
                list.Insert(0, item);
            }
            cb.DataSource = list;
            cb.DisplayMember = "Value";
            cb.ValueMember = "Key";
        }
 
        public static bool CheckIsTypeEnum(System.Type enumType, bool isThrowException)
        {
            if (enumType == null)
            {
                if (isThrowException)
                {
                    throw new NullReferenceException(string.Format("类型参数{0}不能为空", "enumType"));
                }
                return false;
            }
            if (!enumType.IsEnum)
            {
                if (isThrowException)
                {
                    throw new Exception(string.Format("类型{0}必须要为枚举", enumType.FullName));
                }
                return false;
            }
            return true;
        }
 
        public static string LoadDescription<T>(object obj) where T: struct
        {
            if (CheckIsTypeEnum(typeof(T), true))
            {
                return LoadDescription<T>((T) Enum.Parse(typeof(T), obj.ToString()));
            }
            return "";
        }
 
        public static string LoadDescription<T>(T t) where T: struct
        {
            string str = t.ToString();
            foreach (KeyValuePair<T, EnumMapperAttribute> pair in LoadEnum<T>())
            {
                if (pair.Key.Equals(t))
                {
                    return pair.Value.Description;
                }
            }
            return str;
        }
 
        public static List<KeyValuePair<T, EnumMapperAttribute>> LoadEnum<T>() where T: struct
        {
            Action<T> action = null;
            List<KeyValuePair<T, EnumMapperAttribute>> list = new List<KeyValuePair<T, EnumMapperAttribute>>();
            if (CheckIsTypeEnum(typeof(T), true))
            {
                if (action == null)
                {
                    action = delegate (T _enumData) {
                        list.Add(new KeyValuePair<T, EnumMapperAttribute>(_enumData, EnumMapperAttribute.GetSelf(typeof(T).GetField(_enumData.ToString()))));
                    };
                }
                Array.ForEach<T>(Enum.GetValues(typeof(T)) as T[], action);
            }
            return list;
        }
 
        public static T LoadEnumComboBoxCurrentSelect<T>(ComboBox cb) where T: struct
        {
            T local = default(T);
            KeyValuePair<string, string> selectedItem = (KeyValuePair<string, string>) cb.SelectedItem;
            string key = selectedItem.Key;
            if (Enum.IsDefined(typeof(T), key))
            {
                local = (T) Enum.Parse(typeof(T), key);
            }
            return local;
        }
 
        public static EnumMapperAttribute LoadMapper<T>(object obj) where T: struct
        {
            return LoadMapper<T>((T) Enum.Parse(typeof(T), obj.ToString()));
        }
 
        public static EnumMapperAttribute LoadMapper<T>(T t) where T: struct
        {
            if (CheckIsTypeEnum(typeof(T), true))
            {
                foreach (KeyValuePair<T, EnumMapperAttribute> pair in LoadEnum<T>())
                {
                    if (pair.Key.Equals(t))
                    {
                        return pair.Value;
                    }
                }
            }
            return null;
        }
    }
}