Attribute.GetCustomAttribute Method (Module, Type, Boolean)

Retrieves a custom attribute 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 GetCustomAttribute ( _
	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
A reference to the single custom attribute of type attributeType that is applied to element, or Nothing if there is no such attribute.

ExceptionCondition
ArgumentNullException

element or attributeType is Nothing.

ArgumentException

attributeType is not derived from Attribute.

AmbiguousMatchException

More than one of the requested attributes was found.

The following code example illustrates the use of the GetCustomAttribute method taking a Module as a parameter.

Imports System
Imports System.Reflection
Imports System.Diagnostics

' Add the Debuggable attribute to the module.
<Module: Debuggable(True, False)> 

Module DemoModule
    Sub Main()
        ' Get the module type information to access its metadata. 
        Dim modType As Type = GetType(DemoModule)
        ' See if the Debuggable attribute is defined. 
        Dim isDef As Boolean = Attribute.IsDefined(modType.Module, _
                               GetType(DebuggableAttribute))
        Dim strDef As String 
        If isDef = True Then
            strDef = "is" 
        Else
            strDef = "is not" 
        End If 
        ' Display the result
        Console.WriteLine("The debuggable attribute {0} defined for " & _
                          "module {1}.", strDef, modType.Name)
        ' If the attribute is defined, display the JIT settings. 
        If isDef = True Then 
            ' Retrieve the attribute itself. 
            Dim attr As Attribute = _
                Attribute.GetCustomAttribute(modType.Module, _
                GetType(DebuggableAttribute))
            If Not attr Is Nothing And TypeOf attr Is DebuggableAttribute Then 
                Dim dbgAttr As DebuggableAttribute = _
                    CType(attr, DebuggableAttribute)
                Console.WriteLine("JITTrackingEnabled is {0}.", _
                    dbgAttr.IsJITTrackingEnabled.ToString())
                Console.WriteLine("JITOptimizerDisabled is {0}.", _
                    dbgAttr.IsJITOptimizerDisabled.ToString())
            Else
                Console.WriteLine("The Debuggable attribute could " & _
                                  "not be retrieved.")
            End If 
        End If 
    End Sub 
End Module 

' Output: 
' The debuggable attribute is defined for module DemoModule. 
' JITTrackingEnabled is True. 
' JITOptimizerDisabled is False.

.NET Framework

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

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft