[p=24, null, left]Many of you probably noticed that Windows Phone 7 does not support standard Binary Serialization (using
BinaryFormatter from System.Runtime.Serialization namespace), like full version of .NET Framework does, so I have decided to create
my own custom solution for Binary serializationand deserialization using BinaryWriter and BinaryReader classes. This post covers my solution partly.
[p=24, null, left]
Additional Information Adding a reference[p=24, null, left]Probably this step could be optional if you decide to modify my code, but currently my approach to Binary serialization requires System.Runtime.Serialization namespace to be added to project for creating a SampleData class described bellow. To add a reference right click the project name and choose Add Reference there. Select System.Runtime.Serialization namespace on .NET tab (check image bellow). [p=24, null, left]
复制代码
Creating CustomBinarySerializer class
My goal was to make Binary serialization and deserialization look similar to
DataContract
and
XML Serialization
, meaning that the should be a constructor that accepts type as parameter and two methods WriteObject and ReadObject. For this reasons I have created CustomBinarySerializer class. Currently, this class handles onlystring and List<int> types (for my SampleData class described above). Feel free to add other handled types and, perhaps, modify this class by creatingextention methods for those types like described inthis article. Partly implemented version of CustomBinarySerializer looks like that (pay attention that this class serialize and deserialize only those properties that are marked withDataMemberAttribute):
复制代码
SerializationTo serialize an object you need to create an instance of CustomBinarySerializer class (passing a type of serialized object as an input parameter) and then write serialized data to a stream object using WriteObject method, that accepts stream and object for serialization.
复制代码
DeserializationTo deserialize your object you need to create an instance of CustomBinarySerializer class again (based on a type of serialized object) and then read serialized data using ReadObject method, that accepts an instance of Stream class as parameter.
复制代码
TestingYou can use method bellow to test Binary serialization and deserialization. SampleData object after deserialization should be exactly the same as the one before the serialization.
复制代码
Here is how serialized SampleData object looks like during the debug:
Additional Information Adding a reference[p=24, null, left]Probably this step could be optional if you decide to modify my code, but currently my approach to Binary serialization requires System.Runtime.Serialization namespace to be added to project for creating a SampleData class described bellow. To add a reference right click the project name and choose Add Reference there. Select System.Runtime.Serialization namespace on .NET tab (check image bellow). [p=24, null, left]
Creating a sample classI am going to use almost the same class I’ve created for theDataContract Serialization tutorial. To allow properties to be serialized you have to apply DataMemberAttribute to them.
- public class SampleData
- {
- [DataMember]
- public string ContentText { get; set; }
- [DataMember]
- public List<int> SomeItems { get; set; }
- public SampleData()
- {
- ContentText = "some text";
- SomeItems = new List<int>() { 1, 2, 3 };
- }
- }
- public class CustomBinarySerializer
- {
- private List<PropertyInfo> serializableProperties = new List<PropertyInfo>();
- private Type serializableObjectType;
- public CustomBinarySerializer(Type objectType)
- {
- serializableObjectType = objectType;
- serializableProperties = GetMarkedProperties(objectType);
- }
- private List<PropertyInfo> GetMarkedProperties(Type type)
- {
- return (from property in type.GetProperties()
- where property.GetCustomAttributes(true)
- .Where((x) => x is System.Runtime.Serialization.DataMemberAttribute).Count() > 0
- select property
- ).ToList();
- }
- #region Write
- public void WriteObject(Stream stream, object graph)
- {
- if (stream == null || graph == null)
- return;
- BinaryWriter bw = new BinaryWriter(stream);
- foreach (PropertyInfo pi in serializableProperties)
- {
- var value = pi.GetValue(graph, null);
- if (pi.PropertyType == typeof(string))
- {
- bw.Write(value as string ?? string.Empty);
- }
- else if (pi.PropertyType == typeof(List<int>))
- {
- WriteIntegerList(bw, value as List<int>);
- }
- }
- }
- private void WriteIntegerList(BinaryWriter bw, List<int> list)
- {
- if (list == null || !list.Any())
- {
- bw.Write(0);
- }
- else
- {
- bw.Write(list.Count);
- list.ForEach(x => bw.Write(x));
- }
- }
- #endregion Write
- #region Read
- public object ReadObject(Stream stream)
- {
- if (stream == null)
- return null;
- BinaryReader br = new BinaryReader(stream);
- object deserializedObject = Activator.CreateInstance(serializableObjectType);
- foreach (PropertyInfo pi in serializableProperties)
- {
- if (pi.PropertyType == typeof(string))
- {
- pi.SetValue(deserializedObject, br.ReadString(), null);
- }
- else if (pi.PropertyType == typeof(List<int>))
- {
- pi.SetValue(deserializedObject, ReadIntegerList(br), null);
- }
- }
- return deserializedObject;
- }
- private List<int> ReadIntegerList(BinaryReader br)
- {
- List<int> list = new List<int>();
- int count = br.ReadInt32();
- int index = count;
- while(index > 0){
- list.Add(br.ReadInt32());
- index--;
- }
- return list;
- }
- #endregion Read
- }
SerializationTo serialize an object you need to create an instance of CustomBinarySerializer class (passing a type of serialized object as an input parameter) and then write serialized data to a stream object using WriteObject method, that accepts stream and object for serialization.
- public static void Serialize(Stream streamObject, object objForSerialization)
- {
- if (objForSerialization == null || streamObject == null)
- return;
- CustomBinarySerializer ser = new CustomBinarySerializer(objForSerialization.GetType());
- ser.WriteObject(streamObject, objForSerialization);
- }
DeserializationTo deserialize your object you need to create an instance of CustomBinarySerializer class again (based on a type of serialized object) and then read serialized data using ReadObject method, that accepts an instance of Stream class as parameter.
- public static object Deserialize(Stream streamObject, Type serializedObjectType)
- {
- if (serializedObjectType == null || streamObject == null)
- return null;
- CustomBinarySerializer ser = new CustomBinarySerializer(serializedObjectType);
- return ser.ReadObject(streamObject);
- }
TestingYou can use method bellow to test Binary serialization and deserialization. SampleData object after deserialization should be exactly the same as the one before the serialization.
- public static void Test()
- {
- // serialization
- MemoryStream ms = new MemoryStream();
- BinarySerializationHelper.Serialize(ms, new SampleData());
- ms.Position = 0;
- // deserialization
- var sampleData = BinarySerializationHelper.Deserialize(ms, typeof(SampleData));
- ms.Close();
- }