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; }
}
}
-
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\$Jeff Mercado– Jeff Mercado2012年01月11日 18:43:04 +00:00Commented Jan 11, 2012 at 18:43
-
\$\begingroup\$ Good point Jeff! Will work for unknown types as long as they are serializable. \$\endgroup\$codingoutloud– codingoutloud2012年01月12日 15:31:56 +00:00Commented Jan 12, 2012 at 15:31
3 Answers 3
Your methodology is solid on the generics front. Highly recommend using using
statements rather than try..finally
s. 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);
}
}
}
}
-
\$\begingroup\$ Great edits @Jesse C. Slicer - thanks - esp the asymmetric types on the extension methods. \$\endgroup\$codingoutloud– codingoutloud2012年01月12日 16:39:07 +00:00Commented Jan 12, 2012 at 16:39
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.
You should make sure that the object is Serializable.
A type that is serializable will return true
for:
m.GetType().IsSerializable
-
\$\begingroup\$ thank you - good comment. I think that could be enforced with a Diagnostics.Debug.Assert or a CodeContract (from .NET 4). \$\endgroup\$codingoutloud– codingoutloud2012年02月04日 23:42:55 +00:00Commented Feb 4, 2012 at 23:42