我知道属性非常有用。有一些预定义的属性,例如[Browsable(false)],它允许您在财产选项卡中隐藏财产。下面是一个解释属性的好问题:.NET中的属性是什么?
您在项目中实际使用的预定义属性(及其名称空间)是什么?
我知道属性非常有用。有一些预定义的属性,例如[Browsable(false)],它允许您在财产选项卡中隐藏财产。下面是一个解释属性的好问题:.NET中的属性是什么?
您在项目中实际使用的预定义属性(及其名称空间)是什么?
当前回答
[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() { }
}
其他回答
作为我喜欢的中间层开发人员
System.ComponentModel.EditorBrowsableAttribute允许我隐藏财产,以便UI开发人员不会被他们不需要看到的财产淹没。
System.ComponentModel.BindableAttribute有些东西不需要数据绑定。同样,减少了UI开发人员需要做的工作。
我也喜欢劳伦斯·约翰斯顿提到的默认值。
System.ComponentModel.BrowsableAttribute和Flags定期使用。
我使用System.STAThreadAttributeSystem.ThreadStaticAttribute当需要时。
顺便说一句这些对所有.Net框架开发人员来说都同样重要。
[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() { }
}
在我看来,过时是框架中最有用的属性之一。对不应再使用的代码发出警告的功能非常有用。我喜欢有一种方法告诉开发人员不应该再使用某些东西,也喜欢有一个方法来解释为什么,并指出更好的/新的做法。
Conditional属性对于调试使用也非常方便。它允许您在代码中添加用于调试的方法,这些方法在构建解决方案以供发布时不会被编译。
然后,我发现有很多特定于Web控件的属性很有用,但这些属性更具体,在我发现的服务器控件开发之外没有任何用处。
我认为这里必须提到以下属性也非常重要:
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;
}
当您在调试期间将鼠标悬停在Type的实例上时,[DuggerDisplay]对于快速查看该类型的自定义输出非常有用。例子:
[DebuggerDisplay("FirstName={FirstName}, LastName={LastName}")]
class Customer
{
public string FirstName;
public string LastName;
}
这是它在调试器中的外观:
此外,值得一提的是,设置了CacheDuration属性的[WebMethod]属性可以避免不必要地执行web服务方法。