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
OP (my emphasis):
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 pickedDateTime
because we generally don't have access to the source code and it has some interesting properties:...which on my machine defaults to:
Maybe I'm fussy and I just want to see:
...I can do that via:
...which results in:
Example:
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()]
.