When dealing with Enums (parsing, getting values, etc.) there is entirely too much casting and Types being passed around. For instance, to simply parse a string to it’s Enum value, one would do this:
We had to define the enum type THREE (count them) THREE times, in a single row. Blech! With this helper, this statement becomes much cleaner:
Only ONE declaration of the Enum type. Nice! (Okay, so I cheated and used the 'var' keyword, but you get the point)
Plus more goodies -- feel free to take a look.
Now, I am certain that I am not the first to create a generic helper to make parsing Enums easier (article one, article two to name a few) but here is a complete Enum<T> helper class for all your Enum needs. Note, I even recreated all the standard static methods available in Enum, so that there will be no need to pass the “typeof(X)” parameter.
public static class Enum<T>
where T: struct
{
public static T Parse(string name)
{
return Parse(name, false);
}
public static T Parse(string name, bool ignoreCase)
{
return (T)Enum.Parse(typeof(T), name, ignoreCase);
}
public static T? TryParse(string name)
{
return TryParse(name, false);
}
public static T? TryParse(string name, bool ignoreCase)
{
T value;
if (!string.IsNullOrEmpty(name) && TryParse(name, out value, ignoreCase))
return value;
return null;
}
public static bool TryParse(string name, out T value)
{
return TryParse(name, out value, false);
}
public static bool TryParse(string name, out T value, bool ignoreCase)
{
try
{
value = Parse(name, ignoreCase);
return true;
}
catch (ArgumentException)
{
value = default(T);
return false;
}
}
public static IEnumerable<T> ToEnumerable()
{
foreach (var value in Enum.GetValues(typeof(T)))
yield return (T)value;
}
public static IEnumerable<object> GetValues()
{
foreach (var value in Enum.GetValues(typeof(T)))
yield return value;
}
public static IDictionary<object, string> GetDictionary()
{
Dictionary<object, string> dictionary = new Dictionary<object, string>();
foreach (var value in GetValues())
dictionary.Add(
Convert.ChangeType(value, Enum.GetUnderlyingType(typeof(T))),
GetName(value));
return dictionary;
}
#region Strongly-Typed Enum Extenders
public static string Format(object value, string format)
{
return Enum.Format(typeof(T), value, format);
}
public static string GetName(object value)
{
return Enum.GetName(typeof(T), value);
}
public static IEnumerable<string> GetNames()
{
return Enum.GetNames(typeof(T));
}
public static Type GetUnderlyingType()
{
return Enum.GetUnderlyingType(typeof(T));
}
public static bool IsDefined(object value)
{
return Enum.IsDefined(typeof(T), value);
}
public static T ToObject(object value)
{
return (T)Enum.ToObject(typeof(T), value);
}
public static T ToObject(byte value)
{
return (T)Enum.ToObject(typeof(T), value);
}
public static T ToObject(sbyte value)
{
return (T)Enum.ToObject(typeof(T), value);
}
public static T ToObject(int value)
{
return (T)Enum.ToObject(typeof(T), value);
}
public static T ToObject(uint value)
{
return (T)Enum.ToObject(typeof(T), value);
}
public static T ToObject(long value)
{
return (T)Enum.ToObject(typeof(T), value);
}
public static T ToObject(ulong value)
{
return (T)Enum.ToObject(typeof(T), value);
}
public static T ToObject(short value)
{
return (T)Enum.ToObject(typeof(T), value);
}
public static T ToObject(ushort value)
{
return (T)Enum.ToObject(typeof(T), value);
}
#endregion
}