Chapter 9. Attributes


Attributes are declarative tags that can be used to annotate types (classes, structures, etc.) or type members, thereby modifying their meaning or customizing their behavior. The descriptive information provided by each attribute is stored as metadata in the .NET assembly and can be extracted either at design time or at runtime using a process called reflection.

Consider the following example that uses the <WebMethod> attribute.

     <WebMethod(Description:="Indicates number of visitors to a page")> _     Public Function PageHitCount(baseURL As String) As Integer 

Ordinarily, public methods of a class can be invoked locally through an instance of that class; they are not treated as members of a web service. The <WebMethod> attribute marks an ordinary class method so that it is callable over the Internet as part of a web service. This <WebMethod> attribute includes a single property, Description, which provides the text that will appear on the page describing the web service.

If attributes provide such important software features, why aren't they simply implemented as language elements? The answer is "flexibility." Attributes are stored as metadata in an assembly, rather than as part of its executable code. As an item of metadata, the attribute describes the program element to which it applies and is available for retrieval and examination at design time (when using a tool like Visual Studio that recognizes attributes), at compile time (when the compiler can use attributes to modify, customize, or extend the compiler's basic operation), and at runtime (when it can be used by the Common Language Runtime to modify the code's ordinary runtime behavior).

Since metadata in an assembly can be examined by other applications, third-party tools can take advantage of the attributes included in your code. A third-party tool may make available an attribute previously unavailable with Visual Studio, and you can begin to use its enhanced behavior without having to wait for Microsoft to update the language or the compiler. If these features were implemented as language constructs instead of metadata-generating attributes , this level of flexibility would be difficult or impossible.

The behavior of interface objects (such as Windows Forms controls) in Visual Studio illustrates the importance of attributes. Since Visual Studio offers drag-and-drop placement of controls on forms or web pages, it is necessary for controls to have a design time behavior in addition to their runtime behavior. For instance, when you double-click on a control in Visual Studio (at design time), the code or code template for its default event handler appears. How does Visual Studio know which event handler is the default? An attribute provides the solution. Visual Studio recognizes an attribute named <DefaultEvent>, which provides the control designer with a way to indicate a control's default event. Since the attribute's information is stored in the assembly's metadata, Visual Studio can simply examine the control's metadata to see whether a <DefaultEvent> attribute is attached to a particular event.

The attribute-based system of programming implemented in .NET is extensible. In addition to the attributes predefined by Visual Basic, the .NET Framework, or other vendors, you can define custom attributes that you can then apply to program elements. For an attribute to be meaningful, there must also be software that attempts to detect the presence of the attribute somewhere in your code's lifetime (design time, compile time, runtime) and that acts based on the attribute's settings.




Visual Basic 2005(c) In a Nutshell
Visual Basic 2005 in a Nutshell (In a Nutshell (OReilly))
ISBN: 059610152X
EAN: 2147483647
Year: 2004
Pages: 712

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net