-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathEnumExtensions.cs
More file actions
114 lines (99 loc) · 4.39 KB
/
EnumExtensions.cs
File metadata and controls
114 lines (99 loc) · 4.39 KB
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
using System.Reflection;
namespace RayCarrot.RCP.Metro;
/// <summary>
/// Extension methods for <see cref="Enum"/>
/// </summary>
public static class EnumExtensions
{
/// <summary>
/// Gets all flags in the specified <see cref="Enum"/>
/// </summary>
/// <param name="input">The <see cref="Enum"/> to check</param>
/// <returns>The flags</returns>
/// <exception cref="ArgumentException">input is not an <see cref="Enum"/></exception>
/// <exception cref="ArgumentNullException">input is null</exception>
/// <exception cref="InvalidOperationException">The method is invoked by reflection in a reflection-only context, -or-
/// input s a type from an assembly loaded in a reflection-only context</exception>
public static IEnumerable<Enum> GetFlags(this Enum input)
{
if (input == null)
throw new ArgumentNullException(nameof(input));
foreach (Enum value in Enum.GetValues(input.GetType()))
if (input.HasFlag(value))
yield return value;
}
/// <summary>
/// Gets all flags in the specified <see cref="Enum"/>
/// </summary>
/// <typeparam name="T">The type of enum</typeparam>
/// <param name="input">The <see cref="Enum"/> to check</param>
/// <returns>The flags</returns>
/// <exception cref="ArgumentException">input is not an <see cref="Enum"/></exception>
/// <exception cref="ArgumentNullException">input is null</exception>
/// <exception cref="InvalidOperationException">The method is invoked by reflection in a reflection-only context, -or-
/// input s a type from an assembly loaded in a reflection-only context</exception>
public static IEnumerable<T> GetFlags<T>(this T input)
where T : Enum
{
if (input == null)
throw new ArgumentNullException(nameof(input));
foreach (Enum value in Enum.GetValues(input.GetType()))
if (input.HasFlag(value))
yield return (T)value;
}
/// <summary>
/// Sets the specified flag on the enum value
/// </summary>
/// <typeparam name="T">The enum type</typeparam>
/// <param name="value">The enum value</param>
/// <param name="flag">The flag to set</param>
/// <param name="set">True to set the flag, false to clear it</param>
/// <returns>The new enum value</returns>
public static T SetFlag<T>(this Enum value, T flag, bool set)
where T : Enum
{
Type underlyingType = Enum.GetUnderlyingType(value.GetType());
dynamic valueAsInt = Convert.ChangeType(value, underlyingType);
dynamic flagAsInt = Convert.ChangeType(flag, underlyingType);
if (set)
valueAsInt |= flagAsInt;
else
valueAsInt &= ~flagAsInt;
return (T)valueAsInt;
}
/// <summary>
/// Gets all values for an <see cref="Enum"/>
/// </summary>
/// <typeparam name="T">The type of enum</typeparam>
/// <param name="input">The enum to get values from</param>
/// <returns>An array with the values</returns>
/// <exception cref="ArgumentException">input is not an <see cref="Enum"/></exception>
/// <exception cref="ArgumentNullException">input is null</exception>
/// <exception cref="InvalidOperationException">The method is invoked by reflection in a reflection-only context, -or-
/// input s a type from an assembly loaded in a reflection-only context</exception>
public static T[] GetValues<T>(this T input)
where T : Enum
{
if (input == null)
throw new ArgumentNullException(nameof(input));
return EnumHelpers.GetValues<T>();
}
/// <summary>
/// Gets the first attribute of specified type for the enum value
/// </summary>
/// <typeparam name="T">The type of attribute to retrieve</typeparam>
/// <param name="value">The enum value to get the attribute for</param>
/// <returns>The attribute instance</returns>
public static T? GetAttribute<T>(this Enum value)
where T : Attribute
{
if (value == null)
throw new ArgumentNullException(nameof(value));
// Get the member info for the value
MemberInfo[] memberInfo = value.GetType().GetMember(value.ToString());
// Get the attribute
IEnumerable<T> attributes = memberInfo.First().GetCustomAttributes<T>(false);
// Return the first attribute
return attributes.FirstOrDefault();
}
}