我知道属性非常有用。有一些预定义的属性,例如[Browsable(false)],它允许您在财产选项卡中隐藏财产。下面是一个解释属性的好问题:.NET中的属性是什么?

您在项目中实际使用的预定义属性(及其名称空间)是什么?


当前回答

我喜欢System.Diagnostics中的[DuggerStepThrough]。

它非常方便,可以避免使用那些单行do-nothing方法或财产(如果您被迫在早期的.Net中工作,而没有自动财产)。将属性放在一个短方法或属性的getter或setter上,即使在调试器中单击“stepinto”,您也会立即执行。

其他回答

// on configuration sections
[ConfigurationProperty] 

// in asp.net
[NotifyParentProperty(true)]

DesignerSerializationVisibilityAttribute非常有用。当您在控件或组件上放置运行时属性,并且不希望设计器对其进行序列化时,可以这样使用:

[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Foo Bar {
    get { return baz; }
    set { baz = value; }
}

[DeploymentItem(“myFile1.txt”)]DeploymentItem上的MSDN文档

如果您正在对文件进行测试或将该文件用作测试的输入,那么这非常有用。

我发现[DefaultValue]非常有用。

DebuggerHiddenAttribute,它允许避免单步执行不应调试的代码。

public static class CustomDebug
{
    [DebuggerHidden]
    public static void Assert(Boolean condition, Func<Exception> exceptionCreator) { ... }
}

...

// The following assert fails, and because of the attribute the exception is shown at this line
// Isn't affecting the stack trace
CustomDebug.Assert(false, () => new Exception()); 

此外,它还防止在堆栈跟踪中显示方法,这在使用只包装另一个方法的方法时非常有用:

[DebuggerHidden]
public Element GetElementAt(Vector2 position)
{
    return GetElementAt(position.X, position.Y);
}

public Element GetElementAt(Single x, Single y) { ... }

如果现在调用GetElementAt(新的Vector2(10,10)),并且包装的方法发生错误,那么调用堆栈不会显示正在调用引发错误的方法的方法。