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

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


当前回答

我总是使用属性,[Serializable]、[WebMethod]、[DefaultValue]、[Description(此处为“Description”)]。

但除此之外,c#中还有一个全局属性。

[assembly: System.CLSCompliant(true)]
[assembly: AssemblyCulture("")]
[assembly: AssemblyDescription("")]

其他回答

当您在调试期间将鼠标悬停在Type的实例上时,[DuggerDisplay]对于快速查看该类型的自定义输出非常有用。例子:

[DebuggerDisplay("FirstName={FirstName}, LastName={LastName}")]
class Customer
{
    public string FirstName;
    public string LastName;
}

这是它在调试器中的外观:

此外,值得一提的是,设置了CacheDuration属性的[WebMethod]属性可以避免不必要地执行web服务方法。

[Serializable]始终用于将对象序列化到外部数据源(如xml)或从远程服务器序列化对象。在这里了解更多信息。

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

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

更多信息

[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() { }
}

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