CHAPTER 20 FILE I/O AND OBJECT SERIALIZATION
If you were to serialize this new type, you would again find that the data has been persisted as
uppercase and deserialized as lowercase.
Source Code You can find the CustomSerialization project under the Chapter 20 subdirectory.
With this example behind you, your exploration of the core details of object serialization services,
including various ways to customize the process, is complete. As you have seen, the serialization and
deserialization process makes it easy to persist large amounts of data, and it can be less labor-intensive
than working with the various reader/writer classes of the System.IO namespace.
Summary
You began this chapter by examining the use of the Directory(Info) and File(Info) types. As you
learned, these classes allow you to manipulate a physical file or directory on your hard drive. Next, you
examined a number of classes derived from the abstract Stream class. Given that Stream-derived types
operate on a raw stream of bytes, the System.IO namespace provides numerous reader/writer types (e.g.,
StreamWriter, StringWriter, and BinaryWriter) that simplify the process. Along the way, you also
checked out the functionality provided by DriveType, learned how to monitor files using the
FileSystemWatcher type, and saw how to interact with streams in an asynchronous manner.
This chapter also introduced you to the topic of object serialization services. As you have seen, the
.NET platform uses an object graph to account for the full set of related objects that you want to persist
to a stream. As long as each member in the object graph has been marked with the [Serializable]
attribute, the data is persisted using your format of choice (binary or SOAP).
You also learned that it is possible to customize the out-of-the-box serialization process using two
possible approaches. First, you learned how to implement the ISerializable interface (and support a
special private constructor), which enables you to become more involved with how formatters persist
the supplied data. Second, you learned about a set of .NET attributes that simplify the process of custom
serialization. All you need to do is apply the [OnSerializing], [OnSerialized], [OnDeserializing], or
[OnDeserialized] attribute on members that take a StreamingContext parameter, and the formatters will
invoke them accordingly.
800