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.

334 ❘ ChaPTer 14 reflectiOn<br />

CusTom aTTribuTes<br />

From this book, you have seen how you can define attributes on various items within your program. These<br />

attributes have been defined by Microsoft as part of the .<strong>NET</strong> Framework class library, <strong>and</strong> many of them<br />

receive special support from the <strong>C#</strong> compiler. This means that for those particular attributes, the compiler<br />

could customize the compilation process in specific ways; for example, laying out a struct in memory<br />

according to the details in the StructLayout attributes.<br />

The .<strong>NET</strong> Framework also allows you to define your own attributes. Clearly, these attributes will not have<br />

any effect on the compilation process, because the compiler has no intrinsic awareness of them. However,<br />

these attributes will be emitted as metadata in the compiled assembly when they are applied to program<br />

elements.<br />

By itself, this metadata might be useful for documentation purposes, but what makes attributes really<br />

powerful is that by using reflection, your code can read this metadata <strong>and</strong> use it to make decisions at<br />

runtime. This means that the custom attributes that you define can directly affect how your code runs.<br />

For example, custom attributes can be used to enable declarative code access security checks for custom<br />

permission classes, to associate information with program elements that can then be used by testing tools,<br />

or when developing extensible frameworks that allow the loading of plug-ins or modules.<br />

Writing Custom attributes<br />

To underst<strong>and</strong> how to write your own custom attributes, it is useful to know what the compiler does when it<br />

encounters an element in your code that has a custom attribute applied to it. To take the database example,<br />

suppose that you have a <strong>C#</strong> property declaration that looks like this:<br />

[FieldName("SocialSecurityNumber")]<br />

public string SocialSecurityNumber<br />

{<br />

get {<br />

// etc.<br />

When the <strong>C#</strong> compiler recognizes that this property has an attribute applied to it (FieldName), it will start<br />

by appending the string Attribute to this name, forming the combined name FieldNameAttribute. The<br />

compiler will then search all the namespaces in its search path (those namespaces that have been mentioned<br />

in a using statement) for a class with the specified name. Note that if you mark an item with an attribute<br />

whose name already ends in the string Attribute, the compiler will not add the string to the name a second<br />

time; it will leave the attribute name unchanged. Therefore, the preceding code is equivalent to this:<br />

[FieldNameAttribute("SocialSecurityNumber")]<br />

public string SocialSecurityNumber<br />

{<br />

get {<br />

// etc.<br />

The compiler expects to find a class with this name, <strong>and</strong> it expects this class to be derived directly or<br />

indirectly from System.Attribute. The compiler also expects that this class contains information that<br />

governs the use of the attribute. In particular, the attribute class needs to specify the following:<br />

➤<br />

➤<br />

➤<br />

➤<br />

The types of program elements to which the attribute can be applied (classes, structs, properties,<br />

methods, <strong>and</strong> so on)<br />

Whether it is legal for the attribute to be applied more than once to the same program element<br />

Whether the attribute, when applied to a class or interface, is inherited by derived classes <strong>and</strong><br />

interfaces<br />

The m<strong>and</strong>atory <strong>and</strong> optional parameters the attribute takes<br />

If the compiler cannot find a corresponding attribute class, or if it finds one but the way that you have used<br />

that attribute does not match the information in the attribute class, the compiler will raise a compilation<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!