属性是一种声明性标记,用于向运行时传达有关程序中各种元素(如类、方法、结构、枚举器、程序集等)行为的信息。您可以通过使用属性向程序添加声明性信息。声明性标记由放置在其所用于的元素上方的方括号([ ])表示。
以下是属性的语法:
'[attribute(positional_parameters, name_parameter = value, ...)]
Element
.Net Framework 提供两种类型的属性:预定义属性和自定义构建属性。
让我们看看如何声明自定义属性 -
'//a custom attribute BugFix to be assigned to a class and its members
[AttributeUsage(
AttributeTargets.Class |
AttributeTargets.Constructor |
AttributeTargets.Field |
AttributeTargets.Method |
AttributeTargets.Property,
AllowMultiple = true)]
public class DeBugInfo : System.Attribute