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\$CommentedJan 11, 2012 at 18:43
  • \$\begingroup\$Good point Jeff! Will work for unknown types as long as they are serializable.\$\endgroup\$CommentedJan 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\$CommentedJan 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.

\$\endgroup\$
    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\$CommentedFeb 4, 2012 at 23:42

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.