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; }
   }
}
\$\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\$ Jan 11, 2012 at 18:43
  • \$\begingroup\$ Good point Jeff! Will work for unknown types as long as they are serializable. \$\endgroup\$ 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);
         }
      }
   }
}
\$\endgroup\$
1
  • \$\begingroup\$ Great edits @Jesse C. Slicer - thanks - esp the asymmetric types on the extension methods. \$\endgroup\$ Jan 12, 2012 at 16:39
2
\$\begingroup\$

You should make sure that the object is Serializable.

A type that is serializable will return true for:

m.GetType().IsSerializable
\$\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\$ Feb 4, 2012 at 23:42
2
\$\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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.