Attribute.GetCustomAttributes Method (Module, Type, Boolean)

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)

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

Parameters

element
Type: System.Reflection.Module

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

attributeType
Type: System.Type

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

inherit
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.

ExceptionCondition
ArgumentNullException

element or attributeType is Nothing.

ArgumentException

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" 
                Else
                    strCompliant = "is not" 
                End If
                Console.WriteLine("Module {0} {1} CLSCompliant.", _
                    modType.Name, strCompliant)
            End If 
        Next 
    End Sub 
End Module 

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

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft