Exemplo n.º 1
0
 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);
 }
Exemplo n.º 2
0
        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
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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
        }