AttributeUsage(C# 和 Visual Basic)
确定可以如何使用自定义特性类。AttributeUsage 是一个可应用于自定义特性定义,以便控制如何应用新特性的特性。显式应用时默认设置如下所示:
<System.AttributeUsage(System.AttributeTargets.All,
AllowMultiple:=False,
Inherited:=True)>
Class NewAttribute
Inherits System.Attribute
End Class
[System.AttributeUsage(System.AttributeTargets.All,
AllowMultiple = false,
Inherited = true)]
class NewAttribute : System.Attribute { }
在本例中,NewAttribute 类可应用于任何支持特性的代码实体,但是对每个实体只可应用一次。当应用于基类时,它可由派生类继承。
AllowMultiple 和 Inherited 参数是可选的,所以此代码具有相同的效果:
<System.AttributeUsage(System.AttributeTargets.All)>
Class NewAttribute
Inherits System.Attribute
End Class
[System.AttributeUsage(System.AttributeTargets.All)]
class NewAttribute : System.Attribute { }
第一个 AttributeUsage 参数必须是 AttributeTargets 枚举的一个或多个元素。多个目标类型可使用 OR 运算符链接在一起,如下例所示:
Imports System
...
<AttributeUsage(AttributeTargets.Property Or AttributeTargets.Field)>
Class NewPropertyOrFieldAttribute
Inherits Attribute
End Class
using System;
...
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
class NewPropertyOrFieldAttribute : Attribute { }
如果 AllowMultiple 参数设置为 true,则返回特性可对单个实体应用多次,如下例所示:
Imports System
...
<AttributeUsage(AttributeTargets.Class, AllowMultiple:=True)>
Class MultiUseAttr
Inherits Attribute
End Class
<MultiUseAttr(), MultiUseAttr()>
Class Class1
End Class
using System;
...
[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
class MultiUseAttr : Attribute { }
[MultiUseAttr]
[MultiUseAttr]
class Class1 { }
[MultiUseAttr, MultiUseAttr]
class Class2 { }
在这种情况下,由于 AllowMultiple 设置为 true,MultiUseAttr 可反复应用。所示的应用多个特性的这两种格式均有效。
如果 Inherited 设置为 false,则该特性不由从特性化的类派生的类继承。例如:
Imports System
...
<AttributeUsage(AttributeTargets.Class, Inherited:=False)>
Class Attr1
Inherits Attribute
End Class
<Attr1()>
Class BClass
End Class
Class DClass
Inherits BClass
End Class
using System;
...
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
class Attr1 : Attribute { }
[Attr1]
class BClass { }
class DClass : BClass { }
在这种情况下,Attr1 不通过继承应用于 DClass。
备注
AttributeUsage 特性是一个单用途特性,它无法对相同的类应用多次。AttributeUsage 是 AttributeUsageAttribute 的别名。
有关更多信息,请参见 使用反射访问特性(C# 和 Visual Basic)。
示例
下面的示例将阐释 Inherited 参数和 AllowMultiple 参数对 AttributeUsage 特性的效果,以及如何才能枚举应用于类的自定义特性。
Imports System
...
' Create some custom attributes:
<AttributeUsage(System.AttributeTargets.Class, Inherited:=False)>
Class A1
Inherits System.Attribute
End Class
<AttributeUsage(System.AttributeTargets.Class)>
Class A2
Inherits System.Attribute
End Class
<AttributeUsage(System.AttributeTargets.Class, AllowMultiple:=True)>
Class A3
Inherits System.Attribute
End Class
' Apply custom attributes to classes:
<A1(), A2()>
Class BaseClass
End Class
<A3(), A3()>
Class DerivedClass
Inherits BaseClass
End Class
Public Class TestAttributeUsage
Sub Main()
Dim b As New BaseClass
Dim d As New DerivedClass
' Display custom attributes for each class.
Console.WriteLine("Attributes on Base Class:")
Dim attrs() As Object = b.GetType().GetCustomAttributes(True)
For Each attr In attrs
Console.WriteLine(attr)
Next
Console.WriteLine("Attributes on Derived Class:")
attrs = d.GetType().GetCustomAttributes(True)
For Each attr In attrs
Console.WriteLine(attr)
Next
End Sub
End Class
using System;
...
// Create some custom attributes:
[AttributeUsage(System.AttributeTargets.Class, Inherited = false)]
class A1 : System.Attribute { }
[AttributeUsage(System.AttributeTargets.Class)]
class A2 : System.Attribute { }
[AttributeUsage(System.AttributeTargets.Class, AllowMultiple = true)]
class A3 : System.Attribute { }
// Apply custom attributes to classes:
[A1, A2]
class BaseClass { }
[A3, A3]
class DerivedClass : BaseClass { }
public class TestAttributeUsage
{
static void Main()
{
BaseClass b = new BaseClass();
DerivedClass d = new DerivedClass();
// Display custom attributes for each class.
Console.WriteLine("Attributes on Base Class:");
object[] attrs = b.GetType().GetCustomAttributes(true);
foreach (Attribute attr in attrs)
{
Console.WriteLine(attr);
}
Console.WriteLine("Attributes on Derived Class:");
attrs = d.GetType().GetCustomAttributes(true);
foreach (Attribute attr in attrs)
{
Console.WriteLine(attr);
}
}
}
示例输出
Attributes on Base Class:
A1
A2
Attributes on Derived Class:
A3
A3
A2