GetCustomAttributes Method (Type, Boolean)
Collapse the table of content
Expand the table of content

Module.GetCustomAttributes Method (Type, Boolean)


Gets custom attributes of the specified type.

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

Public Overridable Function GetCustomAttributes (
	attributeType As Type,
	inherit As Boolean
) As Object()


Type: System.Type

The type of attribute to get.

Type: System.Boolean

This argument is ignored for objects of this type.

Return Value

Type: System.Object()

An array of type Object containing all custom attributes of the specified type.

Exception Condition

attributeType is null.


attributeType is not a Type object supplied by the runtime. For example, attributeType is a TypeBuilder object.

The following example displays the module names of the specified type that match the specified search criteria.

Imports System.Reflection

' Define a module-level attribute.
<Module: ReflectionModule_Examples.MySimpleAttribute("module-level")> 
' This code assumes that the root namespace is set to empty("").
Namespace ReflectionModule_Examples
    Class MyMainClass
        Shared Sub Main()
            Dim moduleArray() As [Module]
            moduleArray = GetType(MyMainClass).Assembly.GetModules(False)
            ' In a simple project with only one module, the module at index
            ' 0 will be the module containing these classes.
            Dim myModule As [Module] = moduleArray(0)
            Dim attributes() As Object
            ' Get only MySimpleAttribute attributes for this module.
            attributes = myModule.GetCustomAttributes( _
                myModule.GetType("ReflectionModule_Examples.MySimpleAttribute", _
                False, False), True)
            Dim o As [Object]
            For Each o In attributes
                Console.WriteLine("Found this attribute on myModule: {0}", o.ToString())
            Next o
        End Sub 'Main
    End Class 'MyMainClass
    ' Define a very simple custom attribute.
    <AttributeUsage(AttributeTargets.Class Or AttributeTargets.Module)> _
     Public Class MySimpleAttribute
        Inherits Attribute
        Private name As String
        Public Sub New(ByVal newName As String)
            name = newName
        End Sub 'NewNew
    End Class 'MySimpleAttribute
End Namespace 'ReflectionModule_Examples

.NET Framework
Available since 1.1
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top
© 2016 Microsoft