Managed Extensions: Implementing Custom Serialization

Welcome to this week's installment of .NET Tips & Techniques! Each week, award-winning Architect and Lead Programmer Tom Archer demonstrates how to perform a practical .NET programming task using either C# or Managed C++ Extensions.

In an earlier installment of .NET Tips & Techniques, I illustrated how to serialize your __gc classes—as well as selected member variables—to and from disk. In this week's tip, I'll illustrate a technique called custom serialization, which will give you far greater control over serializing your object's contents than the simple Serializable and NonSerialized attributes covered in the earlier article.

The ISerializable Interface

You realize custom serialization by implementing the ISerializable interface. This interface contains only one method—GetObjectData—which the Formatter object automatically calls during serialization. This process gives you complete control over how each member is serialized—including the ability to perform any pre- or post-processing.

The basic functionality of this method is to populate the SerializationInfo object with the data needed to perform serialization, where the StreamingContext specifies the destination for the serialization. You typically won't need to manipulate the StreamingContext parameter because it has already been initialized for you. It's passed to you for the rare occasion when you need to know the ultimate destination of the object's data.

As you can see, a bit of work needs to be done—but it's not much and certainly isn't difficult. The first thing to note is the addition of a second (protected) constructor. This constructor is called automatically during serialization and enables you to have full control over which members are set and how. As you can see, you retrieve the value read from disk by calling the SerializationInfo::GetString method. Each supported type has a distinct method (such as GetString, GetInt32, GetInt64, and so forth).

The second thing to note is the GetObjectData method. Here, I'm simply calling the overloaded SerializationInfo::AddValue method for each member that I want written to disk. In this case, I'm intentionally omitting the Programmer::Age member because I don't care to serialize its value.

You'll also notice that the client code for serializing and de-serializing an object is exactly the same as in the earlier article. In other words, the addition of custom serialization has no impact on the client code.

Complete Control Over Serialization

Custom serialization gives you complete control over the serialization process while requiring you to implement only two methods—ISerializable::GetObjectData (for writing) and an additional constructor (for reading).

A word to the wise if you're mixing MFC and Managed Extensions: Trying to serialize MFC objects with .NET serialization is far more trouble than it's worth. It's better to serialize MFC objects by using the standard CArchive-based MFC serialization technique and use custom serialization only for .NET (or __gc) classes.

About the Author

Tom Archer - MSFT

I am a Program Manager and Content Strategist for the Microsoft MSDN Online team managing the Windows Vista and Visual C++ developer centers.
Before being employed at Microsoft, I was awarded MVP status for the Visual C++ product. A 20+ year veteran of programming with various languages - C++, C, Assembler, RPG III/400, PL/I, etc. - I've also written many technical books (Inside C#, Extending MFC Applications with the .NET Framework, Visual C++.NET Bible, etc.) and 100+ online articles.

Top White Papers and Webcasts

The 2014 State of DevOps Report — based on a survey of 9,200+ people in IT operations, software development and technology management roles in 110 countries — reveals:
Companies with high-performing IT organizations are twice as likely to exceed their profitability, market share and productivity goals.
IT performance improves with DevOps maturity, and strongly correlates with well-known DevOps practices.
Job satisfaction is the No. 1 predictor of performance against organizational …