15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Using reflection ❘ 345<br />

finding out about Custom attributes<br />

The methods you use to fi nd out which custom attributes are defi ned on an assembly or type depend<br />

on what type of object the attribute is attached to. If you want to fi nd out what custom attributes<br />

are attached to an assembly as a whole, you need to call a static method of the Attribute class,<br />

GetCustomAttributes() , passing in a reference to the assembly:<br />

Attribute[] definedAttributes =<br />

Attribute.GetCustomAttributes(assembly1);<br />

// assembly1 is an Assembly object<br />

This is actually quite signifi cant. You may have wondered why, when you defi ned<br />

custom attributes, you had to go to all the trouble of actually writing classes for them,<br />

<strong>and</strong> why Microsoft hadn’t come up with some simpler syntax. Well, the answer is<br />

here. The custom attributes do genuinely exist as objects, <strong>and</strong> when an assembly<br />

is loaded you can read in these attribute objects, examine their properties, <strong>and</strong> call<br />

their methods.<br />

GetCustomAttributes() , which is used to get assembly attributes, has a few overloads. If you call it<br />

without specifying any parameters other than an assembly reference, it will simply return all the custom<br />

attributes defi ned for that assembly. You can also call GetCustomAttributes() specifying a second<br />

parameter, which is a Type object that indicates the attribute class in which you are interested. In this<br />

case, GetCustomAttributes() returns an array consisting of all the attributes present that are of the<br />

specifi ed type.<br />

Note that all attributes are retrieved as plain Attribute references. If you want to call any of the methods<br />

or properties you defi ned for your custom attributes, you will need to cast these references explicitly to the<br />

relevant custom attribute classes. You can obtain details of custom attributes that are attached to a given<br />

data type by calling another overload of Assembly.GetCustomAttributes() , this time passing a Type<br />

reference that describes the type for which you want to retrieve any attached attributes. If you want to<br />

obtain attributes that are attached to methods, constructors, fi elds, <strong>and</strong> so on, however, you will need to call<br />

a GetCustomAttributes() method that is a member of one of the classes MethodInfo , ConstructorInfo ,<br />

FieldInfo , <strong>and</strong> so on.<br />

If you expect only a single attribute of a given type, you can call the GetCustomAttribute()<br />

method instead, which returns a single Attribute object. You will use GetCustomAttribute()<br />

in the WhatsNewAttributes example to fi nd out whether the SupportsWhatsNew attribute is<br />

present in the assembly. To do this, you call GetCustomAttribute() , passing in a reference to the<br />

WhatsNewAttributes assembly, <strong>and</strong> the type of the SupportsWhatsNewAttribute attribute. If this<br />

attribute is present, you get an Attribute instance. If no instances of it are defi ned in the assembly,<br />

you get null . And if two or more instances are found, GetCustomAttribute() throws a System<br />

.Reflection.AmbiguousMatchException .<br />

Attribute supportsAttribute =<br />

Attribute.GetCustomAttributes(assembly1,<br />

typeof(SupportsWhatsNewAttribute));<br />

Completing the Whatsnewattributes example<br />

You now have enough information to complete the WhatsNewAttributes example by writing the source<br />

code for the fi nal assembly in the sample, the LookUpWhatsNew assembly. This part of the application is<br />

a console application. However, it needs to reference the other assemblies of WhatsNewAttributes <strong>and</strong><br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!