Implementing a Generic Object State Dumper

Environment: .NET 1.0.3705

Every .NET class inherits the ToString method from the root Object class. This method, ToString, has several uses. One of its main uses is to dump the state of the class objects to facilitate debugging process. For example, in the following class, MyClass overrides ToString to return the state of the MyClass objects.

using System;

class MyClass
{
  public MyClass(){}


  public override string ToString()
  {
    return "iMyState1 = " + iMyState1.ToString() + "n" +
           "strMyState = " + strMyState2 + "n";
  }

  private int iMyState1;
  private string strMyState2;
}

The problem with the above approach is that it is a manual process; whenever MyClass is changed by adding or deleting attributes, it is necessary to update the ToString to dump correct and complete state. For example, if a new state variable strMyState3 is added to the MyClass, ToString must be updated so that it returns the strMyState3 value, also. Similarly, if iMyState1 is deleted from the class, ToString must be updated to remove references to iMyState1 from the function to compile the code.

The above process is manual and thus error prone. It is possible to implement a generic mechanism using .NET reflection to automate this process. This relieves the developer from updating the ToString frequently and instead concentrate on implementing the functionality.

This article includes the source code of the Dbg class that implements the two functions DisplayObject and DisplayClass, which facilitate this process. The Dbg.DisplayObject is used to dump the state of an instance object and Dbg.DisplayClass is used to dump the static attributes of a class.

Let’s rewrite MyClass using Dbg class.

using System;
using MyDbg;    // Class Dbg is defined in MyDbg namespace

class MyClass
{
  public MyClass(){}


  public override string ToString()
  {
    return Dbg.DisplayObject(this);
  }

  private int iMyState1;
  private string strMysState2;

}

Now, if attributes are added or deleted from MyClass, it is automatically taken care and no changes are needed to the ToString method. Not only this, the resulting code is much more readable. The readability effect can be seen prominently when the class state contains several variables.

Mostly, you will need to use Dbg.DisplayObject, which dumps both instance as well as static attributes of a class. However, sometimes you will need to see static attributes of a class without an instantiating object of that class. In such situations, Dbg.DisplayClass is indispensable.

Note that the use of Dbg.DisplayObject and Dbg.DisplayClass is not limited to the ToString method. These can be used to dump the state of a object or class at any time and place. These can even be used to dump the state of objects and classes that you have not implemented and do not have access to the source.

The download of this article contain two files. The first file is Dbg.cs, which implements the Dbg class. The second file is test.cs, which shows the usage of Dbg.cs. To compile it, use the csc Dbg.cs test.cs command. It will create the test.exe application.

The easiest way to use Dbg in an application is to add the Dbg.cs file to the project and import MyDbg namespace, as done in the examples given.

Downloads


Download source – 2 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read