VB.NET Attributes

Attribute Reference

Attributes are metadata you can apply to program elements (classes, methods, properties, etc.) to provide additional information to the compiler or runtime.

Attribute Target Description Example
Serializable Class, Structure Indicates that a type can be serialized.
<Serializable>
Public Class Person
    Public Property Name As String
    Public Property Age As Integer
End Class
Obsolete Any Marks program elements that should no longer be used.
<Obsolete("Use NewMethod instead.", True)>
Public Sub OldMethod()
    '...
End Sub
DebuggerStepThrough Method, Property, Constructor Instructs the debugger to step through the code without stopping.
<DebuggerStepThrough>
Public Sub Helper()
    '...
End Sub
DllImport Method Declares an external method implemented in an unmanaged DLL.
<DllImport("user32.dll", SetLastError:=True)>
Private Shared Function MessageBox(hWnd As IntPtr, text As String, caption As String, type As UInteger) As Integer
End Function
CLSCompliant Assembly, Module, Type, Member Indicates whether the code follows the Common Language Specification.
<Assembly: CLSCompliant(True)>
<CLSCompliant(False)>
Public Class MixedLanguage
    '...
End Class

Using Custom Attributes

You can define your own attribute by inheriting from Attribute.

Public Class DocumentationAttribute
    Inherits Attribute

    Public ReadOnly Property Url As String

    Public Sub New(url As String)
        Me.Url = url
    End Sub
End Class

<Documentation("https://example.com/api")>
Public Class ApiClient
    '...
End Class

Further Reading