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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • On-demand Event Event Date: July 22, 2014 In this WhatWorks analysis, John Pescatore examines a use case where end users had local administrative rights on their PCs and it had gotten out of hand for this Fortune 500 Energy and Utilities company. The compelling event that prompted the company to reexamine this situation was the migration to Windows 7. In Windows XP, a custom tool that allowed users one of three levels of administrative rights to their workstations would need to be replaced during the Windows …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds