C# Enum 添加自定义Attribute,然后通过泛型与反射的方式得到事先定义的标记

  

  

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
<br><br>这是测试用的代码,               private void button3_Click(object sender, EventArgs e)
        {
            foreach (var v in Enum.GetValues(typeof(AwardsType)))
            {
                object obj = FunEnum.GetCustomAttribute<AwardsType>((AwardsType)v);
 
                EnumDescription enumDes = obj as EnumDescription;
                if (enumDes != null)
                {
                    Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff")
                        + "    DisplayText=" + enumDes.DisplayText
                        + ",  DisplayTextEx=" + enumDes.DisplayTextEx);
                }
            }
 
 
            foreach (var v in Enum.GetValues(typeof(AwardsType)))
            {
                object obj = FunEnum.GetCustomAttribute<AwardsType>((AwardsType)v);
 
                EnumDescription enumDes = obj as EnumDescription;
                if (enumDes != null)
                {
                    Debug.WriteLine(DateTime.Now.ToString("HH:mm:ss.fff")
                        + "    DisplayText=" + enumDes.DisplayText
                        + ",  DisplayTextEx=" + enumDes.DisplayTextEx);
                }
            }
 
        }
<br>
<br><br>以下为定义的类与静态函数namespace WindowsFormsApplication1
{
    public enum AwardsType
    {
        [EnumDescription(DisplayText = "世界级", DisplayTextEx = "AA")]
        World = 1,
 
        [EnumDescription(DisplayText = "国家级", DisplayTextEx = "BB")]
        Country = 2,
 
        [EnumDescription(DisplayText = "省市级", DisplayTextEx = "CC")]
        Provinces = 3,
 
        [EnumDescription(DisplayText = "校级", DisplayTextEx = "DD")]
        School = 4,
    };
 
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Enum)]
    public class EnumDescription : Attribute
    {
        public string DisplayText;
 
        public string DisplayTextEx;
    }
 
 
    public static class FunEnum
    {
        // 此处做一个dictionary,这样原则上每个enum只要反射一次就可以得到想要的值
        static Dictionary<string, object> dictEnum = new Dictionary<string, object>();
 
        public static object GetCustomAttribute<T>(T t) where T : struct
        {
            Type enumType = t.GetType();
 
            if (!enumType.IsEnum)
            {
                return null;
            }
 
            string sKey = enumType.ToString() + "." + t.ToString();
 
            if (!dictEnum.ContainsKey(sKey))
            {
                FieldInfo[] infos = enumType.GetFields();
 
                foreach (FieldInfo fi in infos)
                {
                    string sTempKey = enumType.ToString() + "." + fi.GetValue(t);
                    Debug.WriteLine("sTempKey= " + sTempKey);
 
                    if (dictEnum.ContainsKey(sTempKey)) continue;
 
                    object[] eds = fi.GetCustomAttributes(true);
 
                    if (eds != null && eds.Length > 0)
                    {
                        dictEnum.Add(sTempKey, eds[0]);
                    }
                    else
                    {
                        dictEnum.Add(sTempKey, null);
                    }
                }
            }
 
 
            if (dictEnum.ContainsKey(sKey))
            {
                return dictEnum[sKey];
            }
            else
            {
                return null;
            }
 
        }
 
    }
}

  

  

posted on   LongHuaiYu  阅读(784)  评论(0编辑  收藏  举报

努力加载评论中...
点击右上角即可分享
微信分享提示