Attribute クラス

定義

カスタム属性の基本クラスを表します。

public ref class Attribute abstract
public ref class Attribute abstract : System::Runtime::InteropServices::_Attribute
[System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=false, Inherited=true)]
public abstract class Attribute
[System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=false, Inherited=true)]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Serializable]
public abstract class Attribute : System.Runtime.InteropServices._Attribute
[System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=false, Inherited=true)]
[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class Attribute : System.Runtime.InteropServices._Attribute
[<System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=false, Inherited=true)>]
type Attribute = class
[<System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=false, Inherited=true)>]
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Serializable>]
type Attribute = class
    interface _Attribute
[<System.AttributeUsage(System.AttributeTargets.All, AllowMultiple=false, Inherited=true)>]
[<System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)>]
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type Attribute = class
    interface _Attribute
Public MustInherit Class Attribute
Public MustInherit Class Attribute
Implements _Attribute
継承
Attribute
派生
属性
実装

次のコード例は、 の使用方法 Attributeを示しています。

using namespace System;
using namespace System::Reflection;

// An enumeration of animals. Start at 1 (0 = uninitialized).
public enum class Animal
{
    // Pets.
    Dog = 1,
    Cat, Bird
};

// A custom attribute to allow a target to have a pet.
public ref class AnimalTypeAttribute: public Attribute
{
public:

    // The constructor is called when the attribute is set.
    AnimalTypeAttribute( Animal pet )
    {
        thePet = pet;
    }


protected:

    // Keep a variable internally ...
    Animal thePet;

public:

    property Animal Pet 
    {
        // .. and show a copy to the outside world.
        Animal get()
        {
            return thePet;
        }

        void set( Animal value )
        {
            thePet = value;
        }
    }
};

// A test class where each method has its own pet.
ref class AnimalTypeTestClass
{
public:

    [AnimalType(Animal::Dog)]
    void DogMethod(){}


    [AnimalType(Animal::Cat)]
    void CatMethod(){}

    [AnimalType(Animal::Bird)]
    void BirdMethod(){}

};

int main()
{
    AnimalTypeTestClass^ testClass = gcnew AnimalTypeTestClass;
    Type^ type = testClass->GetType();

    // Iterate through all the methods of the class.
    System::Collections::IEnumerator^ myEnum = 
        type->GetMethods()->GetEnumerator();
    while ( myEnum->MoveNext() )
    {
        MethodInfo^ mInfo = safe_cast<MethodInfo^>(myEnum->Current);

        // Iterate through all the Attributes for each method.
        System::Collections::IEnumerator^ myEnum1 = 
            Attribute::GetCustomAttributes( mInfo )->GetEnumerator();
        while ( myEnum1->MoveNext() )
        {
            Attribute^ attr = safe_cast<Attribute^>(myEnum1->Current);

            // Check for the AnimalType attribute.
            if ( attr->GetType() == AnimalTypeAttribute::typeid )
                Console::WriteLine( "Method {0} has a pet {1} attribute.", 
                mInfo->Name, (dynamic_cast<AnimalTypeAttribute^>(attr))->Pet );
        }
    }
}

/*
 * Output:
 * Method DogMethod has a pet Dog attribute.
 * Method CatMethod has a pet Cat attribute.
 * Method BirdMethod has a pet Bird attribute.
 */
using System;
using System.Reflection;

// An enumeration of animals. Start at 1 (0 = uninitialized).
public enum Animal {
    // Pets.
    Dog = 1,
    Cat,
    Bird,
}

// A custom attribute to allow a target to have a pet.
public class AnimalTypeAttribute : Attribute {
    // The constructor is called when the attribute is set.
    public AnimalTypeAttribute(Animal pet) {
        thePet = pet;
    }

    // Keep a variable internally ...
    protected Animal thePet;

    // .. and show a copy to the outside world.
    public Animal Pet {
        get { return thePet; }
        set { thePet = value; }
    }
}

// A test class where each method has its own pet.
class AnimalTypeTestClass {
    [AnimalType(Animal.Dog)]
    public void DogMethod() {}

    [AnimalType(Animal.Cat)]
    public void CatMethod() {}

    [AnimalType(Animal.Bird)]
    public void BirdMethod() {}
}

