
122
|
Chapter 9: Attributes
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
language constructs instead of metadata-generating attributes, this level of flexi-
bility 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. ...