I have a class that might have many fields, but in order to identify it in the debugger I am only interested in its Name
property. Is there a way to display its name in the watches while debugging in Visual Studio other than modifying Autoexp.dat?
Is Visual Studio maybe looking for certain fields/functions to derive a string representation similar to how Eclipse does it in Java (it uses the result of the toString()
method as representation in the debugger)?
Good way to do that is to use DebuggerDisplay
attribute:
[DebuggerDisplay("Name = {Name}")]
class Data {
public string Name { get; set; }
}
Now your watch will display Name = valueOfName
for instance of given class. It's in general better than overriding ToString
(which also works), because overriding ToString
affects more than just debugging display, and you might not want to do that just for debugging purposes.
.NET uses the result of ToString
to display the variable value when it is an object
. So simply overriding ToString
would do:
public override string ToString() => this.Name;
If you have no control over the class, putting a watch on the Name
property will do too of course. (Use theInstance?.Name
to be safe for null references)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.