Debug into .Net Framework Source Code

I was writing some WPF testing code today. It was extremely simple code which just programmatically creates a Rectangle and adds it to a canvas. The code this something like this:

    Rectangle rectangle = new Rectangle

    {

        Fill = new SolidColorBrush(Colors.Blue),

        Stroke = new SolidColorBrush(Colors.Blue),

        Width = 150,

        Height = 120

    };

    rectangle.SetValue(Canvas.LeftProperty, 100);

    rectangle.SetValue(Canvas.TopProperty, 100);

    canvas.Children.Add(rectangle);

However, I got an exception on rectangle.SetValue(Canvas.LeftProperty, 100), saying "'100' is not a valid value for property 'Left'."

It didn’t take me long to release that Canvas.LeftProperty actually requires a double instead of an integer, but I was surprised that it couldn’t handle an integer. Normally, in a .NET program, we wouldn’t have to worry about the implicit cast from integer to double, right? So, I was just curious and want to find more about how DependencyObject.SetValue is implemented and wanted to have a look of the .NET framework code.

Normally, when I want to do this, I will just use .NET Reflector, but I just heard from my friends that I can debug into .NET framework code. So, why not give it a go?

So, followed ShawnBurke's great post Configuring Visual Studio to Debug .NET Framework Source Code, I got my Visual Studio set up and ready to go.

Now, fire up my program, set a break point, and in the CallStack panel, there are list of items are greyed out which are the .NET Framework code as Visual Studio doesn’t have the debug symbols. So, just right click, and "Load Symbols", Visual Studio will load them from the debug symbol server you have specified (ShawnBurke’s post above explains how to do it).

LoadSymbols

When the debug symbols loaded (it may take a few minutes. PresentationFramework.pdb has 12Mb and PresentationCore.pdb has 16Mb), you will know as greyed out items on CallStack turn black.

Hit F11, now, you can see this:

DebugInto

Cool, isn’t it? We are now in .NET framework source code! Writing a .NET program could be as cool as writing in java! (just kidding)

After I played around a bit and dug through some .NET framework code, I found it pretty much just check the value passed in is an instance of the type DependencyProperty defined of. The code is something like this:

    if (!propertyType.IsInstanceOfType(value))

    {

        return false;

    }

If it returns false, an exception will be thrown from SetValueCommon.I guess WPF could have done better to handle an integer as a double, after all, if we want to set a location programmatically, normally we would just use an integer, right? Anyway, it is not really my point for this post. I really just want to show how to debug into .NET framework code here.

Pretty cool, isn’t it? So, what is not so cool?

Well, did find a couple of things not as cool as I expected.

  • You have to load debug symbols every time you run the program. It will load from your local cache once it is downloaded from the server, so it won’t take long. But you do have to right click the CallStack, and "Load Symbols" every time.

  • In .Net Framework code, variable values will not be shown properly. You will have "Cannot evaluate expression because the code of the current method is optimized."

DebugValue

This article is part of the GWB Archives. Original Author: Changhong Fu

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.