AttributeUsageAttribute(AttributeTargets) 构造函数

定义

用指定的 AttributeUsageAttributeAttributeTargets 值和 AllowMultiple 值列表初始化 Inherited 类的新实例。

C#
public AttributeUsageAttribute (AttributeTargets validOn);

参数

validOn
AttributeTargets

使用按位"或"运算符组合的一组值,用于指示哪些程序元素是有效的。

示例

DispId 特性的定义阐释了如何使用按位 "或" 运算组合多个 AttributeTargets 值。

C#
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;
         }
      }
   }
}

注解

可以 AttributeTargets 使用按位 "或" 运算组合几个值,以获取所需的有效程序元素组合。

对于默认属性值,请参阅 ValidOnAllowMultipleInherited 属性。

适用于

产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

另请参阅