閱讀英文

共用方式為


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 Intermediate Language (MSIL) 和已發出的程式碼。

Interface 1024

屬性可以套用至介面。

Method 64

屬性可以套用至方法。

Module 2

屬性可以套用至模組。 Module 是指可攜式執行檔 (.dll 或 .exe),且不是 Visual Basic 標準模組。

Parameter 2048

屬性可以套用至參數。

Property 128

屬性 (Attibute) 可以套用至屬性 (Property)。

ReturnValue 8192

屬性可以套用至傳回值。

Struct 8

屬性可以套用至結構,也就是實值型別 (Value Type)。

範例

下列範例說明將屬性套用至各種目標。

注意

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

另請參閱