Automatic Serialization
.NET implements the automatic serialization of objects by means of reflection , a simple and elegant technique that uses metadata exposed by every .NET component. Reflection is discussed in detail in Appendix C. .NET can capture the value of every one of an object’s fields and serialize it to memory, to a file, or to a network connection. .NET also supports automatic deserialization: .NET can create a new object, read its persisted field values, and, using reflection, set the values of its fields. Because reflection can access private fields, including base-class fields, .NET can take a complete snapshot of the state of an object during serialization and perfectly reconstruct that state during deserialization. Another advantage of reflection-based serialization is that the code used by .NET is completely general-purpose—the state of every .NET type can be read or set using reflection.
.NET serializes the object state into a stream. A stream is a logical sequence of bytes, independent of any particular medium (file, memory, communication port, or other resource). This extra level of indirection means that you can use the same serialization infrastructure with any medium, simply by selecting an appropriate stream type. The various stream types provided by .NET all derive from the abstract class Stream
, defined in the System.IO
namespace. Although you need a Stream
instance to serialize and deserialize an object, there is usually no need to interact explicitly ...
Get Programming .NET Components, 2nd Edition 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.