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
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
attribute marks a method as a function callable over the Internet as
part of a web service. This
attribute also includes a single property, Description, which
provides the text that will appear in the page describing the web
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 ...