Perform Selective Serialization with the Memento Pattern

Problem

You want to serialize only part of an object or an object that inherits from another nonserializable object.

Solution

Create a dedicated, serializable object to hold the information that must be persisted.

Discussion

The memento pattern allows you to handle object serialization in a more flexible manner. Some of the reasons you might use the memento pattern include the following:

  • You want to serialize an object that wouldn’t otherwise be serializable (perhaps because it derives from a nonserializable object).

  • You want to serialize a portion of an object. The NonSerialized attribute can also help in this case, but you can’t use it to prevent the serialization of the information in a class ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook 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.