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

Time:04-27

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 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()].

  • Related