public void Serialize(Stream serializationStream, Object graph, Header[] headers) { // Validate the parameters. if (graph == null) { throw new ArgumentNullException("graph"); } if (serializationStream == null) { throw new ArgumentNullException("serializationStream"); } // Wrap the stream in a binary writer. using (BinaryWriter writer = new BinaryWriter(serializationStream)) { // Create a binary value writing context. BinaryValueWriter.BinaryValueContext context = new BinaryValueWriter.BinaryValueContext(this, writer); // Allocate object ID's 1 and 2 to the top-most // object graph and the header block, respectively. bool firstTime; context.gen.GetId(graph, out firstTime); if (headers != null) { context.gen.GetId(headers, out firstTime); } // Write the header information. WriteHeader(writer, (headers != null)); // Write the main object for the message. if (graph is IMethodCallMessage) { // TODO } else if (graph is IMethodReturnMessage) { // TODO } else { if (headers != null) { context.queue.Enqueue(headers); } context.queue.Enqueue(graph); } // Process outstanding queued objects. context.ProcessQueue(); // Write the footer information. WriteFooter(writer); } }
// Write an object to a stream. internal void WriteObject (BinaryValueWriter.BinaryValueContext context, Object value) { // Handle the null case first. if (value == null) { context.writer.Write((byte)(BinaryElementType.NullValue)); return; } // Get the type of the object and see if we've // processed the type before. Type type = value.GetType(); long typeID = context.gen.GetIDForType(type); // Allocate an object identifier. bool firstTime; long objectID = context.gen.GetId(value, out firstTime); if (typeID == -1) { context.gen.RegisterType(type, objectID); } // Get a value writer for the type. BinaryValueWriter writer; writer = BinaryValueWriter.GetWriter(context, type); // Write the object header. writer.WriteObjectHeader (context, value, type, objectID, typeID); // Write the object internals. writer.WriteObject(context, value, type); }