Deserialize with using Base class

Oct 12, 2009 at 10:28 PM

This code will throw an exception, is it possible to do this? I know the different derived types but there are a few of them so I want to use the base class.

 

    class Program
    {
        static void Main()
        {
            var dict = new Dictionary<string, Base>();
            dict.Add("Test", new Derived { Name = "Test" });

            var result = JsonConvert.SerializeObject(dict);
            var testGeneric = JsonConvert.DeserializeObject<Dictionary<string, Base>>(result);
        }
    }

    public abstract class Base { }

    public sealed class Derived : Base
    {
        public string Name { get; set; }
    }