Advertisement
GSz

A different type of enum by Lockszmith

GSz
Dec 14th, 2011
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.22 KB | None | 0 0
  1. //
  2. // Copyright (C) 2011 Gal Szkolnik aka Lockszmith (http://code.lockszmith.com)
  3. // Originally posted at http://code.lockszmith.com/a-different-type-of-enums
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  6. //   - The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  7. //
  8. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  9. //
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq; // for the .AsEnumerable() method call
  13.  
  14. namespace IdleSharp.Sys
  15. {
  16.     // Code has been made public at http://lksz.me/typeSafeEnums
  17.  
  18.     // E is the derived type-safe-enum class
  19.     // - this allows all static members to be truly unique to the specific
  20.     //   derived class
  21.     public interface IEnumBase<E, T> where E: IEnumBase<E, T>
  22.     {
  23.         T Value { get; }
  24.         string Name { get; }
  25.     }
  26.  
  27.     public abstract class EnumBase<E, T> : IEnumBase<EnumBase<E, T>, T> where E: EnumBase<E, T>
  28.     {
  29.         #region Instance code
  30.         public T Value { get; private set; }
  31.         public string Name { get; private set; }
  32.  
  33.         protected EnumBase(string Name, T EnumValue)
  34.         {
  35.             Value = EnumValue;
  36.             this.Name = Name;
  37.             mapping.Add(Name, this);
  38.         }
  39.  
  40.         public override string ToString() { return Name; }
  41.         #endregion
  42.  
  43.         #region Static tools
  44.         static private readonly Dictionary<string, EnumBase<E, T>> mapping;
  45.         static EnumBase() { mapping = new Dictionary<string, EnumBase<E, T>>(); }
  46.         protected static E Parse(string name)
  47.         {
  48.             EnumBase<E, T> result;
  49.             if (mapping.TryGetValue(name, out result))
  50.             {
  51.                 return (E)result;
  52.             }
  53.  
  54.             throw new InvalidCastException();
  55.         }
  56.         // This is protected to force the child class to expose it's own static
  57.         // method.
  58.         // By recreating this static method at the derived class, static
  59.         // initialization will be explicit, promising the mapping dictionary
  60.         // will never be empty when this method is called.
  61.         protected static IEnumerable<E> All { get { return mapping.Values.AsEnumerable().Cast<E>(); } }
  62.         #endregion
  63.     }
  64.  
  65.     public class EnumBase<E> : IEnumBase<E, string> where E : EnumBase<E>
  66.     {
  67.         #region Instance code
  68.         public string Value { get { return Name; } }
  69.         public string Name { get; private set; }
  70.  
  71.         protected EnumBase(string Name)
  72.         {
  73.             this.Name = Name;
  74.             mapping[Name] = this;
  75.         }
  76.  
  77.         public override string ToString() { return Name; }
  78.         #endregion
  79.  
  80.         #region Static tools
  81.         static private readonly Dictionary<string, EnumBase<E>> mapping;
  82.         static EnumBase() { mapping = new Dictionary<string, EnumBase<E>>(); }
  83.         protected static bool TryGet( string name, out E value )
  84.         {
  85.             EnumBase<E> result;
  86.             if (mapping.TryGetValue(name, out result))
  87.             {
  88.                 value = (E)result;
  89.                 return true;
  90.             }
  91.             value = null;
  92.             return false;
  93.         }
  94.         protected static E Parse(string name)
  95.         {
  96.             EnumBase<E> result;
  97.             if (mapping.TryGetValue(name, out result))
  98.             {
  99.                 return (E)result;
  100.             }
  101.  
  102.             throw new InvalidCastException();
  103.         }
  104.         // This is protected to force the child class to expose it's own static
  105.         // method.
  106.         // By recreating this static method at the derived class, static
  107.         // initialization will be explicit, promising the mapping dictionary
  108.         // will never be empty when this method is called.
  109.         protected static IEnumerable<E> All { get { return mapping.Values.AsEnumerable().Cast<E>(); } }
  110.         public static implicit operator string(EnumBase<E> Enum) { return Enum.Value; }
  111.         #endregion
  112.     }
  113.  
  114.  
  115.     public sealed class YesNoEnum : EnumBase<YesNoEnum, int>
  116.     {
  117.         public static readonly YesNoEnum Yes = new YesNoEnum(0, "Yes");
  118.         public static readonly YesNoEnum No = new YesNoEnum(1, "No");
  119.  
  120.         private YesNoEnum(int value, String name) : base(name, value) { }
  121.         public new static IEnumerable<YesNoEnum> All { get { return EnumBase<YesNoEnum, int>.All; } }
  122.         public static explicit operator YesNoEnum(string str) { return Parse(str); }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement