Skip to content

Instantly share code, notes, and snippets.

@imerchant
Created September 26, 2017 14:02
Show Gist options
  • Save imerchant/040ea0cc3b6531d7c851ce15f112bf4c to your computer and use it in GitHub Desktop.
Save imerchant/040ea0cc3b6531d7c851ce15f112bf4c to your computer and use it in GitHub Desktop.
Strongly typed enumeration
public class ContentType : Enumeration<int, ContentType>
{
public static readonly ContentType Json = new ContentType(1, "Json", "application/json");
public static readonly ContentType Pdf = new ContentType(2, "PDF", "application/pdf");
public static readonly ContentType Text = new ContentType(3, "Text", "text/plain");
public string HeaderValue { get; private set; }
private ContentType(int key, string displayName, string headerValue) : base(key, displayName)
{
HeaderValue = headerValue;
}
}
public abstract class Enumeration<TKey, TEnumeration> : IComparable<TEnumeration>, IEquatable<TEnumeration>
where TEnumeration : Enumeration<TKey, TEnumeration>
where TKey : IComparable
{
private readonly string _displayName;
private readonly TKey _key;
private static Lazy<TEnumeration[]> _enumerations = new Lazy<TEnumeration[]>(GetEnumerations);
public TKey Key
{
get { return _key; }
}
public string DisplayName
{
get { return _displayName; }
}
protected Enumeration(TKey key, string displayName)
{
_key = key;
_displayName = displayName;
}
public static TEnumeration[] GetAll()
{
return _enumerations.Value;
}
private static TEnumeration[] GetEnumerations()
{
var enumType = typeof(TEnumeration);
return enumType
.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.DeclaredOnly)
.Where(info => enumType.IsAssignableFrom(info.FieldType))
.Select(info => info.GetValue(null))
.Cast<TEnumeration>()
.ToArray();
}
public static bool TryParseKey(TKey key, out TEnumeration result)
{
return TryParse(e => e.Key.Equals(key), out result);
}
public static bool TryParseName(string displayName, bool ignoreCase, out TEnumeration result)
{
return ignoreCase
? TryParse(e => e.DisplayName.Equals(displayName, StringComparison.OrdinalIgnoreCase), out result)
: TryParse(e => e.DisplayName == displayName, out result);
}
private static bool TryParse(Func<TEnumeration, bool> predicate, out TEnumeration result)
{
result = GetAll().FirstOrDefault(predicate);
return result != null;
}
public sealed override string ToString()
{
return DisplayName;
}
public int CompareTo(TEnumeration other)
{
return Key.CompareTo(other.Key);
}
public override bool Equals(object obj)
{
return Equals(obj as TEnumeration);
}
public bool Equals(TEnumeration other)
{
return other != null && Key.Equals(other.Key);
}
public override int GetHashCode()
{
return Key.GetHashCode();
}
public static bool operator ==(Enumeration<TKey, TEnumeration> left, Enumeration<TKey, TEnumeration> right)
{
return Equals(left, right);
}
public static bool operator !=(Enumeration<TKey, TEnumeration> left, Enumeration<TKey, TEnumeration> right)
{
return !Equals(left, right);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment