Skip to main content
BinaryFormatter methods are not thread-safe, so I ditched the single instance.
Source Link
Jesse C. Slicer
  • 14.6k
  • 1
  • 40
  • 54

Your methodology is solid on the generics front. Highly recommend using using statements rather than try..finallys and I also hoisted the BinaryFormatter creation out to relieve any GC pressure. Finally, I also converted themthe methods to extension methods. Your methodology is solid on the generics front.

namespace Codingoutloud
{
   using System.IO;
   using System.Runtime.Serialization.Formatters.Binary;

   public static class ByteArraySerializer
   {
      private static readonly BinaryFormatter formatter = new BinaryFormatter();

      public static byte[] Serialize<T>(this T m)
      {
         using (var ms = new MemoryStream())
         {
            formatternew BinaryFormatter().Serialize(ms, m);
            return ms.ToArray();
         }
      }

      public static T Deserialize<T>(this byte[] byteArray)
      {
         using (var ms = new MemoryStream(byteArray))
         {
            return (T)formatternew BinaryFormatter().Deserialize(ms);
         }
      }
   }
}

Highly recommend using using statements rather than try..finallys and I also hoisted the BinaryFormatter creation out to relieve any GC pressure. Finally, I converted them to extension methods. Your methodology is solid on the generics front.

namespace Codingoutloud
{
   using System.IO;
   using System.Runtime.Serialization.Formatters.Binary;

   public static class ByteArraySerializer
   {
      private static readonly BinaryFormatter formatter = new BinaryFormatter();

      public static byte[] Serialize<T>(this T m)
      {
         using (var ms = new MemoryStream())
         {
            formatter.Serialize(ms, m);
            return ms.ToArray();
         }
      }

      public static T Deserialize<T>(this byte[] byteArray)
      {
         using (var ms = new MemoryStream(byteArray))
         {
            return (T)formatter.Deserialize(ms);
         }
      }
   }
}

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);
         }
      }
   }
}
added 60 characters in body
Source Link
Jesse C. Slicer
  • 14.6k
  • 1
  • 40
  • 54

Highly recommend using using statements rather than try..finallys and I also hoisted the BinaryFormatter creation out to relieve any GC pressure. Finally, I also madeconverted them to extension methods. Your methodology is solid on the generics front.

namespace Codingoutloud
{
   using System.IO;
   using System.Runtime.Serialization.Formatters.Binary;

   public static class ByteArraySerializer
   {
      private static readonly BinaryFormatter formatter = new BinaryFormatter();

      public static byte[] Serialize<T>(this T m)
      {
         using (var ms = new MemoryStream())
         {
            formatter.Serialize(ms, m);
            return ms.ToArray();
         }
      }

      public static T Deserialize<T>(this byte[] byteArray)
      {
         using (var ms = new MemoryStream(byteArray))
         {
            return (T)formatter.Deserialize(ms);
         }
      }
   }
}

Highly recommend using using statements rather than try..finallys and I also hoisted the BinaryFormatter creation out to relieve any GC pressure. I also made them extension methods.

namespace Codingoutloud
{
   using System.IO;
   using System.Runtime.Serialization.Formatters.Binary;

   public static class ByteArraySerializer
   {
      private static readonly BinaryFormatter formatter = new BinaryFormatter();

      public static byte[] Serialize<T>(this T m)
      {
         using (var ms = new MemoryStream())
         {
            formatter.Serialize(ms, m);
            return ms.ToArray();
         }
      }

      public static T Deserialize<T>(this byte[] byteArray)
      {
         using (var ms = new MemoryStream(byteArray))
         {
            return (T)formatter.Deserialize(ms);
         }
      }
   }
}

Highly recommend using using statements rather than try..finallys and I also hoisted the BinaryFormatter creation out to relieve any GC pressure. Finally, I converted them to extension methods. Your methodology is solid on the generics front.

namespace Codingoutloud
{
   using System.IO;
   using System.Runtime.Serialization.Formatters.Binary;

   public static class ByteArraySerializer
   {
      private static readonly BinaryFormatter formatter = new BinaryFormatter();

      public static byte[] Serialize<T>(this T m)
      {
         using (var ms = new MemoryStream())
         {
            formatter.Serialize(ms, m);
            return ms.ToArray();
         }
      }

      public static T Deserialize<T>(this byte[] byteArray)
      {
         using (var ms = new MemoryStream(byteArray))
         {
            return (T)formatter.Deserialize(ms);
         }
      }
   }
}
Source Link
Jesse C. Slicer
  • 14.6k
  • 1
  • 40
  • 54

Highly recommend using using statements rather than try..finallys and I also hoisted the BinaryFormatter creation out to relieve any GC pressure. I also made them extension methods.

namespace Codingoutloud
{
   using System.IO;
   using System.Runtime.Serialization.Formatters.Binary;

   public static class ByteArraySerializer
   {
      private static readonly BinaryFormatter formatter = new BinaryFormatter();

      public static byte[] Serialize<T>(this T m)
      {
         using (var ms = new MemoryStream())
         {
            formatter.Serialize(ms, m);
            return ms.ToArray();
         }
      }

      public static T Deserialize<T>(this byte[] byteArray)
      {
         using (var ms = new MemoryStream(byteArray))
         {
            return (T)formatter.Deserialize(ms);
         }
      }
   }
}