Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
EnumerationExtensions.cs
Go to the documentation of this file.
1using System;
2
3namespace Extensions;
4
5public static class EnumerationExtensions
6{
7 private class _Value
8 {
9 private static Type _UInt64 = typeof(ulong);
10
11 private static Type _UInt32 = typeof(long);
12
13 public long? Signed;
14
15 public ulong? Unsigned;
16
17 public _Value(object value, Type type)
18 {
19 if (!type.IsEnum)
20 {
21 throw new ArgumentException("Value provided is not an enumerated type!");
22 }
23 Type underlyingType = Enum.GetUnderlyingType(type);
24 if (underlyingType.Equals(_UInt32) || underlyingType.Equals(_UInt64))
25 {
27 }
28 else
29 {
31 }
32 }
33 }
34
35 public static T Include<T>(this Enum value, T append)
36 {
38 object obj = value;
39 _Value value2 = new _Value(append, type);
40 if (value2.Signed is long)
41 {
42 obj = Convert.ToInt64(value) | value2.Signed.Value;
43 }
44 else if (value2.Unsigned is ulong)
45 {
46 obj = Convert.ToUInt64(value) | value2.Unsigned.Value;
47 }
48 return (T)Enum.Parse(type, obj.ToString());
49 }
50
51 public static T Remove<T>(this Enum value, T remove)
52 {
54 object obj = value;
55 _Value value2 = new _Value(remove, type);
56 if (value2.Signed is long)
57 {
58 obj = Convert.ToInt64(value) & ~value2.Signed.Value;
59 }
60 else if (value2.Unsigned is ulong)
61 {
62 obj = Convert.ToUInt64(value) & ~value2.Unsigned.Value;
63 }
64 return (T)Enum.Parse(type, obj.ToString());
65 }
66
67 public static bool Has<T>(this Enum value, T check)
68 {
70 _Value value2 = new _Value(check, type);
71 if (value2.Signed is long)
72 {
73 return (Convert.ToInt64(value) & value2.Signed.Value) == value2.Signed.Value;
74 }
75 if (value2.Unsigned is ulong)
76 {
77 return (Convert.ToUInt64(value) & value2.Unsigned.Value) == value2.Unsigned.Value;
78 }
79 return false;
80 }
81
82 public static bool Missing<T>(this Enum obj, T value)
83 {
84 return !obj.Has(value);
85 }
86}
static T Remove< T >(this Enum value, T remove)
static bool Has< T >(this Enum value, T check)
static bool Missing< T >(this Enum obj, T value)
static T Include< T >(this Enum value, T append)
static long ToInt64(object? value)
Definition Convert.cs:1623
static ulong ToUInt64(object? value)
Definition Convert.cs:1738
static Type GetUnderlyingType(Type enumType)
Definition Enum.cs:309
static object Parse(Type enumType, string value)
Definition Enum.cs:368
static ? Type GetType(string typeName, bool throwOnError, bool ignoreCase)
Definition Type.cs:408
override bool Equals(object? o)
Definition Type.cs:1113