英語で読む

次の方法で共有


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 は、Visual Basic 標準モジュールではなく、移植可能な実行可能ファイル (.dll または .exe) を参照します。

Parameter 2048

パラメーターに属性を適用できます。

Property 128

プロパティに属性を適用できます。

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

こちらもご覧ください