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.Runtime.Serialization;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
class ListCollection : List<int>
class PrivateListCollection: ISerializable, IEnumerable<int>
public PrivateListCollection() { }
public PrivateListCollection(IEnumerable<int> items) => list.AddRange(items);
private List<int> list = new List<int>();
public void Add(int value)
public void GetObjectData(SerializationInfo info, StreamingContext context)
info.AddValue("items", list);
public IEnumerator<int> GetEnumerator() => list.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public static void Test()
var list1 = new ListCollection { 1, 2, 3 };
var list2 = new PrivateListCollection();
foreach (var item in list1)
var settings = new JsonSerializerSettings { };
var json1 = JsonConvert.SerializeObject(list1, settings);
var json2 = JsonConvert.SerializeObject(list2, settings);
Console.WriteLine(json2);
Assert.AreEqual(json1, json2);
var list22 = JsonConvert.DeserializeObject<PrivateListCollection>(json1, settings);
var json22 = JsonConvert.SerializeObject(list22, settings);
Assert.AreEqual(json2, json22);
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.Location.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];