【问题标题】:DebuggerDisplay ignored when showing collection of items显示项目集合时忽略 DebuggerDisplay
【发布时间】:2013-12-13 13:41:30
【问题描述】:

假设我有一个对象:

[DebuggerDisplay("Bar={bar}")]
public class Foo
{
    public String bar{get;set;}
}

当我有一个 bar 实例时,调试器会正确显示 Bar="value of bar" 但是当我有一本Foos 的字典时,字典显示:

{[key, namespace.Foo]}

当我展开 kvp 时,我得到了预期的调试器显示字符串。

当我在Foo 中覆盖ToString() 并拥有Foo 的字典时 字典显示:

{[key, Bar="value of bar"]}

根据文档,ToString() 仅在 DebuggerDisplay 属性未覆盖它时用于调试器显示...

除了单个实例之外,如何获取 debuggerDisplay 属性以覆盖枚举情况下的调试器字符串?

【问题讨论】:

  • 您必须创建自己的派生自 IDictionary<TKey, TValue> 的类型,并将其归因于 DebuggerDisplayAttribute

标签: c# visual-studio debugging visual-studio-2012


【解决方案1】:

这个问题的一个优雅的解决方案是应用DebuggerDisplay 归属于System.Collections.Generic.KeyValuePair<TKey,TValue> 通过AssemblyInfo.cs如下:

using System.Collections.Generic;
using System.Diagnostics;

[assembly: DebuggerDisplay("[Key={Key}, Value={Value}]", Target = typeof(KeyValuePair<,>))]

(Source on StackoverFlow)

【讨论】:

  • 这个解决方案对我不起作用;我只是得到 [Key=0, Value={namespace.Foo}]
猜你喜欢
  • 2011-04-05
  • 2020-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-03
  • 1970-01-01
相关资源
最近更新 更多