-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
EnumTools.cs
115 lines (97 loc) · 3.17 KB
/
EnumTools.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
namespace KeePassDiceware
{
internal static class EnumTools
{
public static string GetDescription(this Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
return fi.GetCustomAttributes(typeof(DescriptionAttribute), false) is DescriptionAttribute[] attributes && attributes.Any()
? attributes.First().Description
: value.ToString();
}
public static T FromDescription<T>(string description) where T : Enum
{
Type enumType = typeof(T);
Array allValues = Enum.GetValues(enumType);
foreach (T value in allValues)
{
string valueString = value.ToString();
FieldInfo fi = enumType.GetField(valueString);
bool match = fi.GetCustomAttributes(typeof(DescriptionAttribute), false) is DescriptionAttribute[] attributes && attributes.Any()
? attributes.First().Description == description
: valueString == description;
if (match)
{
return value;
}
}
throw new ArgumentException($"{nameof(description)} must be in the given enum type. ({typeof(T).Name})");
}
public static string GetCategory(this Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
return fi.GetCustomAttributes(typeof(CategoryAttribute), false) is CategoryAttribute[] attributes && attributes.Any()
? attributes.First().Category
: string.Empty;
}
public static EnumDisplay<T> GetDisplay<T>(this T value) where T : Enum
=> EnumDisplay.Create(value);
public static IEnumerable<T> GetBrowsableValues<T>() where T : Enum
{
Type enumType = typeof(T);
Array allValues = Enum.GetValues(enumType);
foreach (T value in allValues)
{
FieldInfo fi = enumType.GetField(value.ToString());
bool browsable = fi.GetCustomAttributes(typeof(BrowsableAttribute), false) is BrowsableAttribute[] attributes && attributes.Any()
? attributes.First().Browsable
: true;
if (browsable)
{
yield return value;
}
}
}
public static IList<string> GetCategories<T>() where T : Enum
{
Type enumType = typeof(T);
Array allValues = Enum.GetValues(enumType);
var result = new List<string>();
foreach (T value in allValues)
{
FieldInfo fi = enumType.GetField(value.ToString());
string category = fi.GetCustomAttributes(typeof(CategoryAttribute), false) is CategoryAttribute[] attributes && attributes.Any()
? attributes.First().Category
: null;
if (string.IsNullOrWhiteSpace(category) == false
&& result.Contains(category) == false)
{
result.Add(category);
}
}
return result;
}
public static EnumDisplay<T>[] GetDisplays<T>() where T : Enum
{
return GetBrowsableValues<T>()
.Select(v => v.GetDisplay())
.ToArray();
}
// via: https://stackoverflow.com/a/4171296
public static IEnumerable<Enum> GetFlags(this Enum flagsEnum)
{
foreach (Enum value in Enum.GetValues(flagsEnum.GetType()))
{
if (flagsEnum.HasFlag(value))
{
yield return value;
}
}
}
}
}