class DemoClass {
    static void Main(string[] args) {
        AnimalTypeTestClass testClass = new AnimalTypeTestClass();
        Type type = testClass.GetType();
        // Iterate through all the methods of the class.
        foreach(MethodInfo mInfo in type.GetMethods()) {
            // Iterate through all the Attributes for each method.
            foreach (Attribute attr in
                Attribute.GetCustomAttributes(mInfo)) {
                // Check for the AnimalType attribute.
                if (attr.GetType() == typeof(AnimalTypeAttribute))
                    Console.WriteLine(
                        "Method {0} has a pet {1} attribute.",
                        mInfo.Name, ((AnimalTypeAttribute)attr).Pet);
            }
        }
    }
}
/*
 * Output:
 * Method DogMethod has a pet Dog attribute.
 * Method CatMethod has a pet Cat attribute.
 * Method BirdMethod has a pet Bird attribute.
 */
open System

// An enumeration of animals. Start at 1 (0 = uninitialized).
type Animal =
    | Dog = 1
    | Cat = 2
    | Bird = 3

// A custom attribute to allow a target to have a pet.
type AnimalTypeAttribute(pet) =
    inherit Attribute()
    member val Pet = pet with get, set

// A test class where each method has its own pet.
type AnimalTypeTestClass() =
    [<AnimalType(Animal.Dog)>]
    member _.DogMethod() = ()

    [<AnimalType(Animal.Cat)>]
    member _.CatMethod() = ()

    [<AnimalType(Animal.Bird)>]
    member _.BirdMethod() = ()

let testClass = AnimalTypeTestClass()
let clsType = testClass.GetType()
// Iterate through all the methods of the class.
for mInfo in clsType.GetMethods() do
    // Iterate through all the Attributes for each method.
    for attr in Attribute.GetCustomAttributes mInfo do
        // Check for the AnimalType attribute.
        if attr.GetType() = typeof<AnimalTypeAttribute> then
            printfn $"Method {mInfo.Name} has a pet {(attr :?> AnimalTypeAttribute).Pet} attribute."

// Output:
//   Method DogMethod has a pet Dog attribute.
//   Method CatMethod has a pet Cat attribute.
//   Method BirdMethod has a pet Bird attribute.
Imports System.Reflection

Public Module CustomAttrVB

    ' An enumeration of animals. Start at 1 (0 = uninitialized).
    Public Enum Animal
        ' Pets
        Dog = 1
        Cat
        Bird
    End Enum

    ' Visual Basic requires the AttributeUsage be specified.
    ' A custom attribute to allow a target to have a pet.
    <AttributeUsage(AttributeTargets.Method)> _
    Public Class AnimalTypeAttribute
        Inherits Attribute

        ' The constructor is called when the attribute is set.
        Public Sub New(ByVal animal As Animal)
            Me.thePet = animal
        End Sub

        ' Keep a variable internally ...
        Protected thePet As Animal

        ' .. and show a copy to the outside world.
        Public Property Pet() As Animal
            Get
                Return thePet
            End Get
            Set(ByVal Value As Animal)
                thePet = Value
            End Set
        End Property

    End Class

    ' A test class where each method has its own pet.
    Class AnimalTypeTestClass

        <AnimalType(Animal.Dog)> _
        Public Sub DogMethod()
        End Sub

        <AnimalType(Animal.Cat)> _
        Public Sub CatMethod()
        End Sub

        <AnimalType(Animal.Bird)> _
        Public Sub BirdMethod()
        End Sub
    End Class

    ' The runtime test.
    Sub Main()
        Dim testClass As New AnimalTypeTestClass()
        Dim tcType As Type = testClass.GetType()
        Dim mInfo As MethodInfo
        ' Iterate through all the methods of the class.
        For Each mInfo In tcType.GetMethods()
            Dim attr As Attribute
            ' Iterate through all the attributes of the method.
            For Each attr In Attribute.GetCustomAttributes(mInfo)
                If TypeOf attr Is AnimalTypeAttribute Then
                    Dim attrCustom As AnimalTypeAttribute = _
                        CType(attr, AnimalTypeAttribute)
                    Console.WriteLine("Method {0} has a pet {1} attribute.", _
                         mInfo.Name(), attrCustom.Pet.ToString())
                End If
            Next
        Next
    End Sub
End Module

' Output:
' Method DogMethod has a pet Dog attribute.
' Method CatMethod has a pet Cat attribute.
' Method BirdMethod has a pet Bird attribute.

注釈

クラスは Attribute 、定義済みのシステム情報またはユーザー定義のカスタム情報をターゲット要素に関連付けます。 ターゲット要素には、アセンブリ、クラス、コンストラクター、デリゲート、列挙型、イベント、フィールド、インターフェイス、メソッド、ポータブル実行可能ファイル モジュール、パラメーター、プロパティ、戻り値、構造体、または別の属性を指定できます。

