AttributeUsageAttribute(AttributeTargets) 建構函式

定義

初始化類別的新實例 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 值,得到所需的有效程式元素組合。

關於預設屬性值,請參見 ValidOnAllowMultipleInherited 屬性。

適用於

另請參閱