有两种方法可以增加调试信息的有用性,而不是{MyNamespace.MyProject.MyClass}在调试器中查看.
这些是使用[DebuggerDisplayAttribute][1]和ToString()方法.
using System.Diagnostics;
...
[DebuggerDisplay("Name = {Name}")]
public class Person
{
public string Name;
}
Run Code Online (Sandbox Code Playgroud)
要么
public class Person
{
public string Name;
public override string ToString()
{
return string.Format("Name = {0}", Name);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有理由更喜欢一个到另一个?有什么理由不这两个都做?这纯粹是个人偏好吗?
考虑以下课程:
[DebuggerDisplay("{GetType().Name,nq}: FileName = {FileName,nq}")]
public class FileWrapper
{
public string FileName { get; set; }
public bool IsTempFile { get; set; }
public string TempFileName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我想基于IsTempFileName属性添加调试器显示.我想, TempFileName = {TempFileName,nq}在实例是临时文件时添加字符串.我将如何实现这一目标?
我有几个定义DebuggerDisplay属性的类.我想知道是否有一种方法可以根据另一个定义一个DebuggerDisplay属性.如果我有以下课程:
[DebuggerDisplay ("Text = {Text}")]
class A
{
public string Text {get;set;}
}
[DebuggerDisplay ("Property = {Property}")]
class B
{
public A Property {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
我希望看到A类的实例,因为它是在A类DebuggerDisplay属性上定义的.而不是我在查看B类对象时将A类ToString()方法放到调试器上.
当前状态
有两个班:
[DebuggerDisplay(@"One = {One}, two = {Two}")]
public class A
{
public int One { get; set; }
public B Two { get; set; }
}
[DebuggerDisplay(@"Three = {Three}")]
public class B
{
public int Three { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
使用它们:
var a = new A {One = 5, Two = new B {Three = 10}};
Run Code Online (Sandbox Code Playgroud)
内部调试器,也就是在显示工具提示值a是
一个= 5,两个= {DebuggerDisplayTest.B}
目标
我想要的是类似的东西
一个= 5,两个='三= 10'
我知道这可以通过重写ToString()类的方法来实现B.这只是感觉不对,因为我在我的应用程序中编写代码仅用于调试.
我也知道使用类似的字符串
[DebuggerDisplay(@"One = {One}, two = …Run Code Online (Sandbox Code Playgroud) Attribute [DebuggerDisplay](使用DebuggerDisplayAttribute)允许在VS 2010/2008的调试器中定义显示.通过修改AutoExp.cs/.dll,我甚至可以覆盖系统类型和第三方类型的显示,例如
[assembly: DebuggerDisplay (@"\{Name = {Name} FullName = {FullName}}", Target = typeof (Type))]
Run Code Online (Sandbox Code Playgroud)
在内部花括号中,我可以引用字段,属性和方法.是否可以引用扩展方法?
作为一个例子,我试图显示更短的类型名称,例如,$SCG.Dictionary而不是System.Collections.Generic.Dictionary.我将此添加到AutoExp.cs:
using DbgDisp;
[assembly: DebuggerDisplay (@"\{Name = {Name} ShortName = {ShortName()}}", Target = typeof (Type))]
namespace DbgDisp {
public static class Ext {
public static string ShortName (this Type t) { return string.Format ("[{0}]", t.Name); }
} // Ext
} // DbgDisp
Run Code Online (Sandbox Code Playgroud)
但调试器抱怨:当前上下文中不存在名称"ShortName".
我错过了什么,或者只是不可能在那里使用扩展方法?
我知道我可以覆盖ToString (),但这只对我自己的类型有帮助.
c# extension-methods visual-studio-2010 debuggervisualizer 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有哪些最佳实践?是什么指导您决定何时以及如何将属性应用于您的代码?例如..
DebuggerDisplayAttribute某些类型的对象(即自定义数据结构)而不是其他对象更有用?DebuggerDisplayAttribute以及何时覆盖更有意义.ToString()?如果我在 上指定(日期)格式[DebuggerDisplay],我会看到错误 CS0726:
错误 CS0726: ':d' 不是有效的格式说明符
例如这段代码:
[DebuggerDisplay("{From:d} - {To:d}")
public class DateRange
{
public DateTime From { get; set; }
public DateTime To { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
在 Visual Studio 中调试时显示:
我知道这个属性应该在C#中起作用,但在我的情况下却没有.我有一个懒惰的孩子班级.访问此属性可能会产生向服务器往返的副作用.因此,当我在调试器观察窗口中观看时,我不希望发生这种情况.
省略所有不相关的细节,源代码看起来非常普通:
[DebuggerDisplay("(Frozen) {m_children}")]
public IList<IEntityBase> Children
{
get
{
if (m_children == null)
{
m_children = FetchChildrenFromDB(this);
}
return m_children;
}
}
Run Code Online (Sandbox Code Playgroud)
然而,当我观察对象并this在观察窗口中展开时,我在显示中看不到(冻结),这意味着调试器只是忽略该属性.
如果图像链接仍然有效,则应在下面显示:http : //i28.tinypic.com/2zxo9s5.jpg DebuggerDisplay image snapshot http://i28.tinypic.com/2zxo9s5.jpg
根据Reflector的说法,该属性确实存在.我用的是VS2008.
有任何想法吗?
我正在编写一些代码来自动解析/评估DebuggerDisplay字符串以进行单元测试.我很好奇,,nq指令只对字符串有效吗?如果我写的话,我明白了
[DebuggerDisplay("{c,nq}")]
public class D { public C c = new C(); }
public class C { }
Run Code Online (Sandbox Code Playgroud)
然后a new D()将出现{C}在调试器中.,nq从显示字符串中删除具有相同的效果.只有当我将类型更改c为字符串时,才会这样
[DebuggerDisplay("{c,nq}")]
public class D { public string c = "foo"; }
Run Code Online (Sandbox Code Playgroud)
删除/保持,nq似乎有效(它分别产生"foo"和结果foo).所以,nq当你试图显示一个字符串字段/成员时才有意义吗?
.net c# visual-studio debuggerdisplay visual-studio-debugging
什么时候必须使用DebuggerDisplay属性?使用它有什么好处?
我正在寻找一个很好的具体示例,显然需要覆盖ToString()某些东西,但要使用[DebuggerDisplay(...)]自定义属性在调试器中显示其他内容?