Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 24 REFLECTION AND ATTRIBUTES

Accessing an Attribute

At the beginning of the chapter, you saw that you can access information about a type using its Type object. You can access custom attributes in the same way. There are two methods of Type that are particularly useful in this: IsDefined and GetCustomAttributes.

Using the IsDefined Method

You can use the IsDefined method of the Type object to determine whether a particular attribute is applied to a particular class.

For example, the following code declares an attributed class called MyClass and also acts as its own attribute consumer by accessing an attribute declared and applied in the program itself. At the top of the code are declarations of the attribute ReviewComment and the class MyClass, to which it is applied. The code does the following:

First, Main creates an object of the class. It then retrieves a reference to the Type object by using the GetType method, which it inherited from its base class, object.

With the reference to the Type object, it can call the IsDefined method to find out whether attribute ReviewComment is applied to this class.

The first parameter takes a Type object of the attribute you are checking for.

The second parameter is of type bool and specifies whether to search the inheritance tree of MyClass to find the attribute.

[AttributeUsage(AttributeTargets.Class)]

public sealed class ReviewCommentAttribute : System.Attribute { ... }

[ReviewComment("Check it out", "2.4")] class MyClass { }

class Program {

 

 

 

 

 

static void

Main() {

 

 

 

 

MyClass mc = new MyClass();

// Create an

instance

of the class.

Type

t =

mc.GetType();

//

Get the Type object from the instance.

bool

isDefined =

//

Check the

Type for

the attribute.

t.IsDefined(typeof(ReviewCommentAttribute), false);

if( isDefined )

Console.WriteLine("ReviewComment is applied to type {0}", t.Name);

}

}

This code produces the following output:

ReviewComment is applied to type MyClass

661

CHAPTER 24 REFLECTION AND ATTRIBUTES

Using the GetCustomAttributes Method

The GetCustomAttributes method returns an array of the attributes applied to a construct.

The actual object returned is an array of objects, which you must then cast to the correct attribute type.

The Boolean parameter specifies whether to search the inheritance tree to find the attribute. object[] AttArr = t.GetCustomAttributes(false);

When the GetCustomAttributes method is called, an instance of each attribute associated with the target is created.

The following code uses the same attribute and class declarations as the previous example. But in this case, it doesn’t just determine whether an attribute is applied to the class. Instead, it retrieves an array of the attributes applied to the class and cycles through them, printing out their member values.

static void Main()

{

Type t = typeof(MyClass);

object[] AttArr = t.GetCustomAttributes(false);

foreach (Attribute a in AttArr)

{

ReviewCommentAttribute attr = a as ReviewCommentAttribute; if (null != attr)

{

Console.WriteLine("Description

: {0}", attr.Description);

Console.WriteLine("Version Number

: {0}",

attr.VersionNumber);

Console.WriteLine("Reviewer ID

: {0}",

attr.ReviewerID);

}

}

}

This code produces the following output:

Description

: Check it out

Version Number :

2.4

Reviewer ID

:

 

662

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]