예제 #1
0
        static void Main(string[] args)
        {
            AFoo a = new AFoo();

            a.FooBarBuzz = "A";
            a.A          = "Hello World";

            BFoo b = new BFoo();

            b.FooBarBuzz = "B";
            b.B          = "Hello World";

            List <BaseFoo> allFoos = new List <BaseFoo>();

            allFoos.Add(a);
            allFoos.Add(b);

            var result = JsonConvert.SerializeObject(allFoos);

            // issue here:
            //Additional information: Could not create an instance of type ConsoleApplication6.BaseFoo. Type is an interface or abstract class and cannot be instantiated. Path '[0].A', line 1, position 6.
            //var test = JsonConvert.DeserializeObject<List<BaseFoo>>(result);
            JsonConverter[] converters = { new FooConverter() };

            var test = JsonConvert.DeserializeObject <List <BaseFoo> >(result, new JsonSerializerSettings()
            {
                Converters = converters
            });
        }
예제 #2
0
파일: Program.cs 프로젝트: ledgarl/Samples
        static void Main(string[] args)
        {
            AFoo a = new AFoo();
            a.FooBarBuzz = "A";
            a.A = "Hello World";

            BFoo b = new BFoo();
            b.FooBarBuzz = "B";
            b.B = "Hello World";

            List<BaseFoo> allFoos = new List<BaseFoo>();
            allFoos.Add(a);
            allFoos.Add(b);

            var result = JsonConvert.SerializeObject(allFoos);

            // issue here:
            //Additional information: Could not create an instance of type ConsoleApplication6.BaseFoo. Type is an interface or abstract class and cannot be instantiated. Path '[0].A', line 1, position 6.
            //var test = JsonConvert.DeserializeObject<List<BaseFoo>>(result);
            JsonConverter[] converters = { new FooConverter()};

            var test = JsonConvert.DeserializeObject<List<BaseFoo>>(result, new JsonSerializerSettings() { Converters = converters });

        }