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.

338 ❘ ChaPTer 14 reflectiOn<br />

can download from the Wrox web site at www.wrox.com) as a combined Visual Studio solution, as discussed<br />

in Chapter 16, “Visual Studio 2010.” The download includes the required Visual Studio 2010 solution files.<br />

The Whatsnewattributes library assembly<br />

This section starts with the core WhatsNewAttributes assembly. The source code is contained<br />

in the file WhatsNewAttributes.cs, which is located in the WhatsNewAttributes project of the<br />

WhatsNewAttributes solution in the example code for this chapter. The syntax for doing this is<br />

quite simple. At the comm<strong>and</strong> line, you supply the flag target:library to the compiler. To compile<br />

WhatsNewAttributes, type the following:<br />

csc /target:library WhatsNewAttributes.cs<br />

The WhatsNewAttributes.cs file defines two attribute classes, LastModifiedAttribute <strong>and</strong><br />

SupportsWhatsNewAttribute. The attribute LastModifiedAttribute is the attribute that you can<br />

use to mark when an item was last modified. It takes two m<strong>and</strong>atory parameters (parameters that are<br />

passed to the constructor): the date of the modification <strong>and</strong> a string containing a description of the changes.<br />

There is also one optional parameter named issues (for which a public property exists), which can be<br />

used to describe any outst<strong>and</strong>ing issues for the item.<br />

In practice, you would probably want this attribute to apply to anything. To keep the code simple, its usage<br />

is limited here to classes <strong>and</strong> methods. You will allow it to be applied more than once to the same item<br />

(AllowMultiple=true) because an item might be modified more than once, <strong>and</strong> each modification will<br />

have to be marked with a separate attribute instance.<br />

SupportsWhatsNew is a smaller class representing an attribute that doesn’t take any parameters. The<br />

idea of this attribute is that it is an assembly attribute that is used to mark an assembly for which you are<br />

maintaining documentation via the LastModifiedAttribute. This way, the program that will examine<br />

this assembly later on knows that the assembly it is reading is one on which you are actually using your<br />

automated documentation process. Here is the complete source code for this part of the example:<br />

using System;<br />

namespace WhatsNewAttributes<br />

{<br />

[AttributeUsage(<br />

AttributeTargets.Class | AttributeTargets.Method,<br />

AllowMultiple=true, Inherited=false)]<br />

public class LastModifiedAttribute: Attribute<br />

{<br />

private readonly DateTime _dateModified;<br />

private readonly string _changes;<br />

public LastModifiedAttribute(string dateModified, string changes)<br />

{<br />

dateModified = DateTime.Parse(dateModified);<br />

changes = changes;<br />

}<br />

public DateTime DateModified<br />

{<br />

get { return dateModified; }<br />

}<br />

public string Changes<br />

{<br />

get { return changes; }<br />

}<br />

public string Issues { get; set; }<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!