What's the easiest way to create a managed visualiser in C#?

101 views Asked by At

I have a background in C++ and recently I started working in C#.

I have written following pieces of code (in Visual Studio):

var list_Loads = database.GetData<Load>().ToList();
var test_list = list_Loads.Where(o => (o.Name.Substring(0, 3) == "123")).ToList();

When I run the program and I move my mouse over both lists, first I get the count, which is very useful, but when I ask for the entries, this is what I get:

0 : namespace.Load
1 : namespace.Load
2 : namespace.Load
...

Not very useful, as you can imagine :-)

So my question: how can I show the Name attributes of those objects?

I thought: no problem. I have a background in native visualisers, so it should be rather easy to turn this into useful information, but then it comes:
In order to alter the way that those objects are represented, there is the first proposal to add a [DebuggerDisplay] "tag" to the definition of that class in source code.
However, as those classes are part of a framework I'm just referring to, I don't have access to the source code and hence I can't modify this.

Then I found another solution, which comes down to: "Write an entire C# project, debug, test and install it and it might work" (see documentation on "Custom visualisers of data" on the Microsoft website).
I almost choked in my coffee: writing an entire project, just for altering the view of an object??? (While, in C++, you just create a simple .natvis file, mention the classname and some configuration, launch .nvload and that's it.

Does anybody know a simple way to alter the appearance of C# object, without needing to pass through the whole burden of creating an entire C# project?

By the way, when I try to load a natvis file in Visual Studio immediate window, this is what I get:

.nvload "C:\Temp_Folder\test.natvis"
error CS1525: Invalid expression term '.'

What am I doing wrong?

Thanks in advance

1

There are 1 answers

2
AudioBubble On BEST ANSWER

OP (my emphasis):

In order to alter the way that those objects are represented, there is the first proposal to add a [DebuggerDisplay] "tag" to the definition of that class in source code. However, as those classes are part of a framework I'm just referring to, I don't have access to the source code and hence I can't modify this.

Does anybody know a simple way to alter the appearance of C# object, without needing to pass through the whole burden of creating an entire C# project?

If you just want to specify [DebuggerDisplay] on a type, you don't have to have access to the source code. You can make use of [assembly:DebuggerDisplay()] and control how a type appears in the debugger. The only downside is that [assembly:DebuggerDisplay()] naturally only affects the current assembly whose code your mouse is hovering over. If you wish to use the customised display in other assemblies that you own, then you must repeat the [assembly:DebuggerDisplay()] definition.

Here's an easy before-and-after example with DateTime. I picked DateTime because we generally don't have access to the source code and it has some interesting properties:

 var items = new List<DateTime>
 {
     DateTime.Now.AddDays(-2),
     DateTime.Now.AddDays(-1),
     DateTime.Now
 };

...which on my machine defaults to:

enter image description here

Maybe I'm fussy and I just want to see:

  • Day of the week and
  • Day of the year

...I can do that via:

using System.Diagnostics;

[assembly: DebuggerDisplay("{DayOfWeek} {DayOfYear}", Target = typeof(DateTime))]

...which results in:

enter image description here

Example:

namespace DebuggerDisplayTests
{
    public class DebuggerDisplayTests
    {
        public DebuggerDisplayTests()
        {
            var items = new List<DateTime>
            {
                DateTime.Now.AddDays(-2),
                DateTime.Now.AddDays(-1),
                DateTime.Now
            };
        }
    }
    .
    .
    .
}

Overrides

[assembly:DebuggerDisplay()] can also be used as a means to override pre-existing [DebuggerDisplay] on a 3-rd party type. Don't like what style they have chosen? Is the type showing far too much information? Change it with [assembly:DebuggerDisplay()].