ningshuxia
12 小时以前 71c12ff40d58c3dbdde6867396dd99224e57fc32
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
using System.Reflection;
 
namespace HStation.WinFrmUI
{
    /// <summary>
    /// 字符常量辅助类
    /// </summary>
    public class ConstStringHelper
    {
        /// <summary>
        /// 获取常量列表
        /// </summary>
        /// <returns></returns>
        public static SortedList<string, string> GetSortedList<T>()
        {
            var list = new SortedList<string, string>();
            Type type = typeof(T);
            var constList = BindingFlags.Static | BindingFlags.Public;
            var fields = type.GetFields(constList);
            foreach (var item in fields)
            {
                list.Add(item.Name, item.GetValue(null).ToString());
            }
            return list;
        }
 
        //获取常量名列表
        public static List<string> GetKeys<T>()
        {
            var list = new List<string>();
            Type type = typeof(T);
            var constList = BindingFlags.Static | BindingFlags.Public;
            var fields = type.GetFields(constList);
            if (fields == null)
                return default;
            return fields.Select(x => x.GetRawConstantValue().ToString()).ToList();
        }
 
        //获取常量名列表
        public static List<string> GetValues<T>()
        {
            var list = new List<string>();
            Type type = typeof(T);
            var constList = BindingFlags.Static | BindingFlags.Public;
            var fields = type.GetFields(constList);
            if (fields == null)
                return default;
            return fields.Select(x => x.Name).ToList();
        }
 
        //获取常量值
        public static string GetValue<T>(string key)
        {
            Type type = typeof(T);
            var list = BindingFlags.Static | BindingFlags.Public;
            var fields = type.GetFields(list);
            foreach (var item in fields)
            {
                if (item.GetRawConstantValue().ToString() == key)
                    return item.Name;
            }
            return default;
        }
    }
}