How to serialize a rather complex structure into a byte[] array, using BinaryWriter?
Update:
For this to work, every structure (and sub-structure?) must be decorated with the [Serializable] attribute.
I do not need to implement the ISerializable interface, as this is designed to give an object control over its own serialization.
Use the BinaryFormatter to serialize an object to a byte[]. BinaryWriter is just for writing bytes to a stream.
MyObject obj = new MyObject();
byte[] bytes;
IFormatter formatter = new BinaryFormatter();
using (MemoryStream stream = new MemoryStream())
{
formatter.Serialize(stream, obj);
bytes = stream.ToArray();
}