Chapter 8. Attributes
Attributes are declarative tags that can be used to annotate types or class members, thereby modifying their meaning or customizing their behavior. This descriptive information provided by the attribute is stored as metadata in a .NET assembly and can be extracted either at design time or at runtime using reflection.
To see how attributes might be used, consider the
<WebMethod>
attribute, which might appear in
code as follows:
<WebMethod(Description:="Indicates the number of visitors to a page")> _ Public Function PageHitCount(strULR As String) As Integer
Ordinarily, public methods of a class can be invoked locally from an
instance of that class; they are not treated as members of a web
service. In contrast, the <WebMethod>
attribute marks a method as a function callable over the Internet as
part of a web service. This <WebMethod>
attribute also includes a single property, Description, which
provides the text that will appear in the page describing the web
service.
You may wonder why attributes are used on the .NET platform and why they are not simply implemented as language elements. The answer comes from the fact that 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 through reflection both at design time (if a graphical environment such as Visual Studio .NET is used), at compile time (when the compiler can ...
Get VB.NET Language in a Nutshell, Second Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.