using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.ObjectModel;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
public static void Test()
var settings = new JsonSerializerSettings
TypeNameHandling = TypeNameHandling.Objects
var root = new RootObject();
var json = JsonConvert.SerializeObject(root, settings);
var jObject = JObject.Parse(json);
var type = jObject["$type"];
var serializer = JsonSerializer.CreateDefault(settings);
ReflectionUtils.SplitFullyQualifiedTypeName(type.ToString(), out var typeName, out var assemblyName);
var returnedType = serializer.SerializationBinder.BindToType(assemblyName, typeName);
Assert.AreEqual(root.GetType(), returnedType);
public static class ReflectionUtils
public static void SplitFullyQualifiedTypeName(string fullyQualifiedTypeName, out string typeName, out string assemblyName)
int? assemblyDelimiterIndex = GetAssemblyDelimiterIndex(fullyQualifiedTypeName);
if (assemblyDelimiterIndex != null)
typeName = fullyQualifiedTypeName.Substring(0, assemblyDelimiterIndex.GetValueOrDefault()).Trim();
assemblyName = fullyQualifiedTypeName.Substring(assemblyDelimiterIndex.GetValueOrDefault() + 1, fullyQualifiedTypeName.Length - assemblyDelimiterIndex.GetValueOrDefault() - 1).Trim();
typeName = fullyQualifiedTypeName;
private static int? GetAssemblyDelimiterIndex(string fullyQualifiedTypeName)
for (int i = 0; i < fullyQualifiedTypeName.Length; i++)
char current = fullyQualifiedTypeName[i];
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("{0} version: {1}", typeof(JsonSerializer).Assembly.GetName().Name, typeof(JsonSerializer).Assembly.FullName);
Console.WriteLine("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];