我知道属性非常有用。有一些预定义的属性,例如[Browsable(false)],它允许您在财产选项卡中隐藏财产。下面是一个解释属性的好问题:.NET中的属性是什么?
您在项目中实际使用的预定义属性(及其名称空间)是什么?
我知道属性非常有用。有一些预定义的属性,例如[Browsable(false)],它允许您在财产选项卡中隐藏财产。下面是一个解释属性的好问题:.NET中的属性是什么?
您在项目中实际使用的预定义属性(及其名称空间)是什么?
当前回答
这是一篇关于有趣属性InternalsVisibleTo的文章。它基本上模仿了C++朋友访问功能。它对于单元测试非常方便。
其他回答
[TypeConverter(typeof(ExpandableObjectConverter))]
告诉设计器展开财产,这些属性是(控件的)类
[Obfuscation]
指示混淆工具对程序集、类型或成员执行指定的操作。(尽管通常使用程序集级别[Assembly:ObfuscateAssemblyAttribute(true)]
DesignerSerializationVisibilityAttribute非常有用。当您在控件或组件上放置运行时属性,并且不希望设计器对其进行序列化时,可以这样使用:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Foo Bar {
get { return baz; }
set { baz = value; }
}
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)),并且包装的方法发生错误,那么调用堆栈不会显示正在调用引发错误的方法的方法。
[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() { }
}
它的名称不好,在框架中也不受支持,不应该需要参数,但这个属性是不可变类的有用标记:
[ImmutableObject(true)]