How do you serialize a Stream (or more correctly Stream derived) data member of a class?
Assuming we have a 3rd Party class that we can't attribute:
public class Fubar
{
public Fubar() { ... }
public string Label { get; set; }
public int DataType { get; set; }
public Stream Data { get; set; } // Where it's always actually MemoryStream
};
I'm trying to use protobuf-net to serialize the class. Working through the exceptions and various SO questions I've come up with:
RuntimeTypeModel.Default.Add(typeof(Stream), true)
.AddSubType(1, typeof(MemoryStream));
RuntimeTypeModel.Default.Add(typeof(Fubar), false)
.Add(1, "Label")
.Add(2, "DataType")
.Add(3, "Data");
using (MemoryStream ms = new MemoryStream())
{
Fubar f1 = new Fubar();
/* f1 initialized */
// Serialize f1
Serializer.SerializeWithLengthPrefix<Message>(ms, f1, PrefixStyle.Base128);
// Now let's de-serialize
ms.Position = 0;
Fubar f2 = Serializer.DeserializeWithLengthPrefix<Fubar>(ms, PrefixStyle.Base128);
}
The above runs with no errors. The Label and DataType are correct in f2 but the Data variable is just an empty stream. Debugging the code I see that the memory stream is something like 29 bytes (while the Data stream in f1 itself is over 77KiB).
I feel as if I'm missing something fairly trivial but just can't seem to figure out what it would be. I assume that it is indeed possible to serialize a stream data member. Do I have to perhaps somehow specify the data properties for the Stream or MemoryStream types as well?