查看对象属性的DebuggerDisplay

时间:2015-06-24 14:13:50

标签: c# debugging

说我有以下两个类:

[DebuggerDisplay("Test={Test}")]
class Class1
{
    public string Test;
}

[DebuggerDisplay("obj={obj}")]
class Class2
{
    public Class1 obj;
}

class Program
{
    public static void Main(string[] args)
    {
        var c1 = new Class1() { Test = "test" };
        var c2 = new Class2() { obj = c1 };
    }
}

是否可以启用Class2的调试器显示以显示Class1的调试器显示?即,我希望将鼠标悬停在c2上以显示obj="test"

1 个答案:

答案 0 :(得分:1)

你可以用这种风格写作你的目标:

[DebuggerDisplay("{ToString()}")]
class Class1
{
    public string Test;
    public override string ToString()
    {
        return "Test=" + Test;
    }
}

[DebuggerDisplay("{ToString()}")]
class Class2
{
    public Class1 obj;
    public override string ToString()
    {
        return "obj=" + obj;
    }
}

internal class Program
{
    public static void Main(string[] args)
    {
        var c1 = new Class1() {Test = "test"};
        var c2 = new Class2() {obj = c1};
    }
}