AttributeUsageAttribute(AttributeTargets) 构造函数

定义

用指定的 AttributeUsageAttributeAttributeTargets 值和 AllowMultiple 值列表初始化 Inherited 类的新实例。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)

参数

validOn
AttributeTargets

使用按位"或"运算符组合的一组值,用于指示哪些程序元素是有效的。The set of values combined using a bitwise OR operation to indicate which program elements are valid.

示例

DispId 特性的定义阐释了如何使用按位 "或" 运算来合并多个 AttributeTargets 值。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
    
    <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

注解

可以使用按位 "或" 运算组合多个 AttributeTargets 值,以获取所需的有效程序元素组合。You can combine several AttributeTargets values using a bitwise OR operation to get the desired combination of valid program elements.

对于默认属性值,请参阅 ValidOnAllowMultipleInherited 属性。For default property values, see the ValidOn, AllowMultiple, and Inherited properties.

适用于

另请参阅