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


Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • With JRebel, developers get to see their code changes immediately, fine-tune their code with incremental changes, debug, explore and deploy their code with ease (both locally and remotely), and ultimately spend more time coding instead of waiting for the dreaded application redeploy to finish. Every time a developer tests a code change it takes minutes to build and deploy the application. JRebel keeps the app server running at all times, so testing is instantaneous and interactive.

  • Live Event Date: April 29, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Leveraging Flash storage to accelerate an Oracle Real Application Clusters (RAC) environment is one of today's hottest technology topics. Oracle databases require guaranteed levels of storage performance and high availability of data. Until recently, Oracle RAC could only use Flash storage for SmartFlash Cache, which addresses some performance improvements, but limits the benefits that can be gained from a shared Flash infrastructure. Enter …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds