Telerik blogs

Background

There are a variety of reasons why source code goes “missing”.  Poor utilization of source code control, server crash, third party software that didn’t provide the source control.  Of course there are more, but in the end, the result is the same.  You depend on a library that you don’t have the source for. 

This isn’t an issue until, well to be circular, until there’s an issue.  Something isn’t working as expected, or you need to add a new feature to your system, and you don’t know how the library works. One solution is to use JustDecompile to decompile the assembly, add the source code into your solution, and debug away.  While this works, it is a lot of friction just to see what is happening under the covers.

An Example Problem

For this example, I want to debug into the “LegacyCode” dependency in my project.  The code that depends on the legacy code is shown in Listing 1.  I want to see exactly what’s happening in the “DoSomething” method.

[Test]
public void ShouldDoSomething()
{
    var addend1 = 5;
    var addend2 = 10;
    var sut = new LegacyCode.MyClass();
    var result = sut.DoSomething(addend1, addend2);
    Assert.AreEqual(result, 15);
}

Listing 1 – The Legacy Dependency in Action

The Solution

With the Q2 2013 release of JustCode, this has become trivial to solve!  You can now decompile the code right in Visual Studio, set breakpoints, and leverage all of the debugging power of Visual Studio in just a few simple steps!

Modifying Visual Studio Settings

In order to enable debugging into decompiled code, you must first disable “Just My Code”. This setting is located under Tools –> Options –> Debugging. 

Three Way to Decompiling Dependencies

Using JustCode’s navigation feature to GoTo Definition of the DoSomething Method gives me a window that looks like Listing 2.

// 
// This file has been decompiled from ...\LegacyCode.dll 
// 
 
namespace LegacyCode
{
 // metadata token 02000002 
 public class MyClass : System.Object
   {
 // metadata token 06000001 
 public int DoSomething(int addend1, int addend2) {
 return addend1 + addend2;
	}
 // metadata token 06000002 
 public MyClass() {}
   }
}
 

Listing 2 – Raw Decompilation of the Dependency

When I try to set a breakpoint on re DoSomething method, I get the dialog shown in Figure 1.

image

Figure 1

Clicking on the Decompile button does what it says it will do  – it decompiles the assembly and allows breakpoints to be set in your dependency.

Before I go into the debugging aspect, I want to explore the other ways dependencies can be decompiled.

Decompiling Through the Assembly Browser

There’s a new window in JustCode, called the Assembly Browser.  You can access it from JustCode –> Windows –> Show Assembly Browser Window (shown in Figure 2).

SNAGHTML22b5f0c

Figure 2 – Opening the Assembly Browser Window

This Assembly Browser Window shows all of the referenced assemblies (see Figure 3). 

image

Figure 3 – Assembly Browser Window

To decompile an assembly (such as the LegacyCode.dll), right click the assembly and select “Decompile” (as in Figure 4).

image
Figure 4

Once the assembly is decompiled, it will show in the Assembly Browser windows with a green check mark, as in Figure 5.

image
Figure 5

To clear the decompiled information, either right click the assembly in the Assembly Browser and select “Clear Cache”, or to clear all information, click the X at the top left of the Window.

Decompiling Through Solution Explorer

You can also Decompile through Solution Explorer by right clicking on the dependency under “References” and selecting “Just Decompile for Debugging…”.  This decompiles the assembly and loads the Assembly Browser Window.

image
Figure 6 – Decompiling Through Solution Explorer

Debugging the Dependency

Now that I have decompiled my legacy dependency, I can navigate to the DoSomething method (click on the method in my test and hit F12 – GoTo Definition), and I am looking at the source code for the legacy dependency!  As you will see from Listing 3, the fully decompiled code is much cleaner and easier to read.  This is thanks to the excellent decompilation power of JustDecompile, which is fully leveraged by JustCode. 

namespace LegacyCode
{
 public class MyClass
  {
 public MyClass()
	{
	}
 public int DoSomething(int addend1, int addend2)
	{
 return addend1 + addend2;
	}
  }
}
Listing 3 – Fully Decompiled Dependency

Now I can set a break in my dependency and have the full power of Visual Studio debugging right at my fingertips!

 

image

Figure 7 – Interactive Debugging in Visual Studio

Summary

The bottom line is it will take you longer to read this post than it will to debug your decompiled dependency!  The wicked smart folks in engineering have made it crazy easy to work with those pesky dependencies that you have to face.  Download the latest copy of JustCode and try it for yourself.

 

JustCode download banner image


Japikse
About the Author

Phil Japikse

is an international speaker, a Microsoft MVP, ASPInsider, INETA Community Champion, MCSD, CSM/ CSP, and a passionate member of the developer community. Phil has been working with .Net since the first betas, developing software for over 20 years, and heavily involved in the agile community since 2005. Phil also hosts the Hallway Conversations podcast (www.hallwayconversations.com) and serves as the Lead Director for the Cincinnati .Net User’s Group (http://www.cinnug.org). You can follow Phil on twitter via www.twitter.com/skimedic, or read his personal blog at www.skimedic.com/blog.

 

Related Posts

Comments

Comments are disabled in preview mode.