Skip to content

Instantly share code, notes, and snippets.

@bc3tech
Last active August 29, 2015 14:23
Show Gist options
  • Save bc3tech/15cd1aa0b32aaf10e43a to your computer and use it in GitHub Desktop.
Save bc3tech/15cd1aa0b32aaf10e43a to your computer and use it in GitHub Desktop.
Enum Defined/Parse/TryParse helpers
/// <summary>
/// Determines whether the instance is defined as a value for its enumeration
/// </summary>
/// <param name="input">The input.</param>
/// <returns></returns>
/// <example>
/// <code lang="c#">
/// enum MyEnum {
/// Value1 = 1,
/// }
/// MyEnum val = 453;
/// val.IsDefined(); // false
/// val = 1;
/// val.IsDefined(); // true
/// </code>
/// </example>
public static bool IsDefined(this Enum input)
{
return Enum.IsDefined(input.GetType(), input);
}
/// <summary>
/// Determines whether the specified value is defined as a value for the enumeration
/// type of <paramref name="input"/>
/// </summary>
/// <param name="input">The input.</param>
/// <param name="value">The value.</param>
/// <returns></returns>
/// <example>
/// <code lang="c#">
/// enum MyEnum {
/// Value1 = 1,
/// }
/// new MyEnum().IsDefined(453); // false
/// new MyEnum().IsDefined(1); // true
/// new MyEnum().IsDefined("Value1"); // true
/// new MyEnum().IsDefined("Value2"); // false
/// </code>
/// </example>
public static bool IsDefined(this Enum input, object value)
{
return Enum.IsDefined(input.GetType(), value);
}
/// <summary>
/// Parses <paramref name="value"/> in to <typeparamref name="T"/> only if it's defined
/// in the enumeration's values
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="input">The input.</param>
/// <param name="value">The value.</param>
/// <param name="ignoreCase">if set to <c>true</c> [ignore case].</param>
/// <returns></returns>
/// <exception cref="System.ArgumentException">value</exception>
/// <example>
/// <code lang="c#">
/// enum MyEnum {
/// Value1 = 1,
/// }
/// MyEnum result;
/// new MyEnum().ParseDefined("453"); // ArgumentException
/// new MyEnum().ParseDefined("1"); // MyEnum.Value1
/// new MyEnum().ParseDefined("Value1"); // MyEnum.Value1
/// new MyEnum().ParseDefined("Value2"); // ArgumentException
/// </code>
/// </example>
public static T ParseDefined<T>(this Enum input, string value, bool ignoreCase = false)
where T : struct
{
var retval = (T)Enum.Parse(input.GetType(), value, ignoreCase);
if (input.IsDefined(retval))
{
return retval;
}
throw new ArgumentException(string.Format("Requested value '{0}' not defined in {1}", value, input.GetType()),
"value");
}
/// <summary>
/// Tries to parse <paramref name="value"/> in to <typeparamref name="T"/> only if it is defined
/// in the enumeration's values
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="input">The input.</param>
/// <param name="value">The value.</param>
/// <param name="result">The result.</param>
/// <returns></returns>
/// <seealso cref="TryParseDefined(Enum, string, bool, out T)"/>
public static bool TryParseDefined<T>(this Enum input, string value, out T result) where T : struct
{
return TryParseDefined(input, value, false, out result);
}
/// <summary>
/// Tries to parse <paramref name="value"/> in to <typeparamref name="T"/> only if it is defined
/// in the enumeration's values
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="input">The input.</param>
/// <param name="value">The value.</param>
/// <param name="ignoreCase">if set to <c>true</c> [ignore case].</param>
/// <param name="result">The result.</param>
/// <returns></returns>
/// <example>
/// <code lang="c#">
/// enum MyEnum {
/// Value1 = 1,
/// }
/// MyEnum result;
/// new MyEnum().TryParseDefined("453"); // false
/// new MyEnum().TryParseDefined("1"); // true, MyEnum.Value1
/// new MyEnum().TryParseDefined("Value1"); // true, MyEnum.Value1
/// new MyEnum().TryParseDefined("Value2"); // false
/// </code>
/// </example>
public static bool TryParseDefined<T>(this Enum input, string value, bool ignoreCase, out T result) where T : struct
{
result = default(T);
return !string.IsNullOrWhiteSpace(value)
&& Enum.TryParse(value, ignoreCase, out result)
&& input.IsDefined(result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment