c# - 何时使用 DebuggerDisplayAttribute

标签 c# .net visual-studio debugging debuggerdisplay

围绕 DebuggerDisplayAttribute 有哪些最佳实践? ?是什么指导您决定何时以及如何将属性应用于您的代码?例如..

  1. 您是否发现 DebuggerDisplayAttribute 对于某些类型的对象(即自定义数据结构)比其他对象更有用?
  2. 您是在公共(public)类型、内部类型还是两者上定义它?
  3. 您通常会将其添加到初始实现中,还是等待测试人员/用户提出请求?
  4. 什么时候定义 DebuggerDisplayAttribute 更好,什么时候覆盖 .ToString() 更有意义?
  5. 对于在属性中公开多少数据或要包含的计算量有限制,您是否有指导方针?
  6. 是否应用了任何继承规则,使其更有益于应用于基类?
  7. 在决定何时或如何使用它时,还有什么要考虑的吗?

最佳答案

这是主观的,我不愿意说有任何最佳实践,但是:

  1. Do you find DebuggerDisplayAttribute more useful on some types of objects (i.e. custom data structures) rather than others?

到目前为止,最常见的用途是表示业务实体的类型 - 我通常会显示 ID + 名称。还有将存储在应用程序集合中的任何类型。

除此之外,每当我发现自己经常在调试器中搜索属性时,我都会添加它。

2.Do you define it on public types, internal types, or both?

两者皆有。

3.Will you generally add it to the initial implementation, or wait for a tester/user to request it?

测试人员/用户永远不会看到它 - 它仅在调试时使用。

4.When is it better to define DebuggerDisplayAttribute and when does it make more sense to override .ToString()?

当您想要在运行时表示时覆盖 ToString(),无论是为了日志记录还是特定于应用程序的目的。如果您只需要调试,请使用 DebuggerDisplayAttribute。

5.Do you have guidelines on how much data you expose in the attribute, or limits on the amount of computation to include?

由于它不在运行时使用,唯一的限制是它应该足够快而不妨碍调试体验(尤其是在为集合的元素多次调用时)。

您无需像使用运行时日志记录那样担心暴露敏感数据(例如通过覆盖 .ToString),因为此类数据无论如何在调试器中都是可见的。

6.Do any inheritance rules apply that would make it more beneficial to apply on base classes?

不,将它应用到你需要它的类上。

7.Is there anything else to consider when deciding when or how to use it?

我想不出别的了。

关于c# - 何时使用 DebuggerDisplayAttribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5576113/

相关文章:

c# - asp.net c# 如何统计网站访问量

c# - VS 告诉我添加对一个看似无关的程序集的引用。如何找出原因?

visual-studio - Visual Studio 社区 2017 cl.exe

.net - Mono for ASP.NET 对于实际应用程序来说是否足够成熟?

android - _GLIBCXX_USE_C99 未定义,to_wstring 不可用

.net - 将FullTrust授予Visual Studio 2012和.Net 4.0的UNC共享

c# - 如果方法抛出异常,则更改文本 block 的前景

c# - C#9 不支持 JsonPropertyNameAttribute 记录?

.net - 安装 Windows 服务并进行恢复操作以重新启动

c# - DotNet Reflector - 为什么我无法反汇编 XmlHierarchicalEnumerable?