Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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; }
   }
}
share|improve this question
1  
It should be noted that this only (de)serializes serializable objects, so it won't necessarily work for any arbitrary unknown object. –  Jeff Mercado Jan 11 '12 at 18:43
    
Good point Jeff! Will work for unknown types as long as they are serializable. –  codingoutloud Jan 12 '12 at 15:31
add comment

2 Answers 2

up vote 6 down vote accepted

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);
         }
      }
   }
}
share|improve this answer
    
Great edits @Jesse C. Slicer - thanks - esp the asymmetric types on the extension methods. –  codingoutloud Jan 12 '12 at 16:39
add comment

You should make sure that the object is Serializable.

A type that is serializable will return true for:

m.GetType().IsSerializable
share|improve this answer
    
thank you - good comment. I think that could be enforced with a Diagnostics.Debug.Assert or a CodeContract (from .NET 4). –  codingoutloud Feb 4 '12 at 23:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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