AttributeUsageAttribute(AttributeTargets) 建構函式
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
初始化類別的新實例 AttributeUsageAttribute ,包含指定的清單 AttributeTargets、值 AllowMultiple 和值 Inherited 。
public:
AttributeUsageAttribute(AttributeTargets validOn);
public AttributeUsageAttribute(AttributeTargets validOn);
new AttributeUsageAttribute : AttributeTargets -> AttributeUsageAttribute
Public Sub New (validOn As AttributeTargets)
參數
- validOn
- AttributeTargets
這些值集合會透過位元順序 OR 運算組合,以表示哪些程式元素有效。
範例
DispId 屬性的定義說明了如何利用位元順序 OR 運算來組合多個 AttributeTargets 值。
namespace System.Runtime.InteropServices
{
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Field |
AttributeTargets.Property)]
public class DispIdAttribute: System.Attribute
{
public DispIdAttribute(int value) {
// . . .
}
public int Value {
get {
// . . .
return 0;
}
}
}
}
namespace System.Runtime.InteropServices
open System
[<AttributeUsage(AttributeTargets.Method ||| AttributeTargets.Field ||| AttributeTargets.Property)>]
type DispIdAttribute(value: int) =
inherit Attribute()
// . . .
member _.Value with get() =
// . . .
0
Namespace System.Runtime.InteropServices
<AttributeUsage(AttributeTargets.Method _
Or AttributeTargets.Field _
Or AttributeTargets.Property)> _
Public Class DispIdAttribute
Inherits System.Attribute
Public Sub New(value As Integer)
' . . .
End Sub
Public ReadOnly Property Value() As Integer
Get
' . . .
Return 0
End Get
End Property
End Class
End Namespace
備註
你可以用位元順序的 OR 運算組合多個 AttributeTargets 值,得到所需的有效程式元素組合。
關於預設屬性值,請參見 ValidOn、 AllowMultiple和 Inherited 屬性。