Attribute.GetCustomAttributes Method (Module)


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. A parameter specifies the module.

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

Public Shared Function GetCustomAttributes (
	element As Module
) As Attribute()


Type: System.Reflection.Module

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

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.

Exception Condition

element is null.

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