9.2. Serialization

If you've been working with objects—on any platform—you've likely already been exposed to the concept of serialization. If you're moving types between systems or you're just persisting a representation of a type, you're likely to be leveraging some form of serialization. Many of the .NET types, in fact, implement the ISerializable interface that allows them to be used in combination with the platform's serialization classes to extract (serialize) or reconstitute (deserialize) the state of your objects from a stream.

Although serialization can be a fairly in-depth topic, the goal here is to focus specifically on how generic types participate in the existing .NET serialization scheme. You need to understand how a generic type is serialized, and you need to consider how generics might be applied to improve your general interactions with the serialization API. The sections that follow address both of these topics.

9.2.1. Serialization Basics

First, let's start by looking at how basic serialization will work with your generic types. You need to begin by introducing a type that can be successfully serialized. In this case, let's create a SampleCollection<T> class that extends this existing list type. The code for this class is as follows:

[VB code] <Serializable()> _ Public Class SampleCollection(Of T) Inherits List(Of T) Private _intData As Int32 Private _stringData As String Public Sub New(ByVal intData As Int32, ByVal stringData As String) Me._intData = intData ...

Get Professional .NET 2.0 Generics 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.