using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Collections.ObjectModel;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using Newtonsoft.Json.Linq;
public class RecursiveList<T> : List<RecursiveList<T>>
public class RecursiveListStringSurrogate
public class RecursiveListStringSurrogateSelector : ISerializationSurrogateProvider
public object GetDeserializedObject(object obj, Type targetType)
if (obj is RecursiveListStringSurrogate)
return new RecursiveList<string>();
public object GetObjectToSerialize(object obj, Type targetType)
if (obj is RecursiveList<string>)
return new RecursiveListStringSurrogate();
public Type GetSurrogateType(Type type)
if (type == typeof(RecursiveList<string>))
return typeof(RecursiveListStringSurrogate);
public static partial class DataContractSerializerHelper
public static string ToContractXml<T>(this T obj, DataContractSerializer serializer = null, XmlWriterSettings settings = null)
serializer = serializer ?? new DataContractSerializer(obj == null ? typeof(T) : obj.GetType());
using (var textWriter = new StringWriter())
settings = settings ?? new XmlWriterSettings { Indent = true };
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
serializer.WriteObject(xmlWriter, obj);
return textWriter.ToString();
public static T FromContractXml<T>(this string xml, DataContractSerializer serializer = null)
using (var textReader = new StringReader(xml ?? ""))
using (var xmlReader = XmlReader.Create(textReader))
return (T)(serializer ?? new DataContractSerializer(typeof(T))).ReadObject(xmlReader);
public static void Test()
Test(new List<string> ());
Test(new List<List<string>> ());
Test(new List<List<List<string>>> ());
var list = new RecursiveList<string>();
static void Test<T>(T root)
var surrogate = new RecursiveListStringSurrogateSelector();
var serializer = new DataContractSerializer(root.GetType());
serializer.SetSerializationSurrogateProvider(surrogate);
MemoryStream stream1 = new MemoryStream();
var writer = XmlDictionaryWriter.CreateBinaryWriter(stream1);
serializer.WriteObject(writer, root);
Console.WriteLine(System.Text.Encoding.UTF8.GetString(stream1.GetBuffer(), 0, checked((int)stream1.Length)));
var xml = root.ToContractXml(serializer);
public JObject obj { get; set; }
[DataMember(Name = "obj", Order = 0, IsRequired = false)]
public string objString { get { return obj?.ToString(); } set { obj = (value == null ? null : JObject.Parse(value)); } }
public static void Main()
Console.WriteLine("Environment version: {0} ({1})", System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription , GetNetCoreVersion());
Console.WriteLine("Failed with unhandled exception: ");
public static string GetNetCoreVersion()
var assembly = typeof(System.Runtime.GCSettings).GetTypeInfo().Assembly;
var assemblyPath = assembly.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];