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

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


当前回答

在霍夫斯塔德精神中,[Attribute]属性非常有用,因为它是您创建自己属性的方式。我使用了属性而不是接口来实现插件系统,为Enum添加描述,模拟多重调度和其他技巧。

其他回答

我最近一直在使用[DataObjectMethod]。它描述了方法,因此您可以将类与ObjectDataSource(或其他控件)一起使用。

[DataObjectMethod(DataObjectMethodType.Select)] 
[DataObjectMethod(DataObjectMethodType.Delete)] 
[DataObjectMethod(DataObjectMethodType.Update)] 
[DataObjectMethod(DataObjectMethodType.Insert)] 

更多信息

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

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

这是一篇关于有趣属性InternalsVisibleTo的文章。它基本上模仿了C++朋友访问功能。它对于单元测试非常方便。

[System.Security.Permissions.PermissionSetAttribute]允许使用声明性安全性将PermissionSet的安全操作应用于代码。

// usage:
public class FullConditionUITypeEditor : UITypeEditor
{
    // The immediate caller is required to have been granted the FullTrust permission.
    [PermissionSetAttribute(SecurityAction.LinkDemand, Name = "FullTrust")]
    public FullConditionUITypeEditor() { }
}

我建议使用nUnit库中的[TestFixture]和[Test]。

代码中的单元测试为重构和编码文档提供了安全性。