Question

I have been using a Xml serializer to serialize an class and saved it into an object which i later will send to a server. Due the amount of messages i send to the server i decided to change the serialization method into something that will result into something smaller in size.

I found protobuf-net but i only did find documentation about how to serialize a class into a file stream. It seems to me that saving to a file then send it to the server would not be very effective if you send over 100 packages every second. So my question is , how can i serialize a class and save it into an object?

Was it helpful?

Solution

protobuf-net can write to (or read from) any Stream implementation. FileStream is just an example. In the case of communications between machines, this could be a NetworkStream. If you just want to get an in-memory form, then use MemoryStream. For example:

byte[] chunk;
using(var ms = new MemoryStream())
{
    Serializer.Serialize(ms, obj);
    chunk = ms.ToArray();
}
// now do something interesting with 'chunk'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top