代码改变世界

Enum.Has 续:public static class GenericEnumExtensions

  Nana's Lich  阅读(580)  评论(1编辑  收藏  举报
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
.assembly GenericEnumExtensions
{
  .custom instance void [mscorlib]System.Runtime.Versioning.TargetFrameworkAttribute::.ctor(string) = ( 01 00 29 2E 4E 45 54 46 72 61 6D 65 77 6F 72 6B   // ..).NETFramework
                                                                                                        2C 56 65 72 73 69 6F 6E 3D 76 34 2E 30 2C 50 72   // ,Version=v4.0,Pr
                                                                                                        6F 66 69 6C 65 3D 43 6C 69 65 6E 74 01 00 54 0E   // ofile=Client..T.
                                                                                                        14 46 72 61 6D 65 77 6F 72 6B 44 69 73 70 6C 61   // .FrameworkDispla
                                                                                                        79 4E 61 6D 65 1F 2E 4E 45 54 20 46 72 61 6D 65   // yName..NET Frame
                                                                                                        77 6F 72 6B 20 34 20 43 6C 69 65 6E 74 20 50 72   // work 4 Client Pr
                                                                                                        6F 66 69 6C 65 )                                  // ofile
 
  .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 )
  //.custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78   // ....T..WrapNonEx
  //                                                                                                           63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 )       // ceptionThrows.
  .custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 )
  //.hash algorithm 0x00008004
  //.ver 0:0:0:0
}
 
