次の方法で共有


カスタム属性にアクセスする

属性がプログラム要素に関連付けられた後、リフレクションを使用して、その存在と値を照会できます。 .NET には MetadataLoadContextが用意されており、これを使用して、実行のために読み込めなかったコードを調べることができます。

MetadataLoadContext

MetadataLoadContext コンテキストに読み込まれたコードは実行できません。 つまり、コンストラクターを実行する必要があるため、カスタム属性のインスタンスを作成できません。 MetadataLoadContext コンテキストでカスタム属性を読み込んで調べるには、CustomAttributeData クラスを使用します。 静的 CustomAttributeData.GetCustomAttributes メソッドの適切なオーバーロードを使用して、このクラスのインスタンスを取得できます。 詳細については、「 方法: MetadataLoadContext を使用してアセンブリの内容を検査する」を参照してください。

実行コンテキスト

実行コンテキストで属性を照会する主なリフレクション メソッドは、 MemberInfo.GetCustomAttributesAttribute.GetCustomAttributesです。

カスタム属性のアクセシビリティは、アタッチされているアセンブリに関してチェックされます。 これは、カスタム属性がアタッチされているアセンブリ内の型のメソッドがカスタム属性のコンストラクターを呼び出すことができるかどうかを確認することと同じです。

Assembly.GetCustomAttributes(Boolean)などのメソッドは、型引数の可視性とアクセシビリティをチェックします。 ユーザー定義型を含むアセンブリ内のコードのみが、 GetCustomAttributesを使用してその型のカスタム属性を取得できます。

次の C# の例は、一般的なカスタム属性デザイン パターンです。 ランタイム カスタム属性リフレクション モデルを示します。

System.DLL
public class DescriptionAttribute : Attribute
{
}

System.Web.DLL
internal class MyDescriptionAttribute : DescriptionAttribute
{
}

public class LocalizationExtenderProvider
{
    [MyDescriptionAttribute(...)]
    public CultureInfo GetLanguage(...)
    {
    }
}

ランタイムが、DescriptionAttribute メソッドにアタッチGetLanguageパブリック カスタム属性の種類のカスタム属性を取得しようとすると、次のアクションが実行されます。

  1. ランタイムは、DescriptionAttributeType.GetCustomAttributes(Type type)型引数がパブリックであるため、表示およびアクセス可能であることを確認します。
  2. ランタイムは、MyDescriptionAttributeから派生したユーザー定義型DescriptionAttributeが、メソッド GetLanguage()にアタッチされている System.Web.dll アセンブリ内で表示およびアクセス可能であることを確認します。
  3. ランタイムは、 MyDescriptionAttribute のコンストラクターが System.Web.dll アセンブリ内で表示され、アクセス可能であることを確認します。
  4. ランタイムは、カスタム属性パラメーターを使用して MyDescriptionAttribute のコンストラクターを呼び出し、新しいオブジェクトを呼び出し元に返します。

カスタム属性リフレクション モデルでは、型が定義されているアセンブリの外部で、ユーザー定義型のインスタンスがリークする可能性があります。 これは、Type.GetMethodsRuntimeMethodInfo オブジェクトの配列を返すような、ユーザー定義型のインスタンスを返すランタイムシステムライブラリのメンバーと何ら変わりありません。 クライアントがユーザー定義のカスタム属性型に関する情報を検出できないようにするには、その型のメンバーを非パブリックに定義します。

次の例では、リフレクションを使用してカスタム属性にアクセスする基本的な方法を示します。

using System;

public class ExampleAttribute : Attribute
{
    private string stringVal;

    public ExampleAttribute()
    {
        stringVal = "This is the default string.";
    }

    public string StringValue
    {
        get { return stringVal; }
        set { stringVal = value; }
    }
}

[Example(StringValue="This is a string.")]
class Class1
{
    public static void Main()
    {
        System.Reflection.MemberInfo info = typeof(Class1);
        foreach (object attrib in info.GetCustomAttributes(true))
        {
            Console.WriteLine(attrib);
        }
    }
}
Public Class ExampleAttribute
    Inherits Attribute

    Private stringVal As String

    Public Sub New()
        stringVal = "This is the default string."
    End Sub

    Public Property StringValue() As String
        Get
            Return stringVal
        End Get
        Set(Value As String)
            stringVal = Value
        End Set
    End Property
End Class

<Example(StringValue:="This is a string.")> _
Class Class1
    Public Shared Sub Main()
        Dim info As System.Reflection.MemberInfo = GetType(Class1)
        For Each attrib As Object In info.GetCustomAttributes(true)
            Console.WriteLine(attrib)
        Next attrib
    End Sub
End Class

こちらも参照ください