AttributeUsage (C# および Visual Basic)

カスタム属性クラスの使用方法を決定します。AttributeUsage は、新しい属性の適用方法を制御するカスタム属性定義に適用できる属性です。既定の設定を明示的に割り当てると、次のようになります。

    <System.AttributeUsage(System.AttributeTargets.All, 
                       AllowMultiple:=False, 
                       Inherited:=True)> 
    Class NewAttribute
        Inherits System.Attribute
    End Class

[System.AttributeUsage(System.AttributeTargets.All,
                   AllowMultiple = false,
                   Inherited = true)]
class NewAttribute : System.Attribute { }

この例では、NewAttribute クラスは、属性を使用できるコード要素すべてに適用できますが、各要素に適用できるのは 1 つだけです。基本クラスに適用すると、派生クラスによって継承されます。

AllowMultiple 引数と Inherited 引数はオプションなので、次のコードでも同じ効果があります。

<System.AttributeUsage(System.AttributeTargets.All)> 
Class NewAttribute
    Inherits System.Attribute
End Class
[System.AttributeUsage(System.AttributeTargets.All)]
class NewAttribute : System.Attribute { }

最初の AttributeUsage 引数は、AttributeTargets 列挙体の 1 つ以上の要素にする必要があります。複数の型を対象にする場合、次のように OR 演算子で結合できます。

Imports System


...


<AttributeUsage(AttributeTargets.Property Or AttributeTargets.Field)> 
Class NewPropertyOrFieldAttribute
    Inherits Attribute
End Class
using System;


...


[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]
class NewPropertyOrFieldAttribute : Attribute { }

次のように AllowMultiple 引数を true に設定すると、1 つの要素に結果の属性を複数適用できます。

Imports System


...


    <AttributeUsage(AttributeTargets.Class, AllowMultiple:=True)> 
    Class MultiUseAttr
        Inherits Attribute
    End Class

    <MultiUseAttr(), MultiUseAttr()> 
    Class Class1
    End Class

using System;


...


[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
class MultiUseAttr : Attribute { }

[MultiUseAttr]
[MultiUseAttr]
class Class1 { }

[MultiUseAttr, MultiUseAttr]
class Class2 { }

この場合、AllowMultiple が true に設定されているため、MultiUseAttr を繰り返し適用できます。複数の属性を適用する場合、どちらの形式でも有効です。

Inherited を false に設定した場合、属性を指定したクラスから派生するクラスには、属性が継承されません。次に例を示します。

Imports System


...


<AttributeUsage(AttributeTargets.Class, Inherited:=False)> 
Class Attr1
    Inherits Attribute
End Class

<Attr1()> 
Class BClass

End Class  

Class DClass
    Inherits BClass
End Class
using System;


...


[AttributeUsage(AttributeTargets.Class, Inherited = false)]
class Attr1 : Attribute { }

[Attr1]
class BClass { }

class DClass : BClass { }

この場合、Attr1 は 継承によって DClass に適用されません。

解説

AttributeUsage 属性は、シングルユースの属性です。同一のクラスに複数回適用することはできません。AttributeUsage は AttributeUsageAttribute のエイリアスです。

詳細については、「リフレクションを使用した属性へのアクセス (C# および Visual Basic)」を参照してください。

使用例

次の例は、Inherited 引数と AllowMultiple 引数を AttributeUsage 属性に指定する影響と、クラスに適用されるカスタム属性の列挙方法に関するデモンストレーションです。

Imports System


...


    ' Create some custom attributes:
    <AttributeUsage(System.AttributeTargets.Class, Inherited:=False)> 
    Class A1
        Inherits System.Attribute
    End Class

    <AttributeUsage(System.AttributeTargets.Class)> 
    Class A2
        Inherits System.Attribute
    End Class    

    <AttributeUsage(System.AttributeTargets.Class, AllowMultiple:=True)> 
    Class A3
        Inherits System.Attribute
    End Class


    ' Apply custom attributes to classes:
    <A1(), A2()> 
    Class BaseClass

    End Class

    <A3(), A3()> 
    Class DerivedClass
        Inherits BaseClass
    End Class


    Public Class TestAttributeUsage
        Sub Main()
            Dim b As New BaseClass
            Dim d As New DerivedClass
            ' Display custom attributes for each class.
            Console.WriteLine("Attributes on Base Class:")
            Dim attrs() As Object = b.GetType().GetCustomAttributes(True)

            For Each attr In attrs
                Console.WriteLine(attr)
            Next

            Console.WriteLine("Attributes on Derived Class:")
            attrs = d.GetType().GetCustomAttributes(True)
            For Each attr In attrs
                Console.WriteLine(attr)
            Next            
        End Sub
    End Class

using System;


...


// Create some custom attributes:
[AttributeUsage(System.AttributeTargets.Class, Inherited = false)]
class A1 : System.Attribute { }

[AttributeUsage(System.AttributeTargets.Class)]
class A2 : System.Attribute { }

[AttributeUsage(System.AttributeTargets.Class, AllowMultiple = true)]
class A3 : System.Attribute { }

// Apply custom attributes to classes:
[A1, A2]
class BaseClass { }

[A3, A3]
class DerivedClass : BaseClass { }

public class TestAttributeUsage
{
    static void Main()
    {
        BaseClass b = new BaseClass();
        DerivedClass d = new DerivedClass();

        // Display custom attributes for each class.
        Console.WriteLine("Attributes on Base Class:");
        object[] attrs = b.GetType().GetCustomAttributes(true);
        foreach (Attribute attr in attrs)
        {
            Console.WriteLine(attr);
        }

        Console.WriteLine("Attributes on Derived Class:");
        attrs = d.GetType().GetCustomAttributes(true);
        foreach (Attribute attr in attrs)
        {
            Console.WriteLine(attr);
        }
    }
}

出力例

Attributes on Base Class:
A1
A2
Attributes on Derived Class:
A3
A3
A2

参照

関連項目

リフレクション (C# および Visual Basic)

属性 (C# および Visual Basic)

カスタム属性の作成 (C# および Visual Basic)

リフレクションを使用した属性へのアクセス (C# および Visual Basic)

Attribute

System.Reflection

概念

C# プログラミング ガイド

その他の技術情報

Visual Basic のプログラミング ガイド

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