Získávání informací uložených v atributech

Načítání vlastního atributu je jednoduchý proces. Nejprve deklarujte instanci atributu, který chcete načíst. Poté použijte metodu Attribute.GetCustomAttribute pro inicializování nového atributu na hodnotu atributu, který chcete načíst. Jakmile je nový atribut inicializován, jednoduše použijete jeho vlastnosti k získání daných hodnot.

Důležitá poznámkaDůležité

Toto téma popisuje, jak načíst atributy pro kód načtený do kontextu spuštění.Chcete-li načíst atributy pro kód načtený do kontextu pouhé reflexe, musíte použít třídu CustomAttributeData, jak je znázorněno v tématu How to: Load Assemblies into the Reflection-Only Context.

Tento oddíl popisuje následující způsoby načtení atributů:

  • Načítání jediné instance atributu

  • Načítání více instancí atributu použitého pro stejný rozsah

  • Načítání více instancí atributu použitého pro různé rozsahy

Načítání jediné instance atributu

V následujícím příkladu je DeveloperAttribute (popsán v předchozím oddílu) aplikován na třídu MainApp na úrovni třídy. Metoda GetAttribute používá GetCustomAttribute pro načtení hodnot uložených v DeveloperAttribute na úrovni třídy před jejich zobrazením na konzolu.

Imports System
Imports System.Reflection
Imports CustomCodeAttributes

<Developer("Joan Smith", "42", Reviewed := True)>
Class MainApp
    Public Shared Sub Main()
        ' Call function to get and display the attribute.
        GetAttribute(GetType(MainApp))
    End Sub

    Public Shared Sub GetAttribute(t As Type)
        ' Get instance of the attribute.
        Dim MyAttribute As DeveloperAttribute =
            CType(Attribute.GetCustomAttribute(t, GetType(DeveloperAttribute)), DeveloperAttribute)

        If MyAttribute Is Nothing Then
            Console.WriteLine("The attribute was not found.")
        Else
            ' Get the Name value.
            Console.WriteLine("The Name Attribute is: {0}." , MyAttribute.Name)
            ' Get the Level value.
            Console.WriteLine("The Level Attribute is: {0}." , MyAttribute.Level)
            ' Get the Reviewed value.
            Console.WriteLine("The Reviewed Attribute is: {0}." , MyAttribute.Reviewed)
        End If
    End Sub
End Class
using System;
using System.Reflection;
using CustomCodeAttributes;

[Developer("Joan Smith", "42", Reviewed = true)]
class MainApp
{
    public static void Main()
    {
        // Call function to get and display the attribute.
        GetAttribute(typeof(MainApp));
    }

    public static void GetAttribute(Type t)
    {
        // Get instance of the attribute.
        DeveloperAttribute MyAttribute =
            (DeveloperAttribute) Attribute.GetCustomAttribute(t, typeof (DeveloperAttribute));

        if (MyAttribute == null)
        {
            Console.WriteLine("The attribute was not found.");
        }
        else
        {
            // Get the Name value.
            Console.WriteLine("The Name Attribute is: {0}." , MyAttribute.Name);
            // Get the Level value.
            Console.WriteLine("The Level Attribute is: {0}." , MyAttribute.Level);
            // Get the Reviewed value.
            Console.WriteLine("The Reviewed Attribute is: {0}." , MyAttribute.Reviewed);
        }
    }
}
using namespace System;
using namespace System::Reflection;
using namespace CustomCodeAttributes;

[Developer("Joan Smith", "42", Reviewed = true)]
ref class MainApp
{
public:
    static void Main()
    {
        // Call function to get and display the attribute.
        GetAttribute(MainApp::typeid);
    }

    static void GetAttribute(Type^ t)
    {
        // Get instance of the attribute.
        DeveloperAttribute^ MyAttribute =
            (DeveloperAttribute^) Attribute::GetCustomAttribute(t, DeveloperAttribute::typeid);

        if (MyAttribute == nullptr)
        {
            Console::WriteLine("The attribute was not found.");
        }
        else
        {
            // Get the Name value.
            Console::WriteLine("The Name Attribute is: {0}." , MyAttribute->Name);
            // Get the Level value.
            Console::WriteLine("The Level Attribute is: {0}." , MyAttribute->Level);
            // Get the Reviewed value.
            Console::WriteLine("The Reviewed Attribute is: {0}." , MyAttribute->Reviewed);
        }
    }
};

