public void SerializeCommand() { var cmd = new TestCommand(); cmd.Name = "test data"; var buffer = new System.IO.MemoryStream(); #if !SILVERLIGHT var bf = (TestCommand)Csla.Core.ObjectCloner.Clone(cmd); Assert.AreEqual(cmd.Name, bf.Name, "after BinaryFormatter"); var ndcs = new System.Runtime.Serialization.NetDataContractSerializer(); ndcs.Serialize(buffer, cmd); buffer.Position = 0; var n = (TestCommand)ndcs.Deserialize(buffer); Assert.AreEqual(cmd.Name, n.Name, "after NDCS"); #endif buffer = new System.IO.MemoryStream(); var mf = new Csla.Serialization.Mobile.MobileFormatter(); mf.Serialize(buffer, cmd); buffer.Position = 0; var m = (TestCommand)mf.Deserialize(buffer); Assert.AreEqual(cmd.Name, m.Name, "after MobileFormatter"); }
/// <summary> /// Universal object method used to serialize ANY object to disk. /// </summary> /// <typeparam name="T">The type of the target object.</typeparam> /// <param name="obj">The triggering object.</param> /// <param name="filePath">The path on disk for the save file.</param> /// <returns>True if save successful, otherwise False.</returns> public static bool Save <T>(this T obj, string filePath = "File.bin") { try { lock (lockManager) { using (System.IO.Stream stream = System.IO.File.Open( filePath, System.IO.FileMode.Create)) { var serializer = new System.Runtime.Serialization.NetDataContractSerializer(); serializer.Serialize(stream, obj); return(true); } } } catch (Exception ex) { //Dump error. ConsoleColor currentColor = Console.ForegroundColor; Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine(ex.ToString()); Console.ForegroundColor = currentColor; return(false); } }
private void saveToolStripMenuItem_Click(object sender, EventArgs e) { rootNode.Clear(); using (System.IO.FileStream stream = new System.IO.FileStream("grad.xml", System.IO.FileMode.Create, System.IO.FileAccess.Write)) serializer.Serialize(stream, rootNode); MessageBox.Show("OK"); }
public object Disassemble(object value) { if (value == null) { return(DBNull.Value); } if (value == DBNull.Value) { return(DBNull.Value); } if (!(value is IDictionary <string, FormProperty>)) { throw new ArgumentException(); } using (var stream = new MemoryStream()) { var formatter = new XmlSerializer(); formatter.Serialize(stream, value); return(Encoding.UTF8.GetString(stream.ToArray())); } }
public static RuntimeTypeModel BuildMeta() { RuntimeTypeModel model; #if !FX11 model = TypeModel.Create(); model.Add(typeof(Order), false) .Add(1, "OrderID") .Add(2, "CustomerID") .Add(3, "EmployeeID") .Add(4, "OrderDate") .Add(5, "RequiredDate") .Add(6, "ShippedDate") .Add(7, "ShipVia") .Add(8, "Freight") .Add(9, "ShipName") .Add(10, "ShipAddress") .Add(11, "ShipCity") .Add(12, "ShipRegion") .Add(13, "ShipPostalCode") .Add(14, "ShipCountry"); model.Add(typeof(Product), false) .Add(1, "ProductID") .Add(2, "ProductName") .Add(3, "SupplierID") .Add(4, "CategoryID") .Add(5, "QuantityPerUnit") .Add(6, "UnitPrice") .Add(7, "UnitsInStock") .Add(8, "UnitsOnOrder") .Add(9, "ReorderLevel") .Add(10, "Discontinued") .Add(11, "LastEditDate") .Add(12, "CreationDate"); TypeModel compiled = model.Compile(); Type type = typeof(Product); PropertyInfo[] props = type.GetProperties(); Product prod = new Product(); prod.ProductID = 123; prod.ProductName = "abc devil"; prod.SupplierID = 456; prod.CategoryID = 13; prod.QuantityPerUnit = "1"; prod.UnitPrice = 12.99M; prod.UnitsInStock = 96; prod.UnitsOnOrder = 12; prod.ReorderLevel = 30; prod.Discontinued = false; prod.LastEditDate = new DateTime(2009, 5, 7); prod.CreationDate = new DateTime(2009, 1, 3); DumpObject("Original", props, prod); const int loop = 100000; Console.WriteLine("Iterations: " + loop); Stopwatch watch; MemoryStream reuseDump = new MemoryStream(100 * 1024); #if FX30 System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(type); using (MemoryStream ms = new MemoryStream()) { dcs.WriteObject(ms, prod); Console.WriteLine("DataContractSerializer: {0} bytes", ms.Length); } watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { reuseDump.SetLength(0); dcs.WriteObject(reuseDump, prod); } watch.Stop(); Console.WriteLine("DataContractSerializer serialize: {0} ms", watch.ElapsedMilliseconds); watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { reuseDump.Position = 0; dcs.ReadObject(reuseDump); } watch.Stop(); Console.WriteLine("DataContractSerializer deserialize: {0} ms", watch.ElapsedMilliseconds); { reuseDump.Position = 0; Product p1 = (Product)dcs.ReadObject(reuseDump); DumpObject("DataContractSerializer", props, p1); } System.Runtime.Serialization.NetDataContractSerializer ndcs = new System.Runtime.Serialization.NetDataContractSerializer(); using (MemoryStream ms = new MemoryStream()) { ndcs.Serialize(ms, prod); Console.WriteLine("NetDataContractSerializer: {0} bytes", ms.Length); } watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { reuseDump.SetLength(0); ndcs.Serialize(reuseDump, prod); } watch.Stop(); Console.WriteLine("NetDataContractSerializer serialize: {0} ms", watch.ElapsedMilliseconds); watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { reuseDump.Position = 0; ndcs.Deserialize(reuseDump); } watch.Stop(); Console.WriteLine("NetDataContractSerializer deserialize: {0} ms", watch.ElapsedMilliseconds); { reuseDump.Position = 0; Product p1 = (Product)ndcs.Deserialize(reuseDump); DumpObject("NetDataContractSerializer", props, p1); } #endif using (MemoryStream ms = new MemoryStream()) { compiled.Serialize(ms, prod); #if COREFX ArraySegment <byte> tmp; if (!ms.TryGetBuffer(out tmp)) { throw new Exception("oops"); } byte[] buffer = tmp.Array; #else byte[] buffer = ms.GetBuffer(); #endif int len = (int)ms.Length; Console.WriteLine("protobuf-net v2: {0} bytes", len); for (int i = 0; i < len; i++) { Console.Write(buffer[i].ToString("x2")); } Console.WriteLine(); } watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { reuseDump.SetLength(0); compiled.Serialize(reuseDump, prod); } watch.Stop(); Console.WriteLine("protobuf-net v2 serialize: {0} ms", watch.ElapsedMilliseconds); watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { reuseDump.Position = 0; compiled.Deserialize(reuseDump, null, type); } watch.Stop(); Console.WriteLine("protobuf-net v2 deserialize: {0} ms", watch.ElapsedMilliseconds); { reuseDump.Position = 0; Product p1 = (Product)compiled.Deserialize(reuseDump, null, type); DumpObject("protobuf-net v2", props, p1); } // 080d 1203(616263) 207b // 3205(08b9601804) // 5000 6204(08cede01) // 00 08 = 1|000 = field 1, variant // 01 0d = 13 // 02 12 = 10|010 = field 2, string // 03 03 = length 3 // 04 616263 = "abc" // 07 20 = 100|000 = field 4, variant // 08 7B = 123 // 09 32 = 110|010 = field 6, string // 10 05 = length 5 // 11 08 = 1|000 = field 1, variant // 12 b960 (le) = 1100000:0111001 = 12345 // 14 18 = 11|000 = field 3, variant // 15 04 = 4 (signScale = scale 2, +ve) // 16 50 = 1010|000 = field 10, variant // 17 00 = false // 18 62 = 1100|010 = field 12, string // 19 04 = length 4 // 20 08 = 1|000 = field 1, variant // 21 cede01 = 1:1011110:1001110 = 28494 (days, signed) = 14247 = 03/01/2009 Product clone = (Product)compiled.DeepClone(prod); Console.WriteLine(clone.CategoryID); Console.WriteLine(clone.ProductName); Console.WriteLine(clone.CreationDate); Console.WriteLine(clone.ProductID); Console.WriteLine(clone.UnitPrice); #endif model = TypeModel.Create(); model.Add(typeof(Customer), false) .Add(1, "Id") .Add(3, "Name") #if !FX11 .Add(5, "HowMuch") .Add(6, "HasValue") #endif ; ; model.Add(typeof(CustomerStruct), false) .Add(1, "Id") .Add(3, "Name") #if !FX11 .Add(5, "HowMuch") .Add(6, "HasValue") #endif ; return(model); }
public static RuntimeTypeModel BuildMeta() { RuntimeTypeModel model; #if !FX11 model = TypeModel.Create(); model.Add(typeof(Order), false) .Add(1, "OrderID") .Add(2, "CustomerID") .Add(3, "EmployeeID") .Add(4, "OrderDate") .Add(5, "RequiredDate") .Add(6, "ShippedDate") .Add(7, "ShipVia") .Add(8, "Freight") .Add(9, "ShipName") .Add(10, "ShipAddress") .Add(11, "ShipCity") .Add(12, "ShipRegion") .Add(13, "ShipPostalCode") .Add(14, "ShipCountry"); model.Add(typeof(Product), false) .Add(1, "ProductID") .Add(2, "ProductName") .Add(3, "SupplierID") .Add(4, "CategoryID") .Add(5, "QuantityPerUnit") .Add(6, "UnitPrice") .Add(7, "UnitsInStock") .Add(8, "UnitsOnOrder") .Add(9, "ReorderLevel") .Add(10, "Discontinued") .Add(11, "LastEditDate") .Add(12, "CreationDate"); TypeModel compiled = model.Compile(); Type type = typeof(Product); PropertyInfo[] props = type.GetProperties(); Product prod = new Product(); prod.ProductID = 123; prod.ProductName = "abc devil"; prod.SupplierID = 456; prod.CategoryID = 13; prod.QuantityPerUnit = "1"; prod.UnitPrice = 12.99M; prod.UnitsInStock = 96; prod.UnitsOnOrder = 12; prod.ReorderLevel = 30; prod.Discontinued = false; prod.LastEditDate = new DateTime(2009, 5, 7); prod.CreationDate = new DateTime(2009, 1, 3); DumpObject("Original", props, prod); const int loop = 100000; Console.WriteLine("Iterations: " + loop); Stopwatch watch; MemoryStream reuseDump = new MemoryStream(100 * 1024); #if FX30 System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(type); using (MemoryStream ms = new MemoryStream()) { dcs.WriteObject(ms, prod); Console.WriteLine("DataContractSerializer: {0} bytes", ms.Length); } watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { reuseDump.SetLength(0); dcs.WriteObject(reuseDump, prod); } watch.Stop(); Console.WriteLine("DataContractSerializer serialize: {0} ms", watch.ElapsedMilliseconds); watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { reuseDump.Position = 0; dcs.ReadObject(reuseDump); } watch.Stop(); Console.WriteLine("DataContractSerializer deserialize: {0} ms", watch.ElapsedMilliseconds); { reuseDump.Position = 0; Product p1 = (Product) dcs.ReadObject(reuseDump); DumpObject("DataContractSerializer", props, p1); } System.Runtime.Serialization.NetDataContractSerializer ndcs = new System.Runtime.Serialization.NetDataContractSerializer(); using (MemoryStream ms = new MemoryStream()) { ndcs.Serialize(ms, prod); Console.WriteLine("NetDataContractSerializer: {0} bytes", ms.Length); } watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { reuseDump.SetLength(0); ndcs.Serialize(reuseDump, prod); } watch.Stop(); Console.WriteLine("NetDataContractSerializer serialize: {0} ms", watch.ElapsedMilliseconds); watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { reuseDump.Position = 0; ndcs.Deserialize(reuseDump); } watch.Stop(); Console.WriteLine("NetDataContractSerializer deserialize: {0} ms", watch.ElapsedMilliseconds); { reuseDump.Position = 0; Product p1 = (Product) ndcs.Deserialize(reuseDump); DumpObject("NetDataContractSerializer", props, p1); } #endif using (MemoryStream ms = new MemoryStream()) { compiled.Serialize(ms, prod); byte[] buffer = ms.GetBuffer(); int len = (int)ms.Length; Console.WriteLine("protobuf-net v2: {0} bytes", len); for (int i = 0; i < len; i++) { Console.Write(buffer[i].ToString("x2")); } Console.WriteLine(); } watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { reuseDump.SetLength(0); compiled.Serialize(reuseDump, prod); } watch.Stop(); Console.WriteLine("protobuf-net v2 serialize: {0} ms", watch.ElapsedMilliseconds); watch = Stopwatch.StartNew(); for (int i = 0; i < loop; i++) { reuseDump.Position = 0; compiled.Deserialize(reuseDump, null, type); } watch.Stop(); Console.WriteLine("protobuf-net v2 deserialize: {0} ms", watch.ElapsedMilliseconds); { reuseDump.Position = 0; Product p1 = (Product)compiled.Deserialize(reuseDump, null, type); DumpObject("protobuf-net v2", props, p1); } // 080d 1203(616263) 207b // 3205(08b9601804) // 5000 6204(08cede01) // 00 08 = 1|000 = field 1, variant // 01 0d = 13 // 02 12 = 10|010 = field 2, string // 03 03 = length 3 // 04 616263 = "abc" // 07 20 = 100|000 = field 4, variant // 08 7B = 123 // 09 32 = 110|010 = field 6, string // 10 05 = length 5 // 11 08 = 1|000 = field 1, variant // 12 b960 (le) = 1100000:0111001 = 12345 // 14 18 = 11|000 = field 3, variant // 15 04 = 4 (signScale = scale 2, +ve) // 16 50 = 1010|000 = field 10, variant // 17 00 = false // 18 62 = 1100|010 = field 12, string // 19 04 = length 4 // 20 08 = 1|000 = field 1, variant // 21 cede01 = 1:1011110:1001110 = 28494 (days, signed) = 14247 = 03/01/2009 Product clone = (Product)compiled.DeepClone(prod); Console.WriteLine(clone.CategoryID); Console.WriteLine(clone.ProductName); Console.WriteLine(clone.CreationDate); Console.WriteLine(clone.ProductID); Console.WriteLine(clone.UnitPrice); #endif model = TypeModel.Create(); model.Add(typeof(Customer), false) .Add(1, "Id") .Add(3, "Name") #if !FX11 .Add(5, "HowMuch") .Add(6, "HasValue") #endif ; ; model.Add(typeof(CustomerStruct), false) .Add(1, "Id") .Add(3, "Name") #if !FX11 .Add(5, "HowMuch") .Add(6, "HasValue") #endif ; return model; }