属性によって提供される情報は、''メタデータ'' とも呼ばれます。 メタデータは、アプリケーションによって実行時に調べ、プログラムによるデータの処理方法を制御したり、外部ツールによって実行時より前に調べたりして、アプリケーション自体の処理方法や保守方法を制御できます。 たとえば、.NET では、属性型を事前に定義して使用して実行時の動作を制御し、一部のプログラミング言語では属性型を使用して、.NET 共通型システムで直接サポートされていない言語機能を表します。

すべての属性型は、 クラスから直接または間接的に Attribute 派生します。 属性は、任意のターゲット要素に適用できます。複数の属性を同じターゲット要素に適用できます。属性と 属性は、ターゲット要素から派生した要素によって継承できます。 属性を AttributeTargets 適用するターゲット要素を指定するには、 クラスを使用します。

クラスには Attribute 、カスタム属性を取得してテストするための便利なメソッドが用意されています。 属性の使用の詳細については、「属性と属性の適用」を参照してください。

コンストラクター

Attribute()

Attribute クラスの新しいインスタンスを初期化します。

プロパティ

TypeId

派生クラスで実装されると、この Attribute の一意の識別子を取得します。

メソッド

Equals(Object)

このインスタンスが、指定されたオブジェクトと等価であるかどうかを示す値を返します。

GetCustomAttribute(Assembly, Type)

指定したアセンブリに適用されたカスタム属性を取得します。 各パラメーターは、対象のアセンブリと検索対象のカスタム属性の型を指定します。

GetCustomAttribute(Assembly, Type, Boolean)

アセンブリに適用されたカスタム属性を取得します。 各パラメーターは、対象のアセンブリ、検索対象のカスタム属性の型、および無視する検索オプションを指定します。

GetCustomAttribute(MemberInfo, Type)

型のメンバーに適用されたカスタム属性を取得します。 各パラメーターは、対象のメンバーと検索対象のカスタム属性の型を指定します。

GetCustomAttribute(MemberInfo, Type, Boolean)

型のメンバーに適用されたカスタム属性を取得します。 各パラメーターは、対象のメンバー、検索対象のカスタム属性の型、およびそのメンバーの先祖を検索するかどうかを指定します。

GetCustomAttribute(Module, Type)

モジュールに適用されたカスタム属性を取得します。 各パラメーターは、対象のモジュールと検索対象のカスタム属性の型を指定します。

GetCustomAttribute(Module, Type, Boolean)

モジュールに適用されたカスタム属性を取得します。 各パラメーターは、対象のモジュール、検索対象のカスタム属性の型、および無視する検索オプションを指定します。

GetCustomAttribute(ParameterInfo, Type)

メソッド パラメーターに適用されたカスタム属性を取得します。 各パラメーターは、対象のメソッド パラメーターと検索対象のカスタム属性の型を指定します。

GetCustomAttribute(ParameterInfo, Type, Boolean)

メソッド パラメーターに適用されたカスタム属性を取得します。 各パラメーターは、対象のメソッド パラメーター、検索対象のカスタム属性の型、およびそのメソッド パラメーターの先祖を検索するかどうかを指定します。

GetCustomAttributes(Assembly)

アセンブリに適用されたカスタム属性の配列を取得します。 パラメーターは対象のアセンブリを指定します。

GetCustomAttributes(Assembly, Boolean)

アセンブリに適用されたカスタム属性の配列を取得します。 各パラメーターは、対象のアセンブリと無視する検索オプションを指定します。

GetCustomAttributes(Assembly, Type)

アセンブリに適用されたカスタム属性の配列を取得します。 各パラメーターは、対象のアセンブリと検索対象のカスタム属性の型を指定します。

GetCustomAttributes(Assembly, Type, Boolean)

アセンブリに適用されたカスタム属性の配列を取得します。 各パラメーターは、対象のアセンブリ、検索対象のカスタム属性の型、および無視する検索オプションを指定します。

GetCustomAttributes(MemberInfo)

型のメンバーに適用されたカスタム属性の配列を取得します。 パラメーターでメンバーを指定します。

GetCustomAttributes(MemberInfo, Boolean)

型のメンバーに適用されたカスタム属性の配列を取得します。 各パラメーターは、対象のメンバー、検索対象のカスタム属性の型、およびそのメンバーの先祖を検索するかどうかを指定します。

GetCustomAttributes(MemberInfo, Type)

型のメンバーに適用されたカスタム属性の配列を取得します。 各パラメーターは、対象のメンバーと検索対象のカスタム属性の型を指定します。

GetCustomAttributes(MemberInfo, Type, Boolean)

型のメンバーに適用されたカスタム属性の配列を取得します。 各パラメーターは、対象のメンバー、検索対象のカスタム属性の型、およびそのメンバーの先祖を検索するかどうかを指定します。