Tento program při spuštění zobrazuje následující text.

The Name Attribute is: Joan Smith.
The Level Attribute is: 42.
The Reviewed Attribute is: True.

Pokud není atribut nalezen, metoda GetCustomAttribute inicializuje MyAttribute na hodnotu null. V tomto příkladu se kontroluje MyAttribute takovýchto instancí a uživatel je upozorněn, pokud není nalezen žádný atribut. Pokud není DeveloperAttribute nalezen v rozsahu třídy na konzoly se zobrazí následující zpráva.

The attribute was not found. 

V tomto příkladu se předpokládá, že definice atributu je v aktuálním oboru názvů. Nezapomeňte importovat obor názvů, ve kterém se nachází definice atributu, pokud není v aktuálním oboru názvů.

Načítání více instancí atributu použitého pro stejný rozsah

V předchozím příkladu byla třída, která se měla prohlédnout a konkrétní atribut, který měl být nalezen, předány GetCustomAttribute. Tento kód funguje dobře, pokud je použita pouze jedna instance atributu na úrovni třídy. Pokud je však na stejné úrovni třídy použito více instancí atributu, metoda GetCustomAttribute nenačte všechny informace. V případech kdy je více instancí stejného atributu použito ve stejném rozsahu, můžete použít Attribute.GetCustomAttributes pro umístění všech instancí atributu do pole. Například pokud jsou dvě instance DeveloperAttribute použity na úrovni třídy stejné třídy, metodu GetAttribute lze upravit tak, aby zobrazovala informace nalezené v obou atributech. Nezapomeňte, chcete-li použít více atributů na stejné úrovni, musí být atribut definován s vlastností AllowMultiple nastavenou na true v AttributeUsageAttribute.

V následujícím příkladu kódu je ukázáno, jak používat metodu GetCustomAttributes pro vytvoření pole odkazujícího na všechny instance DeveloperAttribute v libovolné dané třídě. Hodnoty všech atributů jsou poté zobrazeny na konzolu.

Public Shared Sub GetAttribute(t As Type)
    Dim MyAttributes() As DeveloperAttribute =
        CType(Attribute.GetCustomAttributes(t, GetType(DeveloperAttribute)), DeveloperAttribute())

    If MyAttributes Is Nothing Then
        Console.WriteLine("The attribute was not found.")
    Else
        For i As Integer = 0 To MyAttributes.Length - 1
            ' Get the Name value.
            Console.WriteLine("The Name Attribute is: {0}." , MyAttributes(i).Name)
            ' Get the Level value.
            Console.WriteLine("The Level Attribute is: {0}." , MyAttributes(i).Level)
            ' Get the Reviewed value.
            Console.WriteLine("The Reviewed Attribute is: {0}.", MyAttributes(i).Reviewed)
        Next i
    End If
End Sub
public static void GetAttribute(Type t)
{
    DeveloperAttribute[] MyAttributes =
        (DeveloperAttribute[]) Attribute.GetCustomAttributes(t, typeof (DeveloperAttribute));

    if (MyAttributes == null)
    {
        Console.WriteLine("The attribute was not found.");
    }
    else
    {
        for (int i = 0 ; i < MyAttributes.Length ; i++)
        {
            // Get the Name value.
            Console.WriteLine("The Name Attribute is: {0}." , MyAttributes[i].Name);
            // Get the Level value.
            Console.WriteLine("The Level Attribute is: {0}." , MyAttributes[i].Level);
            // Get the Reviewed value.
            Console.WriteLine("The Reviewed Attribute is: {0}.", MyAttributes[i].Reviewed);
        }
    }
}
public:
    static void GetAttribute(Type^ t)
    {
        array<DeveloperAttribute^>^ MyAttributes =
            (array<DeveloperAttribute^>^) Attribute::GetCustomAttributes(t, DeveloperAttribute::typeid);

        if (MyAttributes == nullptr)
        {
            Console::WriteLine("The attribute was not found.");
        }
        else
        {
            for (int i = 0 ; i < MyAttributes->Length; i++)
            {
                // Get the Name value.
                Console::WriteLine("The Name Attribute is: {0}." , MyAttributes[i]->Name);
                // Get the Level value.
                Console::WriteLine("The Level Attribute is: {0}." , MyAttributes[i]->Level);
                // Get the Reviewed value.
                Console::WriteLine("The Reviewed Attribute is: {0}.", MyAttributes[i]->Reviewed);
            }
        }
    }

Pokud nejsou nalezeny žádné atributy, tento kód upozorní uživatele. V opačném případě jsou informace obsažené v obou instancích DeveloperAttribute zobrazeny.

Načítání více instancí atributu použitého pro různé rozsahy

Metody GetCustomAttributes a GetCustomAttribute nevyhledávají v celé třídě a nevrací všechny instance atributu v dané třídě. Spíše prohledávají pouze jednu zadanou metodu nebo člena. Pokud máte třídu se stejným atributem použitým na každého člena a chcete načíst hodnoty ve všech atributech použitých na tyto členy, musíte zadat každou metodu nebo člen do GetCustomAttributes a GetCustomAttribute jednotlivě.

V následujícím příkladu kódu je třída převzata jako parametr a vyhledá se DeveloperAttribute (definovaný dříve) na úrovni třídy a každé jednotlivé metody dané třídy.

Public Shared Sub GetAttribute(t As Type)
    Dim att As DeveloperAttribute

    ' Get the class-level attributes.

    ' Put the instance of the attribute on the class level in the att object.
    att = CType(Attribute.GetCustomAttribute(t, GetType(DeveloperAttribute)), DeveloperAttribute)

    If att Is Nothing
        Console.WriteLine("No attribute in class {0}.\n", t.ToString())
    Else
        Console.WriteLine("The Name Attribute on the class level is: {0}.", att.Name)
        Console.WriteLine("The Level Attribute on the class level is: {0}.", att.Level)
        Console.WriteLine("The Reviewed Attribute on the class level is: {0}.\n", att.Reviewed)
    End If

    ' Get the method-level attributes.

    ' Get all methods in this class, and put them
    ' in an array of System.Reflection.MemberInfo objects.
    Dim MyMemberInfo() As MemberInfo = t.GetMethods()

    ' Loop through all methods in this class that are in the
    ' MyMemberInfo array.
    For i As Integer = 0 To MyMemberInfo.Length - 1
        att = CType(Attribute.GetCustomAttribute(MyMemberInfo(i), _
            GetType(DeveloperAttribute)), DeveloperAttribute)
        If att Is Nothing Then
            Console.WriteLine("No attribute in member function {0}.\n" , MyMemberInfo(i).ToString())
        Else
            Console.WriteLine("The Name Attribute for the {0} member is: {1}.",
                MyMemberInfo(i).ToString(), att.Name)
            Console.WriteLine("The Level Attribute for the {0} member is: {1}.",
                MyMemberInfo(i).ToString(), att.Level)
            Console.WriteLine("The Reviewed Attribute for the {0} member is: {1}.\n",
                MyMemberInfo(i).ToString(), att.Reviewed)
        End If
    Next
End Sub
public static void GetAttribute(Type t)
{
    DeveloperAttribute att;

    // Get the class-level attributes.

    // Put the instance of the attribute on the class level in the att object.
    att = (DeveloperAttribute) Attribute.GetCustomAttribute (t, typeof (DeveloperAttribute));

    if (att == null)
    {
        Console.WriteLine("No attribute in class {0}.\n", t.ToString());
    }
    else
    {
        Console.WriteLine("The Name Attribute on the class level is: {0}.", att.Name);
        Console.WriteLine("The Level Attribute on the class level is: {0}.", att.Level);
        Console.WriteLine("The Reviewed Attribute on the class level is: {0}.\n", att.Reviewed);
    }

    // Get the method-level attributes.

    // Get all methods in this class, and put them
    // in an array of System.Reflection.MemberInfo objects.
    MemberInfo[] MyMemberInfo = t.GetMethods();

    // Loop through all methods in this class that are in the
    // MyMemberInfo array.
    for (int i = 0; i < MyMemberInfo.Length; i++)
    {
        att = (DeveloperAttribute) Attribute.GetCustomAttribute(MyMemberInfo[i], typeof (DeveloperAttribute));
        if (att == null)
        {
            Console.WriteLine("No attribute in member function {0}.\n" , MyMemberInfo[i].ToString());
        }
        else
        {
            Console.WriteLine("The Name Attribute for the {0} member is: {1}.",
                MyMemberInfo[i].ToString(), att.Name);
            Console.WriteLine("The Level Attribute for the {0} member is: {1}.",
                MyMemberInfo[i].ToString(), att.Level);
            Console.WriteLine("The Reviewed Attribute for the {0} member is: {1}.\n",
                MyMemberInfo[i].ToString(), att.Reviewed);
        }
    }
}
public:
    static void GetAttribute(Type^ t)
    {
        DeveloperAttribute^ att;

        // Get the class-level attributes.

        // Put the instance of the attribute on the class level in the att object.
        att = (DeveloperAttribute^) Attribute::GetCustomAttribute (t, DeveloperAttribute::typeid);

        if (att == nullptr)
        {
            Console::WriteLine("No attribute in class {0}.\n", t->ToString());
        }
        else
        {
            Console::WriteLine("The Name Attribute on the class level is: {0}.", att->Name);
            Console::WriteLine("The Level Attribute on the class level is: {0}.", att->Level);
            Console::WriteLine("The Reviewed Attribute on the class level is: {0}.\n", att->Reviewed);
        }

        // Get the method-level attributes.

        // Get all methods in this class, and put them
        // in an array of System.Reflection.MemberInfo objects.
        array<MemberInfo^>^ MyMemberInfo = t->GetMethods();

        // Loop through all methods in this class that are in the
        // MyMemberInfo array.
        for (int i = 0; i < MyMemberInfo->Length; i++)
        {
            att = (DeveloperAttribute^) Attribute::GetCustomAttribute(MyMemberInfo[i], DeveloperAttribute::typeid);
            if (att == nullptr)
            {
                Console::WriteLine("No attribute in member function {0}.\n" , MyMemberInfo[i]->ToString());
            }
            else
            {
                Console::WriteLine("The Name Attribute for the {0} member is: {1}.",
                    MyMemberInfo[i]->ToString(), att->Name);
                Console::WriteLine("The Level Attribute for the {0} member is: {1}.",
                    MyMemberInfo[i]->ToString(), att->Level);
                Console::WriteLine("The Reviewed Attribute for the {0} member is: {1}.\n",
                    MyMemberInfo[i]->ToString(), att->Reviewed);
            }
        }
    }

Pokud nebyly nalezeny žádné instance DeveloperAttribute na úrovni metody nebo třídy, metoda GetAttribute upozorní uživatele, že nebyly nalezeny žádné atributy a zobrazí název metody nebo třída, která neobsahuje atribut. Pokud je atribut nalezen, položky Name, Level a Reviewed jsou zobrazeny na konzolu.

Můžete použít členy třídy Type pro získání jednotlivých metod a členů v předané třídě. V tomto příkladu se nejprve prohledá objekt Type pro získání informací o atributu pro úroveň třídy. Dále je použita Type.GetMethods pro umístění instancí všech metod do pole objektů System.Reflection.MemberInfo pro načtení informací o atributu pro úroveň metody. Můžete také použít metodu Type.GetProperties pro vyhledání atributů na úrovni vlastnosti nebo Type.GetConstructors pro zkontrolování atributů na úrovni konstruktoru.

Viz také

Odkaz

System.Type

Attribute.GetCustomAttribute

Attribute.GetCustomAttributes

Koncepty

Rozšíření metadat pomocí atributů