DebuggerDisplay和DebuggerTypeProxy是两个属性,用来在VS调试的状态下,显示一些额外的信息,方便用户进行调试。
DebuggerDisplay 不单能够显示变量的值,还能调用变量的方法来计算结果显示:
class Program { static void Main(string[] args) { Company c = new Company(); Console.ReadLine(); } } [DebuggerDisplay("company name:{name}, employee count:{employeeNames.Count}")] class Company { public string name = "Company1"; List<string> employeeNames = new List<string>() { "Employee1", "Employee2", "Employee3" }; }
DebuggerTypeProxy 可以附加一些属性在VS调试窗口中,提供了更加灵活的方式。如果DebugView中的属性和类型本身的属性名字相同,那么类型的属性将会被覆盖,在VS调试器中看不见了。
class Program { static void Main(string[] args) { Company2 c2 = new Company2(); Debugger.Break(); Console.ReadLine(); } } [DebuggerTypeProxy(typeof(Company2DebugView))] class Company2 { internal class Company2DebugView { private Company2 m_Company2; //必须要有一个构造函数,包含目标类型 public Company2DebugView(Company2 company2) { m_Company2 = company2; } public string Employees { get { return String.Join(" | ", m_Company2.employeeNames.ToArray()); } } } private string name = "Company2"; private List<string> employeeNames = new List<string>() { "Employee1", "Employee2", "Employee3" }; }