7.11.08

Displaying data in Visual Studio debugger variable windows

How can you display details of a custom type in debugger windows without expanding type tree?

Let’s consider following sample:

private static void Main()
{
var Js = new[]{
new IDableText("John"),
new IDableText("Jack"),
new IDableText("Joe"),
new IDableText("Jackson")
};

foreach (var j in Js)
{
// ...
}
}

class IDableText
{
public IDableText(string name)
{
this.Text = name;
this.CreationTime = DateTime.Now;
this.Id = Guid.NewGuid();
}

public DateTime CreationTime { get; private set; }
public string Text { get; private set; }
public Guid Id { get; private set; }
}

This code will produce all too known debug variable window:

image


But, if you decorate class IDableText with a built-in attribute like this…

[DebuggerDisplay("Text={Text}  CreationTime={CreationTime}  Id={Id}")]


…you’ll get more readable result.
image
Nice!

smile_shades

1 comment:

Anonymous said...

Navrati na grupu sutra, MSSQL2008 Community Launch je. Pozdrav, Vatroslav