using System; using System.Collections.Generic; using System.Collections; using System.IO; using System.Reflection; namespace Persistence.Test { public class NewSerializationTest { public static void Test1() { Root r = new Root(); r.selfReferences = new List {r, r}; r.c = new Custom {r = r}; r.dict.Add("one", 1); r.dict.Add("two", 2); r.dict.Add("three", 3); Serializer s = new Serializer(r); Persistence.XmlFormatter xmlFormatter = new XmlFormatter(); StreamWriter writer = new StreamWriter("test.xml"); foreach (ISerializationToken token in s) { string line = xmlFormatter.Format(token); writer.Write(line); Console.Out.Write(line); } writer.Close(); XmlParser parser = new XmlParser(new StreamReader("test.xml")); DeSerializer deSerializer = new DeSerializer(); object o = deSerializer.DeSerialize(parser); Root t = CloningFactory.DefaultClone(r); Console.Out.WriteLine(Util.AutoFormat(o, true)); } public static void Test2() { Manager m = new Manager(); Serializer s = new Serializer(m); Persistence.XmlFormatter xmlFormatter = new XmlFormatter(); StreamWriter writer = new StreamWriter("test2.xml"); foreach (ISerializationToken token in s) { string line = xmlFormatter.Format(token); writer.Write(line); Console.Out.Write(line); } writer.Close(); XmlParser parser = new XmlParser(new StreamReader("test2.xml")); DeSerializer deSerializer = new DeSerializer(); object o = deSerializer.DeSerialize(parser); Manager n = CloningFactory.DefaultClone(m); Console.Out.WriteLine(Util.AutoFormat(o, true)); } public static void SpeedTest() { StorableObject storable = new StorableObject(); CloneableObject cloneable = new CloneableObject(); Console.Write("initializing..."); storable.Init(); cloneable.Init(); Console.WriteLine("done"); List storableClones = new List(); List clonableClones = new List(); CloningFactory cloningFactory = new CloningFactory(); for (int i = 0; i < 100000; i++) { Console.Write("cloning storable.. "); storableClones.Add(cloningFactory.Clone(storable)); Console.WriteLine(); Console.Write("cloning cloneable.. "); clonableClones.Add((CloneableObject)cloneable.Clone()); Console.WriteLine(); } } public static void SpeedTest2() { Root r = new Root(); r.selfReferences = new List {r, r}; r.c = new Custom {r = r}; r.dict.Add("one", 1); r.dict.Add("two", 2); r.dict.Add("three", 3); CloneableRoot cr = new CloneableRoot(); cr.selfReferences = new List {cr, cr}; cr.c = new CloneableCustom {r = cr}; cr.dict.Add("one", 1); cr.dict.Add("two", 2); cr.dict.Add("three", 3); List storableClones = new List(); List clonableClones = new List(); CloningFactory cloningFactory = new CloningFactory(); DateTime start = DateTime.Now; Console.Write("cloning storable.. "); for (int i = 0; i < 100000; i++) { storableClones.Add(cloningFactory.Clone(r)); } Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks)); start = DateTime.Now; Console.Write("cloning storable again.. "); for (int i = 0; i < 100000; i++) { storableClones.Add(cloningFactory.Clone(r)); } Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks)); start = DateTime.Now; Console.Write("cloning cloneable.. "); for (int i = 0; i < 100000; i++) { clonableClones.Add((CloneableRoot)cr.Clone(new Dictionary())); } Console.WriteLine(new TimeSpan(DateTime.Now.Ticks - start.Ticks)); clonableClones.Clear(); } public class Base { [Storable] private int baseInt = 3; } public class Derived: Base { [Storable] private int derivedInt = 5; } public static void Main() { Test1(); //Test2(); //SpeedTest(); //SpeedTest2(); Console.In.ReadLine(); } } }