Scheduling Objects

Serialization Formatters are supposed to be able to serialize a graph of
objects correctly. This implies it has to be able to cope with a graph in which
two different objects A and B reference a third one, C, without
writing C twice to the stream. This is very important because when the graph is
deserialized again, you must guarantee the identity of the reinflated C, and
the references to it.

This can be fairly easily done via a simple process called scheduling,
which consists of the following elements:

  • Each object is assigned a unique ID, ensuring that you can spot two references
    pointing to the same object, as in the case above.
  • Objects are scheduled for serialization through a FIFO queue, so instead of
    writing an object as soon as you see a reference to ir, you schedule it for
    later serialization, but only if you haven't seen the same object before.


These two requirements are fairly easy to deal with since the .NET framework
already supports them. Generating object IDs is done with the ObjectIDGenerator
class, which takes care of identifying objects for you and keeps track of which
objects you've already dealt with. IDs generated with it are all positive
64-bit integral values. To generate an ID for an object, all you need to do is
call the GetId() method which will give you a new ID if this is
the first time the object has been seen, or the original ID if it isn't, and
tell you so (via de firstTime out argument).

The second requirement, queuing, can be dealt with by using the
System.Collections.Queue class.

You can find an example of a simple class that does both of these
things here. As you will
see, it's extremely easy to do so.


Tomas Restrepo

Software developer located in Colombia. Sr. PFE at Microsoft.