AttributeTargets 枚举

定义

指定可应用属性的应用程序元素。

此枚举支持其成员值的按位组合。

C#
[System.Flags]
public enum AttributeTargets
C#
[System.Flags]
[System.Serializable]
public enum AttributeTargets
C#
[System.Flags]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum AttributeTargets
继承
AttributeTargets
属性

字段

All 32767

可以对任何应用程序元素应用属性。

Assembly 1

可以对程序集应用属性。

Class 4

可以对类应用属性。

Constructor 32

可以对构造函数应用属性。

Delegate 4096

可以对委托应用属性。

Enum 16

可以对枚举应用属性。

Event 512

可以对事件应用属性。

Field 256

可以对字段应用属性。

GenericParameter 16384

可以对泛型参数应用属性。 目前,此属性仅可应用于 C#、Microsoft 中间语言 (MSIL) 和已发出的代码中。

Interface 1024

可以对接口应用属性。

Method 64

可以对方法应用属性。

Module 2

可以对模块应用属性。 Module 引用的是可移植可执行文件(.dll 或 .exe),而不是 Visual Basic 标准模块。

Parameter 2048

可以对参数应用属性。

Property 128

可以对属性 (Property) 应用属性 (Attribute)。

ReturnValue 8192

可以对返回值应用属性。

Struct 8

可以对结构应用属性,即值类型。

示例

以下示例演示了将属性应用于各种目标。

备注

Visual Basic和 Visual C++ 语法目前不支持将属性应用于类型参数。

C#
using System;

namespace AttTargsCS {
    // This attribute is only valid on a class.
    [AttributeUsage(AttributeTargets.Class)]
    public class ClassTargetAttribute : Attribute {
    }

    // This attribute is only valid on a method.
    [AttributeUsage(AttributeTargets.Method)]
    public class MethodTargetAttribute : Attribute {
    }

    // This attribute is only valid on a constructor.
    [AttributeUsage(AttributeTargets.Constructor)]
    public class ConstructorTargetAttribute : Attribute {
    }

    // This attribute is only valid on a field.
    [AttributeUsage(AttributeTargets.Field)]
    public class FieldTargetAttribute : Attribute {
    }

    // This attribute is valid on a class or a method.
    [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]
    public class ClassMethodTargetAttribute : Attribute {
    }

    // This attribute is valid on a generic type parameter.
    [AttributeUsage(AttributeTargets.GenericParameter)]
    public class GenericParameterTargetAttribute : Attribute {
    }

    // This attribute is valid on any target.
    [AttributeUsage(AttributeTargets.All)]
    public class AllTargetsAttribute : Attribute {
    }

    [ClassTarget]
    [ClassMethodTarget]
    [AllTargets]
    public class TestClassAttribute {
        [ConstructorTarget]
        [AllTargets]
        TestClassAttribute() {
        }

        [MethodTarget]
        [ClassMethodTarget]
        [AllTargets]
        public void Method1() {
        }

        [FieldTarget]
        [AllTargets]
        public int myInt;

        public void GenericMethod<
            [GenericParameterTarget, AllTargets] T>(T x) {
        }

        static void Main(string[] args) {
        }
    }
}

注解

AttributeUsageAttribute 类使用此枚举指定应用属性时有效的元素类型。

AttributeTargets 枚举值可与按位 OR 操作结合使用,以获取首选组合。

适用于

产品 版本
.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

另请参阅