Export (0) Print
Expand All

Attribute.IsDefined Method (Module, Type, Boolean)

Determines whether any custom attributes are 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 IsDefined ( _
	element As Module, _
	attributeType As Type, _
	inherit As Boolean _
) As Boolean

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.Boolean
true if a custom attribute of type attributeType is applied to element; otherwise, false.

ExceptionCondition
ArgumentNullException

element or attributeType is Nothing.

ArgumentException

attributeType is not derived from Attribute.

This method ignores the inherit parameter and does not search the ancestors of element for custom attributes.

The following code example illustrates the use of IsDefined, 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
Show:
© 2015 Microsoft