Программирование на Visual C++. Архив рассылки
Шрифт:
Console.WriteLine("Имя: {0}." , MyAttribute[i].Name);
// Получить поле Уровень.
Console.WriteLine("Уровень: {0}." , MyAttribute[i].Level);
// Получить поле Проверено.
Console.WriteLine("Проверено: {0}.", MyAttribute[i].Reviewed);
}
}
MC++
public:
static void GetAttribute(Type* t) {
// Получить атрибут.
DeveloperAttribute* MyAttribute __gc[] =
__try_cast<DeveloperAttribute* __gc[]>(Attribute::GetCustomAttributes(t, __typeof(DeveloperAttribute)));
if (MyAttribute == 0) Console::WriteLine(S"Атрибут
не найден.");
else for (int i = 0; i < MyAttribute.Length; i++) {
// Получить поле Имя.
Console::WriteLine(S"Имя: {0}." , MyAttribute[i]->Name);
// Получить поле Уровень.
Console::WriteLine(S"Уровень: {0}." , MyAttribute[i]->Level);
// Получить поле Проверено.
Console::WriteLine(S"Проверено: {0}." , MyAttribute[i]->Reviewed);
}
}
Visual Basic.NET
Public Shared Sub GetAttribute(t As Type)
' Получить атрибут.
Dim MyAttribute As DeveloperAttribute = _
CType(Attribute.GetCustomAttributes(t, GetType(DeveloperAttribute)), DeveloperAttribute)
If MyAttribute Is Nothing Then
Console.WriteLine("Атрибут не найден.")
Else
Dim i As Integer
For i = 0 To MyAttribute.Length – 1 ' Получить поле Имя.
Console.WriteLine("Имя: {0}.", MyAttribute(i).Name)
' Получить поле Уровень.
Console.WriteLine("Уровень: {0}.", MyAttribute(i).Level)
' Получить поле Проверено.
Console.WriteLine("Проверено: {0}.", MyAttribute(i).Reviewed)
Next i
End If
End Sub
Получение списка разнотипных атрибутов
Методы GetCustomAttribute и GetCustomAttributes не могут искать атрибут во всем классе и возвращать все его экземпляры. Они просматривают только один метод или поле за раз. Поэтому, если есть класс с одним атрибутом для всех методов и нужно получить все экземпляры этого атрибута, не остается ничего делать, как передавать эти методы один за другим в качестве параметров GetCustomAttribute и GetCustomAttributes.
В следующем фрагменте кода показано, как получить все экземпляры атрибута DeveloperAttribute, определенного как на уровне класса, так и на уровне методов.
C#
using System;
using System.Reflection;
public static void GetAttribute(Type t) {
//
Получить атрибут уровня класса.
DeveloperAttribute att =
(DeveloperAttribute) Attribute.GetCustomAttribute (t, typeof(DeveloperAttribute));
if (att == null)
Console.WriteLine("Класс {0} не имеет атрибута Developer.\n", t.ToString);
else {
Console.WriteLine("Атрибут Имя на уровне класса: {0}.", att.Name);
Console.WriteLine("Атрибут Уровень на уровне класса: {0}.", att.Level);
Console.WriteLine("Атрибут Проверено на уровне класса: {0}.\n", att.Reviewed);
}
// Получить атрибуты уровня методов.
// Получить все методы данного класса и поместить их
// в массив объектов System.Reflection.MemberInfo.
MemberInfo[] MyMemberInfo = t.GetMethods;
// Вывести атрибуты всех методов класса
for (int i = 0; i < MyMemberInfo.Length; i++) {
att =
(DeveloperAttribute)Attribute.GetCustomAttribute(MyMemberInfo[i], typeof (DeveloperAttribute));
if (att == null)
Console.WriteLine("Метод {0} не имеет атрибута Developer.\n" ,
MyMemberInfo[i].ToString);
else {
Console.WriteLine("Атрибут Имя на уровне метода {0}: {1}.", MyMemberInfo[i].ToString, att.Name);
Console.WriteLine("Атрибут Уровень на уровне метода {0}: {1}.", MyMemberInfo[i].ToString, att.Level);
Console.WriteLine("Атрибут Проверено на уровне метода {0}: {1}.\n", MyMemberInfo[i].ToString, att.Reviewed);
}
}
}
MC++
using namespace System;
using namespace System::Reflection;
public:
static void GetAttribute(Type* t) {
// Получить атрибут уровня класса.
DeveloperAttribute* att = __try_cast<DeveloperAttribute*>(Attribute::GetCustomAttribute(t, __typeof(DeveloperAttribute)));
if (att == 0)
Console::WriteLine(S"Класс {0} не имеет атрибута Developer.\n", t->ToString);
else {
Console::WriteLine(S"Атрибут Имя на уровне класса: {0}.", att->Name);
Console::WriteLine(S"Атрибут Уровень на уровне класса: {0}.", att->Level);
Console::WriteLine(S"Атрибут Проверено на уровне класса: {0}.\n", att->Reviewed);
}
// Получить атрибуты уровня методов.
Поделиться с друзьями: