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

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • With the average hard drive now averaging one terabyte in size, the fallout from the explosion of user-created data has become an overwhelming volume of potential evidence that law-enforcement and corporate investigators spend countless hours examining. Join Us and SANS' Rob Lee for our 45-minute webinar, A Triage and Collection Strategy for Time-Sensitive Investigations, will demonstrate how to: Identify the folders and files that often contain key insights Reduce the time spent sifting through content by …

Most Popular Programming Stories

More for Developers

RSS Feeds