Skip to content

Instantly share code, notes, and snippets.

@DoggettCK
Created October 22, 2011 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save DoggettCK/1305566 to your computer and use it in GitHub Desktop.
Save DoggettCK/1305566 to your computer and use it in GitHub Desktop.
Code from "Enumerations and Strings - Stop the Madness!" (modified and added over time)
using System;
using System.Collections.Generic;
using System.Linq;
namespace Util
{
public static class Enums
{
#region Parse Methods
/// <summary>
/// Parses the specified value into the desired enum type (case-sensitive)
/// </summary>
/// <typeparam name="T">Type of enum to parse into</typeparam>
/// <param name="value">Value of enum</param>
/// <returns>Enum of type T corresponding to value</returns>
public static T Parse<T>(string value)
{
return Parse<T>(value, false);
}
/// <summary>
/// Parses the specified value into the desired enum type, optionally ignoring case
/// </summary>
/// <typeparam name="T">Type of enum to parse into</typeparam>
/// <param name="value">Value of enum</param>
/// <param name="ignoreCase"></param>
/// <returns>Enum of type T corresponding to value</returns>
public static T Parse<T>(string value, bool ignoreCase)
{
T eValue;
Parse(value, ignoreCase, out eValue);
return eValue;
}
/// <summary>
/// Parses the specified value into the desired enum type (case-sensitive)
/// </summary>
/// <typeparam name="T">Type of enum to parse into</typeparam>
/// <param name="value">Value of enum</param>
/// <param name="eValue">Enum object to contain the parsed value</param>
public static void Parse<T>(string value, out T eValue)
{
Parse(value, false, out eValue);
}
/// <summary>
/// Shortcut for parsing ints as values
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
public static T Parse<T>(int value)
{
T eValue;
Parse(value, out eValue);
return eValue;
}
/// <summary>
/// Shortcut for parsing ints as values
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <param name="eValue"></param>
public static void Parse<T>(int value, out T eValue)
{
Parse(value.ToString(), false, out eValue);
}
/// <summary>
/// Parses the specified value into the desired enum type (optional case-sensitivity)
/// </summary>
/// <typeparam name="T">Type of enum to parse into</typeparam>
/// <param name="value">Value of enum</param>
/// <param name="ignoreCase"></param>
/// <param name="eValue">Enum object to contain the parsed value</param>
public static void Parse<T>(string value, bool ignoreCase, out T eValue)
{
value = (value ?? string.Empty).Trim();
if (!value.HasValue())
{
eValue = default(T);
return;
}
Type type = typeof(T);
if (IsNullable(type))
{
type = UnderlyingTypeOf(type);
}
eValue = (T)Enum.Parse(type, value, ignoreCase);
}
public static T AsEnum<T>(this string value)
{
return Parse<T>(value);
}
public static T AsEnum<T>(this int value)
{
return Parse<T>(value);
}
public static List<T> ToList<T>()
{
return Enumerable.ToList((IEnumerable<T>)Enum.GetValues(typeof(T)));
}
public static List<T> ToListExcept<T>(params T[] exceptThese)
{
return ToList<T>().Except(exceptThese).ToList();
}
#endregion
public static string ToSentence<T>(params T[] enums)
{
return ToSentence(null as string, enums);
}
public static string ToSentence<T>(string lastCommaSuffix, params T[] enums)
{
if (!typeof(T).IsEnum) return string.Empty;
if (enums.Length == 0) return string.Empty;
string[] enumStrings = Array.ConvertAll(enums, t => t.ToString());
const string separator = ", ";
string sentence = string.Join(separator, enumStrings).Trim();
if (!lastCommaSuffix.HasValue() || enums.Length < 2)
{
return sentence;
}
switch (enums.Length)
{
case 2:
{
return sentence.Replace(separator, string.Format(" {0} ", lastCommaSuffix));
}
default:
{
return sentence.Insert(sentence.LastIndexOf(separator) + separator.Length, string.Format("{0} ", lastCommaSuffix));
}
}
}
#region Private Methods
/// <summary>
/// Gets the underlying type of a nullable type
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
private static Type UnderlyingTypeOf(Type type)
{
return type.GetGenericArguments()[0];
}
/// <summary>
/// Determines whether the specified type is nullable.
/// </summary>
/// <param name="type"></param>
/// <returns></returns>
private static bool IsNullable(Type type)
{
if (!type.IsGenericType)
{
return false;
}
Type genericTypeDefinition = type.GetGenericTypeDefinition();
return (genericTypeDefinition.Equals(typeof (Nullable<>)));
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment