Debugging Tips - Part One

A very cool attribute to save some debugging time is DebuggerTypeProxy (Namespace: System.Diagnostics). Using this attribute, you can write your own logic to interpret the object at debug time. Thus, if you have a list of class objects, then you can project on certain properties and force the debugger to show the value of this proxy class. This attribute is applicable to Structures, Classes, and Assemblies.

Below is the example where I have created a dummy model class. Using DebuggerTypeProxy attribute on this class, I am forcing the debugger to display the value of a property of the proxy class, which is DebuggerProxyForTestDebuggerProxy class. 

  1. [DebuggerTypeProxy(typeof(DebuggerProxyForTestDebuggerProxy))]  
  2.     public class TestDebuggerProxy  
  3.     {  
  4.         public int DeptID;  
  5.         public string DeptName;  
  6.         public List<string> EmpNames = new List<string>();   
  7.     }  

Proxy class

A proxy class must have a constructor that accepts an argument of the class type to which attribute is applied.

  1. public class DebuggerProxyForTestDebuggerProxy  
  2.     {  
  3.         TestDebuggerProxy instance;  
  4.         public DebuggerProxyForTestDebuggerProxy(TestDebuggerProxy obj)  
  5.         {  
  6.             instance = obj;  
  7.         }  
  8.         public string DeptInformation  
  9.         {  
  10.             get  
  11.             {  
  12.                 return $"ID:{instance.DeptID} | DeptName:{instance.DeptName} |  
  13.                 DeptEmps: {string.Join(",",instance.EmpNames)}";  
  14.             }  
  15.         }  
  16.     }  
Proxy class contains the private variable of the model class (TestDebuggerProxy), constructor with a parameter of type TestDebuggerProxy (which sets the private member of the class), and it has also a member called DeptInformation that joins the properties of the private member.

Now, let's create an object of type TestDebuggerProxy in the main method.

  1. static void Main(string[] args)  
  2.         {  
  3.    
  4.             TestDebuggerProxy obj = new TestDebuggerProxy() {   
  5.                  DeptID=1,  
  6.                  DeptName = "Dept 1",  
  7.                  EmpNames = new List<string> {"emp1","emp2","emp3" }  
  8.             };  
  9.             return;  
  10.         }  

Now, run the application in debug mode and hover your mouse over the object "obj". You should see something like below.

 

Note that how debugger is displaying the value of the object. It is actually showing the property of the proxy class and not the actual class. You can view the actual properties of the class by expanding the Raw View.

This is a really simple implementation of how it works but it can be very useful in legacy applications where you have complex objects and you need to check certain properties only while debugging the issue(s).

If you have any idea(s) to make it better, feel free to leave your suggestions/comments/questions.

Up Next
    Ebook Download
    View all
    Learn
    View all