using System.Collections.Generic;
using Newtonsoft.Json.Linq;
public static void Main()
var a = new BaseClass(){ Type = 0 };
var b = new DerivedClass(){ Type = 1, Name = "Hello" };
var list = new List<BaseClass>(){a,b};
var json = JsonConvert.SerializeObject(list);
var intermediate = JsonConvert.DeserializeObject<List<dynamic>>(json);
var result = Construct( intermediate );
Console.WriteLine($"[{string.Join(", ",result.Select(x => x?.ToString() ?? "NULL"))}]");
public static List<object> Construct( List<dynamic> items )
var result = new List<object>();
Console.WriteLine( $"Processing {items.Count} dynamic items" );
foreach( dynamic item in items )
result.Add(Construct( item ));
private static Dictionary<int, Func<dynamic, object>> factoryMap = new () {
{0 , Construct<BaseClass>},
{1 , Construct<DerivedClass>},
public static object Construct( dynamic item )
Console.WriteLine($"Item Type = {item.Type}");
result = factoryMap[(int)item.Type](item);
public static TResult Construct<TResult>( dynamic item ) where TResult: class, new()
Console.WriteLine($"Constructing a {typeof(TResult).ToString()}");
foreach( var property in result.GetType().GetProperties() )
JObject jo = item as JObject;
var propVal = jo.Property(property.Name).ToObject(property.PropertyType);
Console.WriteLine($"Setting property {property.Name} to value {propVal}");
property.SetValue( result, propVal );
public int Type {get; set;}
public class DerivedClass: BaseClass
public string Name {get; set;}