-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEnum{T}.cs
138 lines (114 loc) · 3.64 KB
/
Enum{T}.cs
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
namespace System
{
public static class Enum<T> where T : unmanaged, Enum
{
public static readonly Type Type;
public static readonly Type UnderlyingType;
public static readonly ReadArray1<T> Values;
public static readonly ReadArray1<string> Names;
static Enum()
{
Type = typeof(T);
UnderlyingType = Enum.GetUnderlyingType(Type);
Values = (T[])Enum.GetValues(Type);
Names = Enum.GetNames(Type);
}
public static T From(byte value)
{
try { return (T)(object)value; }
catch { return default; }
}
public static T From(sbyte value)
{
try { return (T)(object)value; }
catch { return default; }
}
public static T From(short value)
{
try { return (T)(object)value; }
catch { return default; }
}
public static T From(ushort value)
{
try { return (T)(object)value; }
catch { return default; }
}
public static T From(int value)
{
try { return (T)(object)value; }
catch { return default; }
}
public static T From(uint value)
{
try { return (T)(object)value; }
catch { return default; }
}
public static T From(long value)
{
try { return (T)(object)value; }
catch { return default; }
}
public static T From(ulong value)
{
try { return (T)(object)value; }
catch { return default; }
}
public static T From(object value)
{
try { return (T)Enum.ToObject(Type, value); }
catch { return default; }
}
public static byte ToByte(T value)
{
try { return (byte)(object)value; }
catch { return default; }
}
public static sbyte ToSByte(T value)
{
try { return (sbyte)(object)value; }
catch { return default; }
}
public static short ToShort(T value)
{
try { return (short)(object)value; }
catch { return default; }
}
public static ushort ToUShort(T value)
{
try { return (ushort)(object)value; }
catch { return default; }
}
public static int ToInt(T value)
{
try { return (int)(object)value; }
catch { return default; }
}
public static uint ToUInt(T value)
{
try { return (uint)(object)value; }
catch { return default; }
}
public static long ToLong(T value)
{
try { return (long)(object)value; }
catch { return default; }
}
public static ulong ToULong(T value)
{
try { return (ulong)(object)value; }
catch { return default; }
}
public static T Parse(string value)
=> (T)Enum.Parse(Type, value);
public static T Parse(string value, bool ignoreCase)
=> (T)Enum.Parse(Type, value, ignoreCase);
public static bool TryParse(string value, out T result)
=> Enum.TryParse(value, out result);
public static bool TryParse(string value, bool ignoreCase, out T result)
=> Enum.TryParse(value, ignoreCase, out result);
public static bool IsDefined(object value)
=> Enum.IsDefined(Type, value);
public static string Format(object value, string format)
=> Enum.Format(Type, value, format);
}
}