-
Notifications
You must be signed in to change notification settings - Fork 34
/
Enumeration.cs
136 lines (113 loc) · 3.93 KB
/
Enumeration.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
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
namespace Headspring
{
[Serializable]
[DebuggerDisplay("{DisplayName} - {Value}")]
public abstract class Enumeration<TEnumeration> : EnumerationGeneric<TEnumeration, int>
where TEnumeration : Enumeration<TEnumeration>
{
protected Enumeration(int value, string displayName)
: base(value, displayName)
{
}
public static TEnumeration FromInt32(int value)
{
return FromValue(value);
}
public static bool TryFromInt32(int listItemValue, out TEnumeration result)
{
return TryParse(listItemValue, out result);
}
}
[Serializable]
[DebuggerDisplay("{DisplayName} - {Value}")]
public abstract class Enumeration<TEnumeration, TValue> : IComparable<TEnumeration>, IEquatable<TEnumeration>
where TEnumeration : Enumeration<TEnumeration, TValue>
where TValue : IComparable
{
readonly string _displayName;
readonly TValue _value;
private static Lazy<TEnumeration[]> _enumerations = new Lazy<TEnumeration[]>(GetEnumerations);
protected Enumeration(TValue value, string displayName)
{
_value = value;
_displayName = displayName;
}
public TValue Value
{
get { return _value; }
}
public string DisplayName
{
get { return _displayName; }
}
public int CompareTo(TEnumeration other)
{
return Value.CompareTo(other.Value);
}
public override sealed string ToString()
{
return DisplayName;
}
public static TEnumeration[] GetAll()
{
return _enumerations.Value;
}
private static TEnumeration[] GetEnumerations()
{
Type enumerationType = typeof(TEnumeration);
return enumerationType
.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly)
.Where(info => enumerationType.IsAssignableFrom(info.FieldType))
.Select(info => info.GetValue(null))
.Cast<TEnumeration>()
.ToArray();
}
public override bool Equals(object obj)
{
return Equals(obj as TEnumeration);
}
public bool Equals(TEnumeration other)
{
return other != null && Value.Equals(other.Value);
}
public override int GetHashCode()
{
return Value.GetHashCode();
}
public static TEnumeration FromValue(TValue value)
{
return Parse(value, "value", item => item.Value.Equals(value));
}
public static TEnumeration Parse(string displayName)
{
return Parse(displayName, "display name", item => item.DisplayName == displayName);
}
static bool TryParse(Func<TEnumeration, bool> predicate, out TEnumeration result)
{
result = GetAll().FirstOrDefault(predicate);
return result != null;
}
private static TEnumeration Parse(object value, string description, Func<TEnumeration, bool> predicate)
{
TEnumeration result;
if (!TryParse(predicate, out result))
{
string message = string.Format("'{0}' is not a valid {1} in {2}", value, description, typeof(TEnumeration));
throw new ArgumentException(message, "value");
}
return result;
}
public static bool TryParse(TValue value, out TEnumeration result)
{
return TryParse(e => e.Value.Equals(value), out result);
}
public static bool TryParse(string displayName, out TEnumeration result)
{
return TryParse(e => e.DisplayName == displayName, out result);
}
}
}