Home > Articles > Programming > C#

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

C# Object Serialization in Action

Listing 1 illustrates a simple C# class called WMI1 from one of my previous articles, "IT Management Using C# with WMI." This class uses the WMI infrastructure to do two simple tasks:

  • Retrieve the set of network interfaces on the local machine.
  • Retrieve the local IP routing table.

One important thing to note about Listing 1: To make a class serializable, the absolute minimum required is the addition of the keyword [Serializable] at the beginning of the class. (Some additional code is needed, too, but I’ll get to that in the next section.)

Listing 1 A C# WMI class.

[Serializable]
public class WMI1
{
    public string ifDetails;
    public string ipRouteDetails;
    
    public WMI1() {
       ifDetails = getNetworkInterfaceDetails();
       ipRouteDetails = getIPRouteTable();
    }
    
    public string getNetworkInterfaceDetails()
    public string getIPRouteTable()
}

Don’t worry about the details; this class does just two simple things:

  • Retrieve the network interfaces on the executing host via the method getNetworkInterfaceDetails().
  • Retrieve the IP routing table on the executing host via the method getIPRouteTable().

These two items of data are stored in two public String data members: ifDetails and ipRouteDetails. Objects of the class containing these two String data members will be serialized as shown in Listing 2.

Listing 2 The serialization code.

BinaryFormatter binaryFormat = new BinaryFormatter();
Stream fStream = new FileStream("WmiData.dat",
   FileMode.Create, FileAccess.Write, FileShare.None);
binaryFormat.Serialize(fStream, netIfs);
fStream.Close();
Console.WriteLine("-> Saved netIfs in binary format.");
// Reconstruct the netIfs from the binary file
fStream = File.OpenRead("WmiData.dat");
WMI1 netIfs2 =
   (WMI1)binaryFormat.Deserialize(fStream);
Console.WriteLine("Deserialized interface details : {0}", netIfs2.ifDetails);
fStream.Close();

Notice the simplicity of the serialization process. You create a BinaryFormatter instance and a Stream instance, and then you can serialize the object. Once you’ve done this, the serialized object resides on your local disk and therefore can outlive the program that created it.

The second part of Listing 2 illustrates the binary de-serialization process. I create a whole new instance (called netIfs2) of the class WMI1. This is just to prove that we’re creating a new object from the serialized object data. I de-serialize the old data into the new object, and then I display the data in a call to Console.WriteLine().

When the object is serialized, the .NET Common Language Runtime creates what’s called an object graph. This is a description of the object class hierarchy and dependencies, which is needed to re-create the serialized object.

Now let’s look at the ways in which you can serialize objects.

  • + Share This
  • 🔖 Save To Your Account