static IStringParser GetEnumParser(Type enumType) { if (!enumType.IsEnum) { throw new Exception("Type is not an enum!"); } if (!parsers.TryGetValue(enumType, out var parser)) { parser = new EnumStringParser(enumType); parsers[enumType] = parser; } return(parser); }
private static void RunEnumStringParserDemo() { Dictionary <string, Seasons> map = new Dictionary <string, Seasons> { { "sppringg", Seasons.Spring }, { "Sptring", Seasons.Spring }, { "Summer", Seasons.Summer }, { "SSS", Seasons.Summer }, { "suMMer", Seasons.Summer } }; EnumStringParser <Seasons> parser = new EnumStringParser <Seasons>(map); Seasons result1 = parser.Parse("Sptring"); // Converts to Seasons.Spring Seasons result2 = parser.Parse("SSS"); // Converts to Seasons.Summer }
public void TestParseEnum1() { Dictionary<string, Seasons> map = new Dictionary<string, Seasons> { { "sppringg", Seasons.Spring }, { "Sptring", Seasons.Spring }, { "Summer", Seasons.Summer }, { "SSS", Seasons.Summer }, { "suMMer", Seasons.Summer } }; EnumStringParser<Seasons> parser = new EnumStringParser<Seasons>(map); Seasons expected = Seasons.Spring; Seasons actual = parser.ParseEnum("Sptring"); Assert.AreEqual(expected, actual); }
public void TestParseEnum1() { Dictionary <string, Seasons> map = new Dictionary <string, Seasons> { { "sppringg", Seasons.Spring }, { "Sptring", Seasons.Spring }, { "Summer", Seasons.Summer }, { "SSS", Seasons.Summer }, { "suMMer", Seasons.Summer } }; EnumStringParser <Seasons> parser = new EnumStringParser <Seasons>(map); Seasons expected = Seasons.Spring; Seasons actual = parser.Parse("Sptring"); Assert.AreEqual(expected, actual); }
private static void RunEnumStringParserDemo() { Dictionary<string, Seasons> map = new Dictionary<string, Seasons> { { "sppringg", Seasons.Spring }, { "Sptring", Seasons.Spring }, { "Summer", Seasons.Summer }, { "SSS", Seasons.Summer }, { "suMMer", Seasons.Summer } }; EnumStringParser<Seasons> parser = new EnumStringParser<Seasons>(map); Seasons result1 = parser.ParseEnum("Sptring"); // Converts to Seasons.Spring Seasons result2 = parser.ParseEnum("SSS"); // Converts to Seasons.Summer }