我知道属性非常有用。有一些预定义的属性,例如[Browsable(false)],它允许您在财产选项卡中隐藏财产。下面是一个解释属性的好问题:.NET中的属性是什么?
您在项目中实际使用的预定义属性(及其名称空间)是什么?
我知道属性非常有用。有一些预定义的属性,例如[Browsable(false)],它允许您在财产选项卡中隐藏财产。下面是一个解释属性的好问题:.NET中的属性是什么?
您在项目中实际使用的预定义属性(及其名称空间)是什么?
当前回答
我认为这里必须提到以下属性也非常重要:
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;
}
其他回答
[TypeConverter(typeof(ExpandableObjectConverter))]
告诉设计器展开财产,这些属性是(控件的)类
[Obfuscation]
指示混淆工具对程序集、类型或成员执行指定的操作。(尽管通常使用程序集级别[Assembly:ObfuscateAssemblyAttribute(true)]
如果我要进行代码覆盖率爬网,我认为这两个将是最好的:
[Serializable]
[WebMethod]
值得一提的是,这里列出了所有.NET属性。有几百个。
我不知道其他人,但我有一些严肃的RTF要做!
[XmlIgnore]
因为这允许您忽略(在任何xml序列化中)“父”对象,否则在保存时会导致异常。
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)),并且包装的方法发生错误,那么调用堆栈不会显示正在调用引发错误的方法的方法。