GetCustomAttributes(Module)

モジュールに適用されたカスタム属性の配列を取得します。 パラメーターは、対象のモジュールを指定します。

GetCustomAttributes(Module, Boolean)

モジュールに適用されたカスタム属性の配列を取得します。 各パラメーターは、対象のモジュールと無視する検索オプションを指定します。

GetCustomAttributes(Module, Type)

モジュールに適用されたカスタム属性の配列を取得します。 各パラメーターは、対象のモジュールと検索対象のカスタム属性の型を指定します。

GetCustomAttributes(Module, Type, Boolean)

モジュールに適用されたカスタム属性の配列を取得します。 各パラメーターは、対象のモジュール、検索対象のカスタム属性の型、および無視する検索オプションを指定します。

GetCustomAttributes(ParameterInfo)

メソッド パラメーターに適用されたカスタム属性の配列を取得します。 パラメーターは、対象のメソッド パラメーターを指定します。

GetCustomAttributes(ParameterInfo, Boolean)

メソッド パラメーターに適用されたカスタム属性の配列を取得します。 各パラメーターは、対象のメソッド パラメーターと、メソッド パラメーターの先祖を検索するかどうかを指定します。

GetCustomAttributes(ParameterInfo, Type)

メソッド パラメーターに適用されたカスタム属性の配列を取得します。 各パラメーターは、対象のメソッド パラメーターと検索対象のカスタム属性の型を指定します。

GetCustomAttributes(ParameterInfo, Type, Boolean)

メソッド パラメーターに適用されたカスタム属性の配列を取得します。 各パラメーターは、対象のメソッド パラメーター、検索対象のカスタム属性の型、およびそのメソッド パラメーターの先祖を検索するかどうかを指定します。

GetHashCode()

このインスタンスのハッシュ コードを返します。

GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
IsDefaultAttribute()

派生クラスでオーバーライドされるとき、このインスタンスの値が派生クラスの既定値であるかどうかを示します。

IsDefined(Assembly, Type)

カスタム属性がアセンブリに適用されているかどうかを判断します。 各パラメーターは、対象のアセンブリと検索対象のカスタム属性の型を指定します。

IsDefined(Assembly, Type, Boolean)

カスタム属性がアセンブリに適用されているかどうかを判断します。 各パラメーターは、対象のアセンブリ、検索対象のカスタム属性の型、および無視する検索オプションを指定します。

IsDefined(MemberInfo, Type)

カスタム属性が型のメンバーに適用されているかどうかを判断します。 各パラメーターは、対象のメンバーと検索対象のカスタム属性の型を指定します。

IsDefined(MemberInfo, Type, Boolean)

カスタム属性が型のメンバーに適用されているかどうかを判断します。 各パラメーターは、対象のメンバー、検索対象のカスタム属性の型、およびそのメンバーの先祖を検索するかどうかを指定します。

IsDefined(Module, Type)

指定した型のカスタム属性がモジュールに適用されているかどうかを判断します。 各パラメーターは、対象のモジュールと検索対象のカスタム属性の型を指定します。

IsDefined(Module, Type, Boolean)

カスタム属性がモジュールに適用されているかどうかを判断します。 各パラメーターは、対象のモジュール、検索対象のカスタム属性の型、および無視する検索オプションを指定します。

IsDefined(ParameterInfo, Type)

カスタム属性がメソッド パラメーターに適用されているかどうかを判断します。 各パラメーターは、対象のメソッド パラメーターと検索対象のカスタム属性の型を指定します。

IsDefined(ParameterInfo, Type, Boolean)

カスタム属性がメソッド パラメーターに適用されているかどうかを判断します。 各パラメーターは、対象のメソッド パラメーター、検索対象のカスタム属性の型、およびそのメソッド パラメーターの先祖を検索するかどうかを指定します。

Match(Object)

派生クラス内でオーバーライドされたときに、指定したオブジェクトとこのインスタンスが等しいかどうかを示す値を返します。

MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)

明示的なインターフェイスの実装

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

一連の名前を対応する一連のディスパッチ識別子に割り当てます。

_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

オブジェクトの型情報を取得します。この情報はインターフェイスの型情報の取得に使用できます。

_Attribute.GetTypeInfoCount(UInt32)

オブジェクトが提供する型情報インターフェイスの数 (0 または 1) を取得します。

_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

オブジェクトによって公開されたプロパティおよびメソッドへのアクセスを提供します。

適用対象

スレッド セーフ

この型はスレッド セーフです。

こちらもご覧ください