2017-04-05 67 views
0

我试图在调试时检查List<T>的值,但是如果T覆盖ToString(),我得到“儿童无法评估”错误。我使用VS2017和调试Unity3D 5.6.0f项目检查覆盖时,“孩子无法评估”ToString()

示例代码:

void Start() { 
    //if Sphere.ToString() is commented out, the contents are displayed correctly 
    var obstacles = parser.ParseFile(Variables.Paths.DefaultScene).ToList(); 
} 
//... 
[System.Diagnostics.DebuggerDisplay("{ToString()}")]//with or without this attribute 
public class Sphere 
{ 
    public Vector3 Center { get; set; } 
    public float Radius { get; set; } 

    public override string ToString() 
    { 
     return "[r: " + Radius + " c: " + Center + "]"; 
    } 
} 

是否T被类或结构似乎任何没有效果,DebuggerDisplay属性同样不具有任何影响 - 列表不能无论如何都要接受检查然而,

var tmp = new Sphere(); 

显示tmpToString()正确。我该如何解决这个问题?

回答

0

快速查看文档可以发现,您应该使用自定义ToString()DebuggerDisplay属性。不是都。特别是如果DebuggerDisplay只是一个{ToString()}调用。

这里有提到这个问题的文档段:

如果一个类有一个重写的ToString()方法时,调试器使用 覆盖方法,而不是默认{}。因此,如果 已覆盖ToString()方法,则调试器将使用 覆盖的方法而不是默认的{},并且您不需要使用DebuggerDisplay 。如果使用两者,则DebuggerDisplay 属性优先于重写的ToString()方法。

来源:Using the DebuggerDisplay Attribute

+0

作为的问题,有无提到'DebuggerDisplay'没有任何效果都没有。试图检查清单时总会产生“儿童无法评估”。 – wondra

+0

你有没有尝试删除ToString()并在头上添加这个? '[System.Diagnostics.DebuggerDisplay(“[r:{Radius} c:{Center}]”)]' –