Ramblings of an Id10t C# Programmer

Random Posts about .net

EnumParse Extension Method

clock July 21, 2008 10:13 by author Shawn C

 

Not the first one and most likely not the last EnumParse Methods

/// <summary>
/// Attempts to parse a string in to an enum value
/// </summary>
public static T EnumParse<T>(this string stringEnum)
{
    return stringEnum.EnumParse<T>(false);
} 

 

/// <summary>
/// Attempts to parse a string in to an enum value 
/// </summary>
/// <typeparam name="T">Enum to convert to</typeparam>
/// <param name="stringEnum">string to Parse</param>
/// <param name="ignoreCase">if the string is exect enum value true should be used else false</param>
/// <returns>enum of type T</returns>
public static T EnumParse<T>(this string stringEnum, bool ignoreCase)
{
    if (string.IsNullOrEmpty(stringEnum) )
    {
        throw new ArgumentNullException("stringEnum");
    }
 
    stringEnum = stringEnum.Trim();
    if (stringEnum.Length == 0)
    {
        throw new ArgumentException("Must specify valid information for parsing in the string.", "stringEnum");
    }
 
    Type t = typeof (T);
 
    if (!t.IsEnum)
    {
        throw new ArgumentException("Type provided must be an Enum.", "T");
    }
 
    T enumType = (T) Enum.Parse(t, stringEnum, ignoreCase);
    return enumType;
}

Currently rated 1.5 by 12 people

  • Currently 1.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


EnumToList Extension Method

clock July 21, 2008 09:57 by author Shawn C

 

While not the best solution I believe this is the only solution at this time due to the fact that enums are not instances of objects. Allows you to take an Enum and convert it to a Typed List which also gives you LINQabilty on it.

Usage:

   1: var list = StringComparison.CurrentCulture.EnumToList();
   2: foreach (var enumValue in list)
   3: {
   4: ...
   5: }

And the method:

   1: /// <summary>
   2: ///     Converts a enum to a list
   3: /// </summary>
   4: /// <typeparam name="T">string name of type of Enum</typeparam>
   5: /// <returns>An enumerable list of Enum Type "T"</returns>
   6: /// <remarks>
   7: ///     Usage:
   8: ///     var list = StringComparison.CurrentCulture.EnumToList();
   9: /// </remarks>
  10: public static IEnumerable<T> EnumToList<T>(this T enumName) where T : struct
  11: {
  12:     Type enumType = enumName.GetType();
  13:  
  14:     // Can’t use generic type constraints on value types,
  15:     // so have to do check like this
  16:     if (enumType.BaseType != typeof (Enum))
  17:         throw new ArgumentException(enumType + " is not an Enum.", "T", null);
  18:  
  19:     var enumValArray = Enum.GetValues(enumType);
  20:     var enumValList = new List<T>(enumValArray.Length);
  21:  
  22:     foreach (int val in enumValArray)
  23:     {
  24:         enumValList.Add((T) Enum.Parse(enumType, val.ToString()));
  25:     }
  26:  
  27:     return enumValList;
  28: }

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


About the author

I like to believe that I am a halfway decent programmer that earns a living programming in .net. Graduated from R.I.T., and I have been programming for 10 years for various sized companies in the Rochester, NY area. To this day I still live in Rochester, NY but I have a great wife and 3 fat cats to make my days enjoyable.

View Shawn Carr's profile on LinkedIn

Tag cloud

Recent posts

Recent comments

None

Other Information

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in  anyway.

© Copyright 2008

Month List

Calendar

<<  May 2012  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

Most comments

Sign in