Attribute.GetCustomAttributes Method (Module, Type, Boolean)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Retrieves an array of the custom attributes applied to a module. Parameters specify the module, the type of the custom attribute to search for, and an ignored search option.

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

Public Shared Function GetCustomAttributes (
	element As Module,
	attributeType As Type,
	inherit As Boolean
) As Attribute()


Type: System.Reflection.Module

An object derived from the Module class that describes a portable executable file.

Type: System.Type

The type, or a base type, of the custom attribute to search for.

Type: System.Boolean

This parameter is ignored, and does not affect the operation of this method.

Return Value

Type: System.Attribute()

An Attribute array that contains the custom attributes of type attributeType applied to element, or an empty array if no such custom attributes exist.

Exception Condition

element or attributeType is null.


attributeType is not derived from Attribute.

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

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

Imports System
Imports System.Reflection
Imports System.ComponentModel

' Give the Module some attributes.
<Module: Description("A sample description")> 

' Make the CLSCompliant attribute False.
' The CLSCompliant attribute is applicable for /target:module.
<Module: CLSCompliant(False)> 

Module DemoModule

    Sub Main()
        ' Get the Module type to access its metadata.
        Dim modType As Reflection.Module = GetType(DemoModule).Module
        Dim attr As Attribute
        ' Iterate through all the attributes for the module.
        For Each attr In Attribute.GetCustomAttributes(modType)
            ' Check for the Description attribute.
            If TypeOf attr Is DescriptionAttribute Then
                ' Convert the attribute to access its data.
                Dim descAttr As DescriptionAttribute = _
                    CType(attr, DescriptionAttribute)
                Console.WriteLine("Module {0} has the description ""{1}"".", _
                    modType.Name, descAttr.Description)

            ' Check for the CLSCompliant attribute.
            ElseIf TypeOf attr Is CLSCompliantAttribute Then
                ' Convert the attribute to access its data.
                Dim CLSCompAttr As CLSCompliantAttribute = _
                    CType(attr, CLSCompliantAttribute)
                Dim strCompliant As String
                If CLSCompAttr.IsCompliant Then
                    strCompliant = "is"
                    strCompliant = "is not"
                End If
                Console.WriteLine("Module {0} {1} CLSCompliant.", _
                    modType.Name, strCompliant)
            End If
    End Sub
End Module

' Output:
' Module CustAttrs2VB.exe has the description "A sample description".
' Module CustAttrs2VB.exe is not CLSCompliant.

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