Serializing Objects for Transmission through a Socket


The desktop version of the .NET Framework allows most types of objects to be serialized into an array of bytes, which can then be sent through a socket. For complex objects, developers implement the ISerializable interface, with code to serialize and deserialize their object data.

The .NET Compact Framework does not support this functionality. The only class that can serialize itself automatically is the DataSet class, which is discussed in detail in Chapter 6, "ADO.NET on the .NET Compact Framework." Normally, the DataSet class is used as an in-memory relational database. It is ideal for caching small amounts of data from a remote server while preserving the relational structure of the data. The DataSet can store every kind of fundamental data type available on the .NET Compact Framework, as outlined in Chapter 2, "Introducing the .NET Compact Framework." Additionally, the DataSet can serialize its contents to an XML string and repopulate itself from an XML string by using only a few lines of code. Chapter 8, "XML and the DataSet," is entirely devoted to explaining the DataSet 's support of XML.

To serialize and deserialize objects other than the DataSet with the .NET Compact Framework, developers must write their own serializing and deserializing code. We provide a simple class for serializing and deserializing primitive types in the next section. Also, Chapter 13, "Exploring .NET Reflection," provides a general-purpose class that can serialize and deserialize arbitrary object types.

Serializing Packets: Sample Application

We present here a sample application called ManagedChatSerialize. It is available in the folder SampleApplications\Chapter5\ManagedChatSerialize_CSharp and SampleApplications\ Chapter5\ManagedChatSerialize_VB . The central class in ManagedChatSerialize is the ChatData packet. This packet is implemented as a separate class, and it has the following properties:

Sender A string holding the name of the person who sent the packet

ChatText The chat text message being sent

The program works by creating ChatData packets, populating them with data, and then extracting the state of the class by calling ChatData.ToString() . The program sends the string representation of the class through a socket. At the other side, the chat program populates a ChatData packet by calling ChatData.FromString() . Then the remote chat program accesses the chat data through the properties provided by the ChatData class.

The ChatData class uses the SimpleSerializer class to serialize its internal state to and from strings. The SimpleSerializer can serialize and deserialize objects to and from a Stream . Developers can use this code as a foundation for enabling their own objects to serialize and deserialize themselves on the .NET Compact Framework. A more advanced serializer that can serialize arbitrary data types by using reflection is presented in Chapter 13, "Exploring .NET Reflection."

You'll notice that there is only one instance of the ChatData packet ever created in the program. This is an optimization that is very important on the .NET Compact Framework. Creating and destroying objects is very expensive. Thus, we provide a method called ChatData.Clear to reset the packet to the same state as if it were just created, with no internal information. That makes it easy to retain the same instance of the ChatData class while creating the illusion of creating a new instance of the class whenever we need one.



Microsoft.NET Compact Framework Kick Start
Microsoft .NET Compact Framework Kick Start
ISBN: 0672325705
EAN: 2147483647
Year: 2003
Pages: 206

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net