Attributes Overview in Visual Basic

Attributes are descriptive tags that provide additional information about programming elements such as types, fields, methods, and properties. Other applications, such as the Visual Basic compiler, can refer to the extra information in attributes to determine how these items can be used.

Attributes are saved with the metadata of Visual Basic assemblies. Metadata is information that describes every element managed by the runtime. This includes information required for debugging and garbage collection, as well as security attributes, marshaling data, extended class and member definitions, version binding, and any other information the runtime requires.

With attributes, you specify the metadata in much the same way as you use keywords like Public and Private to provide information about access levels. However, unlike keywords, most attributes are not language-specific. Using attributes, you can extend the capabilities of the Visual Basic language without requiring changes to the compiler.

The .NET Framework and the Visual Basic language define many useful attributes, and you can define your own custom attributes that are meaningful to your application. Custom attributes are based on the System.Attribute class, and they use the AttributeUsageAttribute attribute to provide additional information about how the attribute can be used.

Some key points about attributes include:

  • You can apply one or more attributes to entire assemblies, modules, or smaller program elements such as classes and properties.

  • Attributes can accept arguments in the same way as methods and properties.

  • The process of retrieving metadata from attributes is called reflection. Reflection involves using tools that allow objects to retrieve and examine metadata about their own members. For more information, see Reflection Sample.

Community Additions