.class public abstract auto ansi sealed beforefieldinit GenericEnumExtensions.GenericEnumExt
       extends [mscorlib]System.Object
{
  .custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 )
 
    .method public hidebysig static bool  Has<valuetype .ctor ([mscorlib]System.Enum) T>(!!T target, !!T flag) cil managed
    {
        .custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 )
 
        .maxstack  8
        ldarga     0
        ldarg.1
        box        !!T
        constrained. !!T
        callvirt   instance bool [mscorlib]System.Enum::HasFlag(class [mscorlib]System.Enum)
        ret
    }
 
 
    .method public hidebysig static valuetype [mscorlib]System.Nullable`1<!!T>
                    ParseEnum<valuetype .ctor ([mscorlib]System.Enum) T>(string target) cil managed
    {
        .custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 )
 
        .maxstack  8
        ldarg.0
        ldc.i4.0
        call       valuetype [mscorlib]System.Nullable`1<!!T> GenericEnumExtensions.GenericEnumExt::ParseEnum<!!T>(string, bool)
        ret
    }
 
    .method public hidebysig static valuetype [mscorlib]System.Nullable`1<!!T>
                    ParseEnum<valuetype .ctor ([mscorlib]System.Enum) T>(string target, bool ignoreCase) cil managed
    {
        .custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 )
 
        .maxstack  3
        .locals init (valuetype [mscorlib]System.Nullable`1<!!T>, valuetype [mscorlib]System.Nullable`1<!!T>)
        .try
        {
            ldtoken    !!T
            call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
            ldarg.0
            ldarg.1
            call       object [mscorlib]System.Enum::Parse(class [mscorlib]System.Type, string, bool)
            unbox.any  !!T
            newobj     instance void valuetype [mscorlib]System.Nullable`1<!!T>::.ctor(!0)
            stloc.0
            leave.s    L_Return
        }
        catch [mscorlib]System.Object
        {
            pop
            ldloca     1
            initobj    valuetype [mscorlib]System.Nullable`1<!!T>
            ldloc.1
            stloc.0
            leave.s    L_Return
        }
        L_Return: 
        ldloc.0
        ret
    }
 
  //========//========//========//========
  //========//========//========//========
 
  .method public hidebysig static string
          FormatEnum<valuetype .ctor ([mscorlib]System.Enum) T>(object target, string format) cil managed
  {
    .custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 )
 
    .maxstack  8
    ldtoken    !!T
    call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
    ldarg.0
    ldarg.1
    call       string [mscorlib]System.Enum::Format(class [mscorlib]System.Type, object, string)
    ret
  }
 
  .method public hidebysig static string
          Format<valuetype .ctor ([mscorlib]System.Enum) T>(!!T target, string format) cil managed
  {
    .custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 )
 
    .maxstack  8
    ldtoken    !!T
    call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
    ldarg.0
    box        !!T
    ldarg.1
    call       string [mscorlib]System.Enum::Format(class [mscorlib]System.Type, object, string)
    ret
  }
 
  .method public hidebysig static string
          GetEnumName<valuetype .ctor ([mscorlib]System.Enum) T>(object target) cil managed
  {
    .custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 )
 
    .maxstack  8
    ldtoken    !!T
    call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
    ldarg.0
    call       string [mscorlib]System.Enum::GetName(class [mscorlib]System.Type, object)
    ret
  }
 
  .method public hidebysig static string[]
          GetNames<valuetype .ctor ([mscorlib]System.Enum) T>() cil managed
  {
    .maxstack  8
    ldtoken    !!T
    call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
    call       string[] [mscorlib]System.Enum::GetNames(class [mscorlib]System.Type)
    ret
  }
 
  .method public hidebysig static class [mscorlib]System.Type
          GetUnderlyingType<valuetype .ctor ([mscorlib]System.Enum) T>() cil managed
  {
    .maxstack  8
    ldtoken    !!T
    call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
    call       class [mscorlib]System.Type [mscorlib]System.Enum::GetUnderlyingType(class [mscorlib]System.Type)
    ret
  }
 
  .method public hidebysig static !!T[]  GetValues<valuetype .ctor ([mscorlib]System.Enum) T>() cil managed
  {
    .maxstack  8
    ldtoken    !!T
    call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
    call       class [mscorlib]System.Array [mscorlib]System.Enum::GetValues(class [mscorlib]System.Type)
    castclass  !!T[]
    ret
  }
 
  .method public hidebysig static bool  IsDefinedEnum<valuetype .ctor ([mscorlib]System.Enum) T>(object target) cil managed
  {
    .custom instance void [System.Core]System.Runtime.CompilerServices.ExtensionAttribute::.ctor() = ( 01 00 00 00 )
 
    .maxstack  8
    ldtoken    !!T
    call       class [mscorlib]System.Type [mscorlib]System.Type::GetTypeFromHandle(valuetype [mscorlib]System.RuntimeTypeHandle)
    ldarg.0
    call       bool [mscorlib]System.Enum::IsDefined(class [mscorlib]System.Type, object)
    ret
  }
 
}

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 程序集 GenericEnumExtensions.dll, v4.0.30319
 
using System;
using System.Runtime.CompilerServices;
 
namespace GenericEnumExtensions
{
  public static class GenericEnumExt
  {
    public static string Format<T>(this T target, string format) where T : struct, Enum;
    public static string FormatEnum<T>(this object target, string format) where T : struct, Enum;
    public static string GetEnumName<T>(this object target) where T : struct, Enum;
    public static string[] GetNames<T>() where T : struct, Enum;
    public static Type GetUnderlyingType<T>() where T : struct, Enum;
    public static T[] GetValues<T>() where T : struct, Enum;
    public static bool Has<T>(this T target, T flag) where T : struct, Enum;
    public static bool IsDefinedEnum<T>(this object target) where T : struct, Enum;
    public static T? ParseEnum<T>(this string target) where T : struct, Enum;
    public static T? ParseEnum<T>(this string target, bool ignoreCase) where T : struct, Enum;
  }
}

 

编辑推荐:
· C++代码改造为UTF-8编码问题的总结
· DeepSeek 解答了困扰我五年的技术问题
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
阅读排行:
· What?废柴, 还在本地部署DeepSeek吗?Are you kidding?
· 程序员转型AI:行业分析
· 深入集成:使用 DeepSeek SDK for .NET 实现自然语言处理功能
· 为DeepSeek添加本地知识库
· .NET程序员AI开发基座:Microsoft.Extensions.AI
点击右上角即可分享
微信分享提示