CHAPTER 20 FILE I/O AND OBJECT SERIALIZATION
Once you assign all objects a numerical value, the object graph can record each object’s set of
dependencies.
For example, assume you have created a set of classes that model some automobiles (of course).
You have a base class named Car, which has-a Radio. Another class named JamesBondCar extends the Car
base type. Figure 20-4 shows a possible object graph that models these relationships.
Figure 20-4. A simple object graph
When reading object graphs, you can use the phrase “depends on” or “refers to” when connecting the
arrows. Thus, in Figure 20-4, you can see that the Car refers to the Radio class (given the has-a
relationship). JamesBondCar refers to Car (given the is-a relationship), as well as to Radio (it inherits this
protected member variable).
Of course, the CLR does not paint pictures in memory to represent a graph of related objects.
Rather, the relationship documented in the previous diagram is represented by a mathematical formula
that looks something like this:
[Car 3, ref 2], [Radio 2], [JamesBondCar 1, ref 3, ref 2]
If you parse this formula, you can see that object 3 (the Car) has a dependency on object 2 (the
Radio). Object 2, the Radio, is a lone wolf and requires nobody. Finally, object 1 (the JamesBondCar) has a
dependency on object 3, as well as object 2. In any case, when you serialize or deserialize an instance of
JamesBondCar, the object graph ensures that the Radio and Car types also participate in the process.
The beautiful thing about the serialization process is that the graph representing the relationships
among your objects is established automatically behind the scenes. As you will see later in this chapter,
however, you can become more involved in the construction of a given object graph by customizing the
serialization process using attributes and interfaces.
Note Strictly speaking, the XmlSerializer type (described later in this chapter) does not persist state using
object graphs; however, this type still serializes and deserializes related objects in a predictable manner.
782