using System.Collections.Generic;
public abstract class Enumeration<TEnum, TValue>
where TEnum : Enumeration<TEnum, TValue>
where TValue: class, IComparable
private static readonly Dictionary<string, Enumeration<TEnum, TValue>> Mapping = new Dictionary<string, Enumeration<TEnum, TValue>>();
public TValue Value { get; private set; }
public string Name { get; private set; }
Console.WriteLine("Static base Constructor called");
System.Runtime.CompilerServices.RuntimeHelpers.RunClassConstructor(typeof(TEnum).TypeHandle);
protected Enumeration(TValue enumValue, string name)
public static TEnum Parse(string name)
Enumeration<TEnum, TValue> result;
if (Mapping.TryGetValue(name, out result))
throw new InvalidCastException();
public static IEnumerable<TEnum> All { get { return Mapping.Values.AsEnumerable().Cast<TEnum>(); } }
public override string ToString() { return Name; }
public sealed class DerivedEnum : Enumeration<DerivedEnum, String>
Console.WriteLine("Static Derived constructor Called");
public static readonly DerivedEnum A = new DerivedEnum("blah", "blah");
private DerivedEnum(string val, string name) : base(val, name) {}
public static explicit operator DerivedEnum(string str) { return Parse(str); }
public static void Main()
Console.WriteLine("Count: " + DerivedEnum.All.Count());
Console.WriteLine("Parse Blah:" + DerivedEnum.Parse("blah"));