public
Created

Code from "Enumerations and Strings - Stop the Madness!" (modified and added over time)

  • Download Gist
gistfile1.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
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
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.