Created
September 24, 2013 15:02
-
-
Save cbilson/6686087 to your computer and use it in GitHub Desktop.
All of these assertions pass.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Runtime.Serialization; | |
using System.Runtime.Serialization.Formatters.Binary; | |
using System.Xml.Serialization; | |
namespace ClassLibrary1 | |
{ | |
[Serializable] | |
public class SomethingSerialized | |
{ | |
public string Name { get; set; } | |
} | |
public class Class1 | |
{ | |
public void BinarySerialization() | |
{ | |
var before = new SomethingSerialized {Name = "bob"}; | |
var formatter = new BinaryFormatter(); | |
var stm = new MemoryStream(); | |
formatter.Serialize(stm, before); | |
stm.Seek(0L, SeekOrigin.Begin); | |
var after = (SomethingSerialized) formatter.Deserialize(stm); | |
Debug.Assert(after.Name == before.Name); | |
} | |
public void XmlSerialization() | |
{ | |
var before = new SomethingSerialized {Name = "bob"}; | |
var serializer = new XmlSerializer(typeof(SomethingSerialized)); | |
var stm = new MemoryStream(); | |
serializer.Serialize(stm, before); | |
stm.Seek(0L, SeekOrigin.Begin); | |
var after = (SomethingSerialized) serializer.Deserialize(stm); | |
Debug.Assert(after.Name == before.Name); | |
} | |
public void DataContractSerialization() | |
{ | |
var before = new SomethingSerialized {Name = "bob"}; | |
var serializer = new DataContractSerializer(typeof(SomethingSerialized)); | |
var stm = new MemoryStream(); | |
serializer.WriteObject(stm, before); | |
stm.Seek(0L, SeekOrigin.Begin); | |
var after = (SomethingSerialized) serializer.ReadObject(stm); | |
Debug.Assert(after.Name == before.Name); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment