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

CHAPTER 24 REFLECTION AND ATTRIBUTES

More About Applying Attributes

The simple attributes shown so far have used a single attribute applied to a method. This section describes other types of attribute usage.

Multiple Attributes

You can apply multiple attributes to a single construct.

Multiple attributes can be listed in either of the following formats:

Separate attribute sections stacked on top of each other

A single attribute section, with the attributes separated by commas

You can list the attributes in any order.

For example, the following two sections of code show the two ways of applying multiple attributes. The sections of code are equivalent.

[ Serializable ]

 

// Stacked

[

MyAttribute("Simple class", "Version 3.57") ]

 

 

[

MyAttribute("Simple class", "Version 3.57"), Serializable ]

// Commas

 

 

 

 

 

 

Attribute

Attribute

 

651

CHAPTER 24 REFLECTION AND ATTRIBUTES

Other Types of Targets

Besides classes, you can also apply attributes to other program constructs such as fields and properties. The following declaration shows an attribute on a field, and multiple attributes on a method:

[MyAttribute("Holds a value", "Version 3.2")]

// On a field

public

int MyField;

 

[Obsolete]

// On a method

[MyAttribute("Prints out a message.", "Version 3.6")]

 

public

void PrintOut()

 

{

 

 

...

 

 

You can also explicitly label attributes to apply to a particular target construct. To use an explicit target, place the target type, followed by a colon, at the beginning of the attribute section. For example, the following code decorates the method with an attribute and also applies an attribute to the return value.

Explicit target

[method: MyAttribute("Prints out a message.", "Version 3.6")] [return: MyAttribute("This value represents ...", "Version 2.3")] public long ReturnSetting()

{

...

The C# language defines ten standard attribute targets, which are listed in Table 24-3. Most of the target names are self-explanatory, but type covers classes, structs, delegates, enums, and interfaces. The typevar target name specifies type parameters to constructs that use generics.

Table 24-3. Attribute Targets

event field method param property return type typevar assembly module

652

CHAPTER 24 REFLECTION AND ATTRIBUTES

Global Attributes

You can also use an explicit target to set attributes at the assembly and module level, by using the assembly and module target names. (Assemblies and modules were explained in Chapter 10.) Some important points about assembly-level attributes are the following:

Assembly-level attributes must be placed outside any namespace scope and are usually placed in the AssemblyInfo.cs file.

The AssembyInfo.cs file usually contains metadata about the company, product, and copyright information.

The following are lines from an AssemblyInfo.cs file:

[assembly: AssemblyTitle("SuperWidget")]

[assembly: AssemblyDescription("Implements the SuperWidget product.")] [assembly: AssemblyConfiguration("")]

[assembly: AssemblyCompany("McArthur Widgets, Inc.")] [assembly: AssemblyProduct("Super Widget Deluxe")]

[assembly: AssemblyCopyright("Copyright © McArthur Widgets 2010")] [assembly: AssemblyTrademark("")]

[assembly: AssemblyCulture("")]

653

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