Managed Extensions: Implementing Custom Serialization | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Oct 5, 2004
3 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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.

Here’s the syntax for the ISerializable::GetObjectData method:

void ISerializable::GetObjectData(SerializationInfo* info,
                                  StreamingContext context);

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.

Here’s a simple example of how to use this interface:

using namespace System::IO;
using namespace System::Runtime::Serialization;
using namespace System::Runtime::Serialization::Formatters::Binary;
...

[Serializable]
__gc class Programmer : public ISerializable
{
public:
  Programmer(String* firstName, String* lastName, Int32 age)
  {
    this->firstName = firstName;
    this->lastName = lastName;
    this->age = age;
  }

protected:
  Programmer(SerializationInfo *si, StreamingContext sc)
  {
    this->firstName = si->GetString(S"firstName");
    this->lastName  = si->GetString(S"lastName");
    // Note that this->Age is not read because it was never written
  }

protected:
  String* firstName;
  String* lastName;
  Int32 age;

public:
  __property String* get_FirstName() { return this->firstName; }
  __property String* get_LastName() { return this->lastName; }
  __property Int32 get_Age() { return this->age; }

public:
  void GetObjectData(SerializationInfo *si, StreamingContext sc)
  {
    si->AddValue(S"firstName", this->firstName);
    si->AddValue(S"lastName", this->lastName);
    // Note that this->Age is not being saved here
  }
};

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.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.