This documentation is archived and is not being maintained.

Attribute.GetCustomAttributes Method (MemberInfo, Boolean)

Retrieves an array of the custom attributes applied to a member of a type. Parameters specify the member, the type of the custom attribute to search for, and whether to search ancestors of the member.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Public Shared Function GetCustomAttributes ( _
	element As MemberInfo, _
	inherit As Boolean _
) As Attribute()
Dim element As MemberInfo 
Dim inherit As Boolean 
Dim returnValue As Attribute()

returnValue = Attribute.GetCustomAttributes(element, _


Type: System.Reflection.MemberInfo

An object derived from the MemberInfo class that describes a constructor, event, field, method, or property member of a class.

Type: System.Boolean

If true, specifies to also search the ancestors of element for custom attributes.

Return Value

Type: System.Attribute()
An Attribute array that contains the custom attributes applied to element, or an empty array if no such custom attributes exist.


element is Nothing.


element is not a constructor, method, property, event, type, or field.


A custom attribute type cannot be loaded.

The return value contains the custom attributes for ancestors of element if inherit is true.


Starting with the .NET Framework version 2.0, this method returns security attributes on methods, constructors, and types if the attributes are stored in the new metadata format. Assemblies compiled with version 2.0 or later use the new format. Dynamic assemblies and assemblies compiled with earlier versions of the .NET Framework use the old XML format. See Emitting Declarative Security Attributes.

The following code example demonstrates the use of GetCustomAttributes, taking a MemberInfo as a parameter.

Imports System
Imports System.Reflection
Imports System.Security
Imports System.Runtime.InteropServices

' Define an enumeration of Win32 unmanaged types 
Public Enum UnmanagedType
End Enum 'UnmanagedType

' Define the Unmanaged attribute. 
Public Class UnmanagedAttribute 
             Inherits Attribute

    ' Storage for the UnmanagedType value. 
    Protected thisType As UnmanagedType

    ' Set the unmanaged type in the constructor. 
    Public Sub New(ByVal type As UnmanagedType) 
        thisType = type
    End Sub 'New 

    ' Define a property to get and set the UnmanagedType value. 
    Public Property Win32Type() As UnmanagedType 
            Return thisType
        End Get 
            thisType = Win32Type
        End Set 
    End Property 
End Class 'UnmanagedAttribute 

' Create a class for an imported Win32 unmanaged function. 
Public Class Win32
    <DllImport("user32.dll", CharSet:=CharSet.Unicode)> _
    Public Shared Function MessageBox(ByVal hWnd As Integer, _
                                      ByVal Text As String, _
                                      ByVal caption As String, _
                                      ByVal type As Integer) As Integer 
    End Function 'MessageBox
End Class 'Win32

Public Class AClass
    ' Add some attributes to Win32CallMethod.
    <Obsolete("This method is obsolete. Use managed MsgBox instead."), _
     Unmanaged(UnmanagedType.User)>  _
    Public Sub Win32CallMethod() 
        Win32.MessageBox(0, "This is an unmanaged call.", "Caution!", 0)    
    End Sub 'Win32CallMethod
End Class 'AClass

Class DemoClass
    Shared Sub Main(ByVal args() As String) 
        ' Get the AClass type to access its metadata. 
        Dim clsType As Type = GetType(AClass)
        ' Get the type information for Win32CallMethod. 
        Dim mInfo As MethodInfo = clsType.GetMethod("Win32CallMethod")
        If Not (mInfo Is Nothing) Then 
            ' Iterate through all the attributes of the method. 
            Dim attr As Attribute
            For Each attr In  Attribute.GetCustomAttributes(mInfo)
                ' Check for the Obsolete attribute. 
                If attr.GetType().Equals(GetType(ObsoleteAttribute)) Then
                    Console.WriteLine("Method {0} is obsolete. The message is:", mInfo.Name)
                    Console.WriteLine("  ""{0}""", CType(attr, ObsoleteAttribute).Message)
                ' Check for the Unmanaged attribute. 
                ElseIf attr.GetType().Equals(GetType(UnmanagedAttribute)) Then
                    Console.WriteLine("This method calls unmanaged code.")
                    Console.WriteLine( _
                            String.Format("The Unmanaged attribute type is {0}.", _
                            CType(attr, UnmanagedAttribute).Win32Type))
                    Dim myCls As New AClass()
                End If 
            Next attr
        End If 
    End Sub 'Main
End Class 'DemoClass

'This code example produces the following results.  

'First, the compilation yields the warning, "...This method is  
'obsolete. Use managed MsgBox instead." 
'Second, execution yields a message box with a title of "Caution!"  
'and message text of "This is an unmanaged call."  
'Third, the following text is displayed in the console window: 

'Method Win32CallMethod is obsolete. The message is: 
'  "This method is obsolete. Use managed MsgBox instead." 
'This method calls unmanaged code. 
'The Unmanaged attribute type is User. 

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0