Serializing and Deserializing an Object Graph Using BinaryFormatter

I am trying to serialize my object graph into a string and then deserialize it from a string. The object is serialized just fine if I do it

using (var memStream = new System.IO.MemoryStream()) { mf.Serialize(memStream, this); memStream.Seek(0, 0); Search s; using (var memStrClone = new System.IO.MemoryStream()) { memStream.CopyTo(memStrClone); memStrClone.Seek(0, 0); s = mf.Deserialize(memStrClone) as Search; } } 

The above code works, but serializes to a string and tries to deserialize the same string as this

 Search s; string xml = ToString<Search>(this); s = FromString<Search>(xml); public static TType FromString<TType>(string input) { var byteArray = Encoding.ASCII.GetBytes(input); using (var stream = new MemoryStream(byteArray)) { var bf = new BinaryFormatter(); return (TType)bf.Deserialize(stream); } } public static string ToString<TType>(TType data) { using (var ms = new MemoryStream()) { var bf = new BinaryFormatter(); bf.Serialize(ms, data); return Encoding.ASCII.GetString(ms.GetBuffer()); } } 

Throws an exception

There is no assembly identifier for object type '1936026741 Core.Sebring.BusinessObjects.Search.Search'.

Any help is appreciated. Thanks.

+4
source share
1 answer

Here is some code that will do what you want (I think), but I have to ask - why do you want to serialize such a string?

If the class is simple enough to serialize to a string, use an XML serializer that is much easier to handle; if you want to serialize it to disk, the binary writes it to a file, and if it is complex and you serialize it for transfer - think about using something like protobuf-net.

I think the essence of your problem is that you are trying to use ASCII encoding - I am using Base64 encoding.

Anyway - here it goes (I just guessed in your search class!)

  class Program { [Serializable] public class Search { public Guid ID { get; private set; } public Search() { } public Search(Guid id) { ID = id; } public override string ToString() { return ID.ToString(); } } static void Main(string[] args) { Search search = new Search(Guid.NewGuid()); Console.WriteLine(search); string serialized = SerializeTest.SerializeToString(search); Search rehydrated = SerializeTest.DeSerializeFromString<Search>(serialized); Console.WriteLine(rehydrated); Console.ReadLine(); } } public class SerializeTest { public static Encoding _Encoding = Encoding.Unicode; public static string SerializeToString(object obj) { byte[] byteArray = BinarySerializeObject(obj); return Convert.ToBase64String(byteArray); } public static T DeSerializeFromString<T>(string input) { byte[] byteArray = Convert.FromBase64String(input); return BinaryDeserializeObject<T>(byteArray); } /// <summary> /// Takes a byte array and deserializes it back to its type of <see cref="T"/> /// </summary> /// <typeparam name="T">The Type to deserialize to</typeparam> /// <param name="serializedType">The object as a byte array</param> /// <returns>The deserialized type</returns> public static T BinaryDeserializeObject<T>(byte[] serializedType) { if (serializedType == null) throw new ArgumentNullException("serializedType"); if (serializedType.Length.Equals(0)) throw new ArgumentException("serializedType"); T deserializedObject; using (MemoryStream memoryStream = new MemoryStream(serializedType)) { BinaryFormatter deserializer = new BinaryFormatter(); deserializedObject = (T)deserializer.Deserialize(memoryStream); } return deserializedObject; } /// <summary> /// Takes an object and serializes it into a byte array /// </summary> /// <param name="objectToSerialize">The object to serialize</param> /// <returns>The object as a <see cref="byte"/> array</returns> public static byte[] BinarySerializeObject(object objectToSerialize) { if (objectToSerialize == null) throw new ArgumentNullException("objectToSerialize"); byte[] serializedObject; using (MemoryStream stream = new MemoryStream()) { BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(stream, objectToSerialize); serializedObject = stream.ToArray(); } return serializedObject; } } 

NTN

+8
source

All Articles