Joe*_*Joe 6 .net debugging debuggerdisplay
我有许多用DebuggerDisplayAttribute修饰的类.
我希望能够在单元测试中添加跟踪语句,以显示这些类的实例.
.NET Framework中是否存在一个方法,它将显示使用DebuggerDisplayAttribute格式化的对象(如果未定义DebuggerDisplayAttribute,则返回使用.ToString())?
编辑
为了澄清,我希望框架中可能有一些东西.我知道我可以从DebuggerDisplayAttribute获取Value属性,但是我需要使用DebuggerDisplayAttribute.Value表示的格式字符串来格式化我的实例.
如果我自己滚动,我会设想一个扩展方法,如下所示:
public string FormatDebugDisplay(this object value)
{
DebugDisplayAttribute attribute = ... get the attribute for value ...
if (attribute = null) return value.ToString();
string formatString = attribute.Value;
??? How do I format value using formatString ???
return SomeFormatMethod(formatString, value);
}
Run Code Online (Sandbox Code Playgroud)
这可能很好,但是 DebuggerDisplayAttribute 的格式字符串由调试器计算,其计算方式与您在“监视”窗口或“立即”窗口中键入的表达式相同。这就是为什么您可以在大括号内放置任意表达式,例如{FirstName + " " + LastName}.
因此,要在代码中评估这些,您需要将 Visual Studio 调试器嵌入到您的应用程序中。可能不会发生。(咧嘴笑)
最好的选择可能是采用 DebuggerDisplay 格式字符串中当前的所有格式化逻辑,并将其改为方法。然后您就可以从代码中调用该方法。您的 DebuggerDisplay 属性最终除了调用该方法之外什么也不做。
[DebuggerDisplay("{Inspect()}")]
public class MyClass {
public string Inspect() { ... }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
322 次 |
| 最近记录: |