Monday, July 13, 2009

A Tidier Way To Parse Enum Values

Here are some methods I wrote for parsing strings into their enum equivalents. I work in .NET 2.0 mostly so I just have these as static methods on a static object, but they could easily be turned into extension methods for the enum class, which would probably make them easier to access and reduce typing.

While converting a string to it’s enum equivalent isn’t hard, this hides all the nasty casting etc.

        #region ParseEnumValue Overloads

/// <summary>
///
Parses the string provided back into it's equivalent enum value.
/// </summary>
/// <remarks>
///
This overload is case-sensitive.
/// </remarks>
/// <typeparam name="T">
The type of enum to convert the string to.</typeparam>
/// <param name="enumValue">
A string containing the name of a value from the specified enum.</param>
/// <returns>
A the value from the enum specified that matches the specified string.</returns>
/// <exception cref="System.ArgumentException">
Occurs if the string does not match a value from the enum.</exception>
/// <exception cref="System.ArgumentNullException">
Occurs if the string provided is null.</exception>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
public static T ParseEnumValue<T>(string enumValue)
{
return ParseEnumValue<T>(enumValue, false);
}

/// <summary>
///
Parses the string provided back into it's equivalent enum value.
/// </summary>
/// <typeparam name="T">
The type of enum to convert the string to.</typeparam>
/// <param name="enumValue">
A string containing the name of a value from the specified enum.</param>
/// <param name="ignoreCase">
A boolean indicating whether or not the conversion should succeed if the string matches an enum but with a different case.</param>
/// <returns>
A the value from the enum specified that matches the specified string.</returns>
/// <exception cref="System.ArgumentException">
Occurs if the string does not match a value from the enum.</exception>
/// <exception cref="System.ArgumentNullException">
Occurs if the string provided is null.</exception>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
public static T ParseEnumValue<T>(string enumValue, bool ignoreCase)
{
if (enumValue == null) throw new ArgumentNullException("enumValue");

return (T)System.Enum.Parse(typeof(T), enumValue, ignoreCase);
}

#endregion

#region
TryParseEnumValue Overloads

/// <summary>
///
Attempts to parse the specified enum.
/// </summary>
/// <typeparam name="T">
The type of enum to convert the string to.</typeparam>
/// <param name="s">
A string containing the name of a value from the specified enum.</param>
/// <param name="value">
Returns either the converted value, or the default value for the enum.</param>
/// <returns>
A boolean indicating whether or not the conversion was successful.</returns>
/// <exception cref="System.ArgumentNullException">
Occurs if the string provided is null.</exception>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s")]
public static bool TryParseEnumValue<T>(string s, out T value)
{
return TryParseEnumValue<T>(s, out value, false);
}

/// <summary>
///
Attempts to parse the specified enum.
/// </summary>
/// <typeparam name="T">
The type of enum to convert the string to.</typeparam>
/// <param name="s">
A string containing the name of a value from the specified enum.</param>
/// <param name="value">
Returns either the converted value, or the default value for the enum.</param>
/// <param name="ignoreCase">
A boolean indicating whether or not the conversion should succeed if the string matches an enum but with a different case.</param>
/// <returns>
A boolean indicating whether or not the conversion was successful.</returns>
/// <exception cref="System.ArgumentNullException">
Occurs if the string provided is null.</exception>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "s"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1021:AvoidOutParameters", MessageId = "1#")]
public static bool TryParseEnumValue<T>(string s, out T value, bool ignoreCase)
{
bool retVal = false;
#if !CEBUILD
if (s == null) throw new ArgumentNullException("s");

value = default(T);
if (IsEnumValue(typeof(T), s, ignoreCase))
{
retVal = true;
value = (T)System.Enum.Parse(typeof(T), s, ignoreCase);
}
#else
try
{
value = (T)System.Enum.Parse(typeof(T), s, ignoreCase);
retVal = true;
}
catch (FormatException)
{
value = default(T);
retVal = false;
}
#endif
return
retVal;
}

#endregion




Technorati Tags: ,,,,,

No comments:

Post a Comment