Attribútumokban tárolt információk lekérése
Az egyéni attribútumok lekérése egyszerű folyamat. Először deklarálja a lekérni kívánt attribútum egy példányát. Ezután a Attribute.GetCustomAttribute metódus használatával inicializálhatja az új attribútumot a lekérni kívánt attribútum értékéhez. Az új attribútum inicializálása után a tulajdonságaival lekérheti az értékeket.
Fontos
Ez a cikk bemutatja, hogyan kérhetők le a végrehajtási környezetbe betöltött kód attribútumai. A csak tükrözési környezetbe betöltött kód attribútumainak lekéréséhez az osztályt kell használnia, ahogyan az CustomAttributeData a Szerelvények betöltése a Önkifejezés ion-Only környezetbe című cikkben látható.
Ez a szakasz az attribútumok lekérésének alábbi módjait ismerteti:
Egy adott hatókörre alkalmazott attribútum több példányának beolvasása
Attribútum több példányának beolvasása különböző hatókörökre
Attribútum egyetlen példányának beolvasása
Az alábbi példában a rendszer az DeveloperAttribute
osztály szintjén az osztályra MainApp
alkalmazza az (előző szakaszban leírt) osztályt. A GetAttribute
metódus az osztályszinten tárolt DeveloperAttribute
értékek lekérésére használhatóGetCustomAttribute
, mielőtt megjelenítené őket a konzolon.
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);
}
}
};
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);
}
}
}
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
Az előző program végrehajtása a következő szöveget jeleníti meg:
The Name Attribute is: Joan Smith.
The Level Attribute is: 42.
The Reviewed Attribute is: True.
Ha az attribútum nem található, a GetCustomAttribute
metódus null értékűre inicializálható MyAttribute
. Ez a példa egy ilyen példányt keres MyAttribute
, és értesíti a felhasználót, ha az attribútum nem található. Ha DeveloperAttribute
nem található az osztály hatókörében, a konzol a következő üzenetet jeleníti meg:
The attribute was not found.
Az előző példa feltételezi, hogy az attribútumdefiníció az aktuális névtérben található. Ne felejtse el importálni azt a névteret, amelyben az attribútumdefiníció található, ha az nem az aktuális névtérben található.
Ugyanazon hatókörre alkalmazott attribútum több példányának beolvasása
Az előző példában a vizsgálandó osztály és a megtalálandó attribútum a metódusnak GetCustomAttribute lesz átadva. Ez a kód akkor működik jól, ha egy attribútumnak csak egy példánya van alkalmazva az osztály szintjén. Ha azonban egy attribútum több példánya is ugyanazon az osztályszinten van alkalmazva, a GetCustomAttribute
metódus nem kéri le az összes információt. Azokban az esetekben, amikor ugyanazon attribútum több példánya is ugyanarra a hatókörre van alkalmazva, metódussal Attribute.GetCustomAttributes az attribútum összes példányát tömbbe helyezheti. Ha például ugyanazon osztály osztályszintjén két példány DeveloperAttribute
van alkalmazva, a GetAttribute
metódus módosítható a két attribútumban található információk megjelenítéséhez. Ne feledje, hogy több attribútumot kell alkalmaznia ugyanazon a szinten. Az attribútumot az AllowMultiple
osztályban AttributeUsageAttribute beállított tulajdonsággal kell definiálnitrue
.
Az alábbi példakód bemutatja, hogyan hozhat létre olyan tömböt, GetCustomAttributes
amely egy adott osztály összes példányára DeveloperAttribute
hivatkozik. A kód ezután megjeleníti az összes attribútum értékét a konzolon.
public:
static void GetAttribute(Type^ t)
{
array<DeveloperAttribute^>^ MyAttributes =
(array<DeveloperAttribute^>^) Attribute::GetCustomAttributes(t, DeveloperAttribute::typeid);
if (MyAttributes->Length == 0)
{
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)
{
DeveloperAttribute[] MyAttributes =
(DeveloperAttribute[]) Attribute.GetCustomAttributes(t, typeof (DeveloperAttribute));
if (MyAttributes.Length == 0)
{
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 Shared Sub GetAttribute(t As Type)
Dim MyAttributes() As DeveloperAttribute =
CType(Attribute.GetCustomAttributes(t, GetType(DeveloperAttribute)), DeveloperAttribute())
If MyAttributes.Length = 0 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
Ha nem található attribútum, ez a kód értesíti a felhasználót. Ellenkező esetben a két példányban DeveloperAttribute
található információk megjelennek.
Attribútum több példányának beolvasása különböző hatókörökre alkalmazva
A GetCustomAttributes metódusok nem GetCustomAttribute keresnek egy teljes osztályt, és az adott osztályban lévő attribútum összes példányát adják vissza. Ehelyett egyszerre csak egy megadott metódusban vagy tagban keresnek. Ha minden tagra ugyanazzal az attribútummal rendelkező osztályt alkalmaz, és a tagokra alkalmazott összes attribútum értékeit le szeretné kérni, minden metódust vagy tagot egyenként kell megadnia a tagoknak és GetCustomAttribute
a tagoknakGetCustomAttributes
.
Az alábbi példakód egy osztályt vesz fel paraméterként, és az DeveloperAttribute
osztály szintjén és az osztály minden egyes metódusán megkeresi a (korábban definiált) osztályt:
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);
}
}
}
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 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
Ha a DeveloperAttribute
metódus szintjén vagy osztályszinten nem találhatók példányok, a metódus értesíti a GetAttribute
felhasználót, hogy nem találhatók attribútumok, és megjeleníti annak a metódusnak vagy osztálynak a nevét, amely nem tartalmazza az attribútumot. Ha attribútumot talál, a konzol megjeleníti a , Level
és Reviewed
a Name
mezőket.
Az osztály tagjaival lekérheti az Type egyes metódusokat és tagokat az átadott osztályban. Ez a példa először lekérdezi az objektumot az Type
osztályszint attribútumadatainak lekéréséhez. Type.GetMethods Ezután az összes metódus példányait objektumtömbbe System.Reflection.MemberInfo helyezi, hogy lekérje a metódusszint attribútumadatait. A metódus használatával Type.GetProperties attribútumokat is kereshet a tulajdonságszinten, vagy Type.GetConstructors a konstruktor szintjén is kereshet attribútumokat.
Lásd még
Visszajelzés
https://aka.ms/ContentUserFeedback.
Hamarosan elérhető: 2024-ben fokozatosan kivezetjük a GitHub-problémákat a tartalom visszajelzési mechanizmusaként, és lecseréljük egy új visszajelzési rendszerre. További információ:Visszajelzés küldése és megtekintése a következőhöz: