Created
February 10, 2022 12:49
-
-
Save linkdotnet/0591e890442f62eff1253d8225b401b7 to your computer and use it in GitHub Desktop.
DebuggerDisplay and DebuggerTypeProxy in Action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See https://aka.ms/new-console-template for more information | |
using System.Diagnostics; | |
using System.Threading.Channels; | |
var father = new Person | |
{ | |
Birthday = new DateTime(1970, 1, 1), | |
Name = "Father McFatherson", | |
}; | |
var mother = new Person | |
{ | |
Birthday = new DateTime(1971, 1, 1), | |
Name = "Mother McMotherson", | |
}; | |
var daughter = new Person | |
{ | |
Birthday = new DateTime(1991, 1, 1), | |
Name = "Daughter McDaughterson", | |
Father = father, | |
Mother = mother, | |
}; | |
// Set the breakpoint here and inspect the "daughter" variable | |
Console.Write(daughter); | |
[DebuggerTypeProxy(typeof(PersonDebugView))] | |
[DebuggerDisplay("{Name}'s Father: {Father.Name}")] | |
public class Person | |
{ | |
public DateTime Birthday { get; set; } | |
public string Name { get; set; } | |
public Person Father { get; set; } | |
public Person Mother { get; set; } | |
} | |
public class PersonDebugView | |
{ | |
private string hidden = "I am not visible"; | |
private Person _person; | |
public PersonDebugView(Person person) | |
{ | |
_person = person; | |
} | |
public string Parents => $"Father: {_person.Father.Name} / Mother: {_person.Mother.Name}"; | |
public int Age => (DateTime.Now.Year - _person.Birthday.Year); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment