为了愉快地显示 aDictionary
的内容,我写了这个:
[assembly: DebuggerDisplay("{Key,nq} -> {Value,nq}", Target = typeof(KeyValuePair<,>))]
namespace test {
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class Thing {
private readonly int _num;
public string DebuggerDisplay => $"DBG: {_num}";
public Thing(int num) => _num = num;
}
public class Program {
static void Main(string[] args) {
var map = new Dictionary<string,Thing> {
["Foo"] = new Thing(1),
["Bar"] = new Thing(2),
};
}
}
}
我本来希望在调试器中看到这个:
Foo -> DBG: 1
Bar -> DBG: 2
但相反,我看到了这个:
Foo -> {test.Thing}
Bar -> {test.Thing}
值得注意的是,如果我扩展KeyValuePair
s,我确实看到:
Name | Value
--------+-------
Key | "Foo"
Value | DBG: 1
所以DebuggerDisplay
确实有效。
那么问题是如何在字典内容的主监视列表中显示复合类型的内容?