1.4. Serializable Classes

Serializable classes are one of the more powerful features of the .NET Framework and, sadly, one of the most overlooked, largely because there is no native IDE support to generate them and, therefore, their existence is not obvious to developers.

Despite this, you do end up using them whenever you return a class type from an ASP.NET Web service method. Under the covers a serializable class is generated for you to handle the class being transmitted across the wire in a Simple Object Access Protocol (SOAP) envelope.

By their very definition, serializable classes are classes that can have their "content" dehydrated for later use. The .NET implementation allows serializable classes to be dehydrated to any form of Stream.

Many API's in the .NET Framework use the concept of streams, which represent an abstract view of data by presenting it in a contiguous sequence of bytes that you can read or write. Thus, they isolate you from any underlying data store, such as a file, serial port, or network connection. This is useful because it enables you to write to and read from a variety of "stores" using the same application programming interface (API).

The following code uses a StreamReader to write to a Memory and File stream demonstrating the ability to use a generic "writer" against different stores.

using (MemoryStream ms = new MemoryStream()) { StreamWriter writer = new StreamWriter(ms); writer.WriteLine("Hello World!"); } using (FileStream fs = new FileStream("output.txt", ...

Get Professional BizTalk® Server 2006 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.