using System.Collections.Generic;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO.Compression;
public static class Constants
public const string DataContractNamespace = "";
[DataContract(Name = "Dispatcher", Namespace = Constants.DataContractNamespace)]
public partial class Dispatcher
private Dictionary<int, Dictionary<int, Dictionary<int, Message>>> m_DataBase;
m_DataBase = new Dictionary<int, Dictionary<int, Dictionary<int, Message>>>();
[System.Runtime.Serialization.OnDeserialized]
void OnDeserializedMethod(System.Runtime.Serialization.StreamingContext context)
m_DataBase = new Dictionary<int, Dictionary<int, Dictionary<int, Message>>>();
internal const string FileName = @"DispatcherDataBase.xml";
public static Dispatcher LoadFromFile()
Dispatcher loadedDataBase;
using (var stream = new FileStream(FileName, FileMode.Open))
var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(Dispatcher));
loadedDataBase = serializer.ReadObject(stream) as Dispatcher;
loadedDataBase = new Dispatcher();
using (var stream = new FileStream(FileName, FileMode.Create))
var serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(this.GetType());
serializer.WriteObject(stream, this);
[DataContract(Name = "Message", Namespace = Constants.DataContractNamespace)]
public string Value { get; set; }
public partial class Dispatcher
public void Set(int index1, int index2, int index3, Message message)
if (!m_DataBase.TryGetValue(index1, out var mid))
m_DataBase[index1] = mid = new Dictionary<int, Dictionary<int, Message>>();
if (!mid.TryGetValue(index2, out var inner))
mid[index2] = inner = new Dictionary<int, Message>();
internal static void AssertEquals(Dispatcher dispatcher1, Dispatcher dispatcher2)
if (dispatcher1.m_DataBase.Count != dispatcher2.m_DataBase.Count)
if (!dispatcher1.m_DataBase.Select(d => new { d.Key, d.Value.Count }).SequenceEqual(dispatcher2.m_DataBase.Select(d => new { d.Key, d.Value.Count })))
if (!dispatcher1.m_DataBase.SelectMany(d => d.Value).Select(d => new { d.Key, d.Value.Count })
.SequenceEqual(dispatcher2.m_DataBase.SelectMany(d => d.Value).Select(d => new { d.Key, d.Value.Count })))
if (!dispatcher1.m_DataBase.SelectMany(d => d.Value).SelectMany(d => d.Value).Select(d => new { d.Key, d.Value.Value })
.SequenceEqual(dispatcher2.m_DataBase.SelectMany(d => d.Value).SelectMany(d => d.Value).Select(d => new { d.Key, d.Value.Value })))
public static void Test()
var dispatcher = Dispatcher.LoadFromFile();
dispatcher.Set(1, 2, 3, new Message { Value = "hello" });
var xml1 = File.ReadAllText(Dispatcher.FileName);
var dispatcher2 = Dispatcher.LoadFromFile();
dispatcher2.SaveToFile();
var xml2 = File.ReadAllText(Dispatcher.FileName);
Assert.IsTrue(xml1 == xml2);
Dispatcher.AssertEquals(dispatcher, dispatcher2);
File.Delete(Dispatcher.FileName);
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.CodeBase.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
int netCoreAppIndex = Array.IndexOf(assemblyPath, "Microsoft.NETCore.App");
if (netCoreAppIndex > 0 && netCoreAppIndex < assemblyPath.Length - 2)
return assemblyPath[netCoreAppIndex + 1];