/
SubclassableEnum.cs
63 lines (50 loc) · 2.1 KB
/
SubclassableEnum.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
using Generic = System.Collections.Generic;
namespace AtomicNet
{
public class SubclassableEnum
<
tSubclassableEnum,
tNaturalType
>
where tSubclassableEnum : SubclassableEnum<tSubclassableEnum, tNaturalType>
{
private
static
readonly Dictionary
<
tNaturalType,
tSubclassableEnum
> allValues = new Dictionary<tNaturalType,tSubclassableEnum>();
private
readonly tNaturalType naturalValue;
static SubclassableEnum() { System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(tSubclassableEnum).TypeHandle); }
protected SubclassableEnum(tNaturalType naturalValue)
{
Throw<System.ArgumentException>.If(allValues.ContainsKey(naturalValue), "A duplicate natural value was specified for the " + TypeSupport<tSubclassableEnum>.Name + " enumerated context.");
allValues.Add(naturalValue, (tSubclassableEnum) this);
this.naturalValue = naturalValue;
}
public static implicit operator tNaturalType(SubclassableEnum<tSubclassableEnum, tNaturalType> value)
{
return value != null ? value.naturalValue : default(tNaturalType);
}
public static tSubclassableEnum TrySelect(tNaturalType naturalValue, tSubclassableEnum defaultValue)
{
return allValues.TryReturnValueAs(naturalValue, defaultValue);
}
public static Generic.List<tSubclassableEnum> AllValues
{
get
{
return allValues.Values.ConvertToList(a=>a);
}
}
public static Generic.List<tNaturalType> AllNaturalValues
{
get
{
return allValues.Values.ConvertToList(a=>a.naturalValue);
}
}
}
}