b.logrythmik

{ dependency injection }

Enum<T> - Better Enum Parsing using Strongly-Typed Generics

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:

TestEnum testEnum = (TestEnum)Enum.Parse(typeof(TestEnum), "Super");

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:

var testEnum = Enum<TestEnum>.Parse("Super");

Only ONE declaration of the Enum type. Nice! (Okay, so I cheated and used the 'var' keyword, but you get the point)

Also, there are some really handy helpers built-in, like:
 TryParse - four overloads, two returning a bool with an out parameter and two that just returns NULL if it fails to parse.
 ToEnumerable - returns a typed-list of all the Enum options

Example:

// Fails parsing, enum value = null

var enumTest = Enum<TestEnum>.TryParse("WrongValue");

 

// Get an IEnumerable of the Enums

var list = Enum<TestEnum>.ToEnumerable();

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.

// So this...

string name = Enum<TestEnum>.GetName(TestEnum.Super);

 

// Instead of this...

string name = Enum.GetName(typeof(TestEnum), TestEnum.Super);

Enum<T> Code:

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

}

Enjoy!

Source Code:

Enum.zip (858.00 bytes)