編輯

共用方式為


AttributeUsageAttribute(AttributeTargets) Constructor

Definition

Initializes a new instance of the AttributeUsageAttribute class with the specified list of AttributeTargets, the AllowMultiple value, and the Inherited value.

public:
 AttributeUsageAttribute(AttributeTargets validOn);
public AttributeUsageAttribute (AttributeTargets validOn);
new AttributeUsageAttribute : AttributeTargets -> AttributeUsageAttribute
Public Sub New (validOn As AttributeTargets)

Parameters

validOn
AttributeTargets

The set of values combined using a bitwise OR operation to indicate which program elements are valid.

Examples

The definition of the DispId attribute illustrates the use of a bitwise OR operation to combine several AttributeTargets values.

namespace InteropServices
{
   [AttributeUsage(AttributeTargets::Method|
   AttributeTargets::Field|
   AttributeTargets::Property)
   ]
   public ref class DispIdAttribute: public Attribute
   {
   public:
      DispIdAttribute( int value )
      {
         // . . .
      }

      property int Value 
      {
         int get()
         {
            // . . .
            return 0;
         }
      }
   };
}
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

Remarks

You can combine several AttributeTargets values using a bitwise OR operation to get the desired combination of valid program elements.

For default property values, see the ValidOn, AllowMultiple, and Inherited properties.

Applies to

See also