15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Custom attributes ❘ 335<br />

error. For example, if the attribute class indicates that the attribute can be applied only to classes, but you<br />

have applied it to a struct definition, a compilation error will occur.<br />

To continue with the example, assume that you have defined the FieldName attribute like this:<br />

[AttributeUsage(AttributeTargets.Property,<br />

AllowMultiple=false,<br />

Inherited=false)]<br />

public class FieldNameAttribute: Attribute<br />

{<br />

private string name;<br />

public FieldNameAttribute(string name)<br />

{<br />

this.name = name;<br />

}<br />

}<br />

The following sections discuss each element of this definition.<br />

attributeUsage attribute<br />

The first thing to note is that the attribute class itself is marked with an attribute — the System<br />

.AttributeUsage attribute. This is an attribute defined by Microsoft for which the <strong>C#</strong> compiler provides<br />

special support. (You could argue that AttributeUsage isn’t an attribute at all; it is more like a<br />

meta-attribute, because it applies only to other attributes, not simply to any class.) The primary purpose<br />

of AttributeUsage is to identify the types of program elements to which your custom attribute can be<br />

applied. This information is given by the first parameter of the AttributeUsage attribute — this parameter<br />

is m<strong>and</strong>atory, <strong>and</strong> is of an enumerated type, AttributeTargets. In the previous example, you have<br />

indicated that the FieldName attribute can be applied only to properties, which is fine, because that is<br />

exactly what you have applied it to in the earlier code fragment. The members of the AttributeTargets<br />

enumeration are:<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

All<br />

Assembly<br />

Class<br />

Constructor<br />

Delegate<br />

Enum<br />

Event<br />

Field<br />

GenericParameter (.<strong>NET</strong> 2.0 <strong>and</strong> higher only)<br />

Interface<br />

Method<br />

Module<br />

Parameter<br />

Property<br />

ReturnValue<br />

Struct<br />

This list identifies all the program elements to which you can apply attributes. Note that when applying the<br />

attribute to a program element, you place the attribute in square brackets immediately before the element.<br />

However, two values in the preceding list do not correspond to any program element: Assembly <strong>and</strong><br />

Module. An attribute can be applied to an assembly or module as a whole instead of to an element in your<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!