2

特别是,在检查 Windows 窗体对象时,我希望查看我的代码中定义的成员,而不总是看到基类中框架定义的数百个成员。

在此处输入图像描述

4

1 回答 1

5

有多种使用框架属性的内置方式(尽管没有一种特别可扩展)。


1.使用[DebuggerDisplay]

假设您的课程如下所示:

public class MyForm : Form
{
    public int Foo { get; set; }
    public string Bar { get; set; }
}

你可以像这样添加[DebuggerDisplayAttribute]到类中:

[DebuggerDisplay("Foo = {Foo}, Bar = {Bar}")
public class MyForm : Form
{
    public int Foo { get; set; }
    public string Bar { get; set; }
}

现在只显示FooBar

优点:仅显示您希望的那些成员。

缺点:您必须明确指定要显示的成员,并对以后添加的每个属性重复该步骤。


2.使用[DebuggerTypeProxy]

您可以定义一个应该在调试器中显示的代理类,而不是您的真实类。

同样,假设您的班级看起来像这样

public class MyForm : Form
{
    public int Foo { get; set; }
    public string Bar { get; set; }
}

您现在可以继续定义一个代理类,该类仅公开您想要显示的那些成员并将其应用于[DebuggerTypeProxyAttribute]类,如下所示:

[DebuggerTypeProxy(typeof(MyFormDebugView))]
public class MyForm : Form
{
    public int Foo { get; set; }
    public string Bar { get; set; }
}

public class MyFormDebugView
{
    public int Foo { get; set; }
    public string Bar { get; set; }
}

现在您只会在调试器显示中看到FooBar,而不是继承的Form.

优点:只显示代理类中的成员。无需明确指定这些成员。

缺点:MyForm每当您向调试器中添加新属性等时,您都必须调整代理类型。

于 2018-08-29T15:14:30.493 回答