Skip to main content
marking answer as outdated
Source Link
Lockszmith
  • 2.6k
  • 1
  • 35
  • 52

Update: Visiting this page, 8 years later, after not touching C# for a long while, looks like my answer is no longer the best solution. I really like the converter solution tied with attribute-functions.

If you are reading this, please make sure you also check out other answers.
(hint: they are above this one)


As most of you, I really liked the selected answer by Jakub Šturc, but I also really hate to copy-paste code, and try to do it as little as I can.

As most of you, I really liked the selected answer by Jakub Šturc, but I also really hate to copy-paste code, and try to do it as little as I can.

Update: Visiting this page, 8 years later, after not touching C# for a long while, looks like my answer is no longer the best solution. I really like the converter solution tied with attribute-functions.

If you are reading this, please make sure you also check out other answers.
(hint: they are above this one)


As most of you, I really liked the selected answer by Jakub Šturc, but I also really hate to copy-paste code, and try to do it as little as I can.

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

As most of you, I really liked the selected answer by Jakub Šturcanswer by Jakub Šturc, but I also really hate to copy-paste code, and try to do it as little as I can.

As most of you, I really liked the selected answer by Jakub Šturc, but I also really hate to copy-paste code, and try to do it as little as I can.

As most of you, I really liked the selected answer by Jakub Šturc, but I also really hate to copy-paste code, and try to do it as little as I can.

added using clause to make the code easier on the newbies.
Source Link
Lockszmith
  • 2.6k
  • 1
  • 35
  • 52
using System; using System.Collections.Generic; using System.Linq; // for the .AsEnumerable() method call // E is the derived type-safe-enum class // - this allows all static members to be truly unique to the specific // derived class public class EnumBase<E, T> where E: EnumBase<E, T> { #region Instance code public T Value { get; private set; } public string Name { get; private set; } protected EnumBase(T EnumValue, string Name) { Value = EnumValue; this.Name = Name; mapping.Add(Name, this); } public override string ToString() { return Name; } #endregion #region Static tools static private readonly Dictionary<string, EnumBase<E, T>> mapping; static EnumBase() { mapping = new Dictionary<string, EnumBase<E, T>>(); } protected static E Parse(string name) { EnumBase<E, T> result; if (mapping.TryGetValue(name, out result)) { return (E)result; } throw new InvalidCastException(); } // This is protected to force the child class to expose it's own static // method. // By recreating this static method at the derived class, static // initialization will be explicit, promising the mapping dictionary // will never be empty when this method is called. protected static IEnumerable<E> All { get { return mapping.Values.AsEnumerable().Cast<E>(); } } #endregion } 
// E is the derived type-safe-enum class // - this allows all static members to be truly unique to the specific // derived class public class EnumBase<E, T> where E: EnumBase<E, T> { #region Instance code public T Value { get; private set; } public string Name { get; private set; } protected EnumBase(T EnumValue, string Name) { Value = EnumValue; this.Name = Name; mapping.Add(Name, this); } public override string ToString() { return Name; } #endregion #region Static tools static private readonly Dictionary<string, EnumBase<E, T>> mapping; static EnumBase() { mapping = new Dictionary<string, EnumBase<E, T>>(); } protected static E Parse(string name) { EnumBase<E, T> result; if (mapping.TryGetValue(name, out result)) { return (E)result; } throw new InvalidCastException(); } // This is protected to force the child class to expose it's own static // method. // By recreating this static method at the derived class, static // initialization will be explicit, promising the mapping dictionary // will never be empty when this method is called. protected static IEnumerable<E> All { get { return mapping.Values.AsEnumerable().Cast<E>(); } } #endregion } 
using System; using System.Collections.Generic; using System.Linq; // for the .AsEnumerable() method call // E is the derived type-safe-enum class // - this allows all static members to be truly unique to the specific // derived class public class EnumBase<E, T> where E: EnumBase<E, T> { #region Instance code public T Value { get; private set; } public string Name { get; private set; } protected EnumBase(T EnumValue, string Name) { Value = EnumValue; this.Name = Name; mapping.Add(Name, this); } public override string ToString() { return Name; } #endregion #region Static tools static private readonly Dictionary<string, EnumBase<E, T>> mapping; static EnumBase() { mapping = new Dictionary<string, EnumBase<E, T>>(); } protected static E Parse(string name) { EnumBase<E, T> result; if (mapping.TryGetValue(name, out result)) { return (E)result; } throw new InvalidCastException(); } // This is protected to force the child class to expose it's own static // method. // By recreating this static method at the derived class, static // initialization will be explicit, promising the mapping dictionary // will never be empty when this method is called. protected static IEnumerable<E> All { get { return mapping.Values.AsEnumerable().Cast<E>(); } } #endregion } 
Source Link
Lockszmith
  • 2.6k
  • 1
  • 35
  • 52
Loading