8.3. Serialization

Reading and writing primitive data types to and from a stream is one thing, but what about objects? Converting an object to a stream is called serialization, and it is used for two primary purposes. The first is to persist an object to storage (such as a file) with the intent of recreating it later. The second is to send an object by value (marshal by value) to another computer or application domain. Serialization is necessary because an object is valid only within the application domain in which it was created. If the architecture demands that objects be shared or used in a distributed environment, then serialization is necessary. Serialization plays a major role in .NET remoting and, fortunately, the framework does most of the work.

When an object is serialized, the name of the assembly (this is the type boundary, after all), the class, and the member data are added to the stream. During this process, any referenced objects are serialized, too (if they are eligible). This is often called an object graph. Circular references are not a problem in the case of object graphs because the CLR keeps track of each serialized object to ensure that the object is not serialized more than once.

This chapter discusses two types of serialization:

  • Binary serialization

  • XML and SOAP serialization

Binary serialization maintains type integrity, which means that every aspect of the object is preserved, including private member data. Maintaining type integrity is useful in situations ...

Get Object-Oriented Programming with Visual Basic .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.