10
\$\begingroup\$

Usage example

var qm = new QueueMessage("foo", 99);
var ba = ByteArraySerializer<QueueMessage>.Serialize(qm));

Class that performs the serialization / deserialization

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Codingoutloud
{
 public static class ByteArraySerializer<T>
 {
 public static byte[] Serialize(T m)
 {
 var ms = new MemoryStream();
 try
 {
 var formatter = new BinaryFormatter();
 formatter.Serialize(ms, m);
 return ms.ToArray();
 }
 finally
 {
 ms.Close();
 }
 }
 public static T Deserialize(byte[] byteArray)
 {
 var ms = new MemoryStream(byteArray);
 try
 {
 var formatter = new BinaryFormatter();
 return (T)formatter.Deserialize(ms);
 }
 finally
 {
 ms.Close();
 }
 }
 }
}

Example of an object we would serialize

using System;
namespace Codingoutloud
{
 [Serializable]
 public class QueueMessage
 {
 public QueueMessage() {}
 public QueueMessage(string name, int id)
 {
 Name = name;
 Id = id;
 }
 public string Name { get; set; }
 public int Id { get; set; }
 }
}
200_success
146k22 gold badges190 silver badges479 bronze badges
asked Jan 11, 2012 at 16:01
\$\endgroup\$
2
  • 1
    \$\begingroup\$ It should be noted that this only (de)serializes serializable objects, so it won't necessarily work for any arbitrary unknown object. \$\endgroup\$ Commented Jan 11, 2012 at 18:43
  • \$\begingroup\$ Good point Jeff! Will work for unknown types as long as they are serializable. \$\endgroup\$ Commented Jan 12, 2012 at 15:31

3 Answers 3

12
\$\begingroup\$

Your methodology is solid on the generics front. Highly recommend using using statements rather than try..finallys. I also converted the methods to extension methods.

namespace Codingoutloud
{
 using System.IO;
 using System.Runtime.Serialization.Formatters.Binary;
 public static class ByteArraySerializer
 {
 public static byte[] Serialize<T>(this T m)
 {
 using (var ms = new MemoryStream())
 {
 new BinaryFormatter().Serialize(ms, m);
 return ms.ToArray();
 }
 }
 public static T Deserialize<T>(this byte[] byteArray)
 {
 using (var ms = new MemoryStream(byteArray))
 {
 return (T)new BinaryFormatter().Deserialize(ms);
 }
 }
 }
}
answered Jan 11, 2012 at 16:55
\$\endgroup\$
1
  • \$\begingroup\$ Great edits @Jesse C. Slicer - thanks - esp the asymmetric types on the extension methods. \$\endgroup\$ Commented Jan 12, 2012 at 16:39
3
\$\begingroup\$

It's possible to serialize "unserializable" types via reflection, including private unserialized members and properties, though its a pain to deal with arbitrary type, you can certainly deal with all the common types easily, and provide for delegates to serialize/deserialize custom types. The main thing for dealing with "unknown" types is to find out if they support a Set method - if not, you can't deserialize them, so no point to serialize them.

answered Oct 11, 2018 at 4:15
\$\endgroup\$
2
\$\begingroup\$

You should make sure that the object is Serializable.

A type that is serializable will return true for:

m.GetType().IsSerializable
James Khoury
3,1651 gold badge25 silver badges51 bronze badges
answered Jan 16, 2012 at 23:45
\$\endgroup\$
1
  • \$\begingroup\$ thank you - good comment. I think that could be enforced with a Diagnostics.Debug.Assert or a CodeContract (from .NET 4). \$\endgroup\$ Commented Feb 4, 2012 at 23:42

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.