次の方法で共有


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)

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

適用対象

スレッド セーフ

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

こちらもご覧ください

  • 属性 の適用の
  • 属性 を使用したメタデータの拡張の