1
Vote

Parsing Enumeration

description

enumerations are case sensitiv, to fix the bug replace
return Maybe.Just(
    MatchBoolString(value)
        ? ConvertBoolString(value)
        : conversionType.IsEnum
            ? Enum.Parse(conversionType, value)
            : Convert.ChangeType(value, conversionType, conversionCulture));
with
return Maybe.Just(
    MatchBoolString(value)
        ? ConvertBoolString(value)
        : conversionType.IsEnum
            ? Enum.Parse(conversionType, value, true)
            : Convert.ChangeType(value, conversionType, conversionCulture));
in method CommandLine.Core.TypeConverter.(ChangeType)

btw great job, thumbs up

comments

n1ceweasel wrote Sep 1, 2013 at 8:40 AM

Enum.Parse throws an unhandled exception if the value not found

replace
return Maybe.Just(
    MatchBoolString(value)
        ? ConvertBoolString(value)
        : conversionType.IsEnum
            ? Enum.Parse(conversionType, value)
            : Convert.ChangeType(value, conversionType, conversionCulture));
with
return Maybe.Just(
    MatchBoolString(value)
        ? ConvertBoolString(value)
        : MatchEnum(conversionType, value)
            ? ConvertEnum(conversionType, value)
            : Convert.ChangeType(value, conversionType, conversionCulture));
new methods
private static bool MatchEnum(Type conversionType, string value)
{
    return Enum.GetNames(conversionType).FirstOrDefault(
        v => v.Equals(value, StringComparison.OrdinalIgnoreCase)) != null;
}

private static object ConvertEnum(Type conversionType, string value)
{
    return Enum.Parse(conversionType, value, true);
}