I'm working on a project where I'm building a byte array that's almost like a serialization of the object graph. Each class is responsible for writing out it's own data, so I'm thinking about using a BinaryWriter and pass that instance around to my child objects and let them serialize themselves into this instead of creating temporary buffers (byte arrays) and merging them.
However I need to prepend each childdata with a byte that specifies the size of the following block, but that isn't known until the child has written all data, and by that time, the writer is positioned at the end of the childdata. I could of course create a temporary buffer and pass a temporary BinaryWriter to the child and then write the data to the "main" BinaryWriter after that, but that feels like a hackish solution.
Are there any alternative solutions? Is it maybe a bad idea to tamper with the position of the BinaryWriter? I would like to avoid creating temporary buffers/writers if possible and just let each child write to my "main" writer/buffer
The solution below might work, but is a little bit hackish. Are there any nicer solutions? (I haven't tested it so there's probably not perfectly byte aligned)
public abstract class BaseObject
{
public abstract void Serialize(BinaryWriter writer);
}
public class Program()
{
public static void Main()
{
List<BaseObject> myChildren = new List<BaseObject>();
// ... Initialize children here..
MemoryStream memoryStream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(memoryStream);
foreach (BaseObject child in myChildren) {
writer.Write((byte)0); // Dummy placeholder. Length is a byte
long sizePosition = writer.BaseStream.Position;
child.Serialize(writer);
long currentPosition = writer.BaseStream.Position;
writer.Seek(sizePosition, SeekOrigin.Begin);
writer.Write((byte)currentPosition-sizePosition);
writer.Seek(currentPosition , SeekOrigin.Begin);
}
}
}