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

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


当前回答

我的投票将是有条件的

[Conditional("DEBUG")]
public void DebugOnlyFunction()
{
    // your code here
}

您可以使用它添加具有高级调试功能的函数;与Debug.Write类似,它只在调试构建中调用,因此允许您将复杂的调试逻辑封装在程序的主流之外。

其他回答

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

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

我认为这里必须提到以下属性也非常重要:

STAThreadAttribute 

指示应用程序的COM线程模型是单线程单元(STA)。

例如,此属性用于Windows窗体应用程序:

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1());
    }
}

而且。。。

SuppressMessageAttribute

禁止报告特定的静态分析工具规则冲突,允许对单个代码工件进行多次禁止。

例如:

[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "isChecked")]
[SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "fileIdentifier")]
static void FileNode(string name, bool isChecked)
{
    string fileIdentifier = name;
    string fileName = name;
    string version = String.Empty;
}

在我的脑海中,这里是一个快速列表,大致按使用频率排序,列出了我在一个大型项目中实际使用的预定义属性(约50万个LoC):

标志,可串行化,WebMethod,COMVisible,TypeConverter,Conditional,ThreadStatic,Obsolete,InternalsVisibleTo,DebuggerStepThrough。

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

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

更多信息

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