How to make a custom type serializable?

Serialization is the process by which an object's state is transformed into a set of bytes (depending on the serialization type used, it could be XML, binary, JSON), which can then be saved in a stream (think MemoryStream or FileStream) or transmitted via WCF or Web API. Making a custom type serializable means that you can apply serialization to custom types by adding the System.SerializableAttribute. Examples of custom types are as follows:

  • Classes and generic classes
  • Structs
  • Enums

A real-world example of serialization could be to create a recovery mechanism for a specific object. Think of a workflow scenario. At some point in time, the state of the workflow needs to be persisted. You can serialize ...

Get C# 7 and .NET Core 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.