public static Packet ToBinary(object value) { int length = SerializedSizeCalculator.Calculate <object>(value); SerializeWriter serializeWriter = new SerializeWriter(length, 0); SerializeWriterHelper <object, SerializeWriter> .Serialize(ref serializeWriter, value); return(serializeWriter.data); }
public static Packet ToBinary <T>(T value) { int categoryId = ClassInfo <T> .CategoryId; if (categoryId == 0) { return(SerializeWriter.ToBinary(value)); } int length = SerializedSizeCalculator.Calculate <T>(value); SerializeWriter serializeWriter = new SerializeWriter(length, categoryId); SerializeWriterHelper <T, SerializeWriter> .Serialize(ref serializeWriter, value); return(serializeWriter.data); }
public static Packet Serialize <T>(T value) { return(SerializeWriter.ToBinary <T>(value)); }
public static void Test() { SelfTest.SimpleStructure1 stest = default(SelfTest.SimpleStructure1); stest.Val1 = 1; stest.Test1 = new SelfTest.SimpleStructure2(); stest.Test1.Val2 = 2; stest.Test1.Val3 = 3; stest.Test1.Val4 = 4; stest.Test1.Val5 = "5"; stest.Test1.Val6 = new string[] { "6", "7", "8" }; stest.Test1.Val7 = new byte[][] { new byte[] { 9, 10, 11, 12, 13 }, new byte[] { 14, 15, 16 }, new byte[] { 17, 18, 19, 20 }, new byte[0] }; stest.Val8 = 14; Packet serialized = SerializeWriter.ToBinary <SelfTest.SimpleStructure1>(stest); for (int i = 0; i < serialized.Count; i++) { if (i != 0) { Console.Write("-"); } Console.Write("{0:x2}", serialized.Array[i + serialized.Offset]); } Console.WriteLine(); SelfTest.SimpleStructure1 simpleStructure; SerializeReader.FromBinary <SelfTest.SimpleStructure1>(serialized, out simpleStructure); SelfTest.ComplexStructure1 stestd = new SelfTest.ComplexStructure1(); SerializeWriter.ToBinary <SelfTest.ComplexStructure1>(stestd); stestd.d.h3.i[1].v6 = "Hello, world!"; Console.WriteLine(SerializeWriter.ToBinary <SelfTest.ComplexStructure1>(stestd).Count.ToString()); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); for (int watch = 0; watch < 1000000; watch++) { serialized = SerializeWriter.ToBinary <SelfTest.SimpleStructure1>(stest); } stopwatch.Stop(); Console.WriteLine("Elapsed: {0}ms", stopwatch.ElapsedMilliseconds); }