using System.Collections.Generic;
public string CommonProperty { get; set; }
class DerivedClass1 : BaseClass
public int SpecificProperty1 { get; set; }
class DerivedClass2 : BaseClass
public double SpecificProperty2 { get; set; }
List<BaseClass> objectList = new List<BaseClass>
new DerivedClass1 { CommonProperty = "Object1", SpecificProperty1 = 42 },
new DerivedClass2 { CommonProperty = "Object2", SpecificProperty2 = 3.14 }
string json = SerializeToJsonWithTypeInfo(objectList);
Console.WriteLine($"JSON: {json}");
List<BaseClass> deserializedList = DeserializeFromJsonWithTypeInfo<List<BaseClass>>(json);
foreach (var obj in deserializedList)
Console.WriteLine($"Object Type: {obj.GetType().Name}");
Console.WriteLine($"CommonProperty: {obj.CommonProperty}");
if (obj is DerivedClass1 derived1)
Console.WriteLine($"SpecificProperty1: {derived1.SpecificProperty1}");
else if (obj is DerivedClass2 derived2)
Console.WriteLine($"SpecificProperty2: {derived2.SpecificProperty2}");
static string SerializeToJsonWithTypeInfo<T>(T data)
JsonSerializerSettings settings = new JsonSerializerSettings
TypeNameHandling = TypeNameHandling.All
return JsonConvert.SerializeObject(data, Formatting.Indented, settings);
static T DeserializeFromJsonWithTypeInfo<T>(string json)
JsonSerializerSettings settings = new JsonSerializerSettings
TypeNameHandling = TypeNameHandling.All
return JsonConvert.DeserializeObject<T>(json, settings);