IsDefined Method

Module.IsDefined Method (Type, Boolean)


Returns a value that indicates whether the specified attribute type has been applied to this module.

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

Public Overridable Function IsDefined (
	attributeType As Type,
	inherit As Boolean
) As Boolean


Type: System.Type

The type of custom attribute to test for.

Type: System.Boolean

This argument is ignored for objects of this type.

Return Value

Type: System.Boolean

true if one or more instances of attributeType have been applied to this module; otherwise, false.

Exception Condition

attributeType is null.


attributeType is not a Type object supplied by the runtime. For example, attributeType is a TypeBuilder object.

The following example demonstrates a use of the IsDefined method.

Imports System.Reflection

'Define a module-level attribute.
<Module: ReflectionModule_Examples.MySimpleAttribute("module-level")> 

'Define a module-level attribute.
Namespace ReflectionModule_Examples
    Class MyMainClass
        Shared Sub Main()
            Dim moduleArray() As [Module]

            moduleArray = GetType(MyMainClass).Assembly.GetModules(False)

            'In a simple project with only one module, the module at index
            ' 0 will be the module containing these classes.
            Dim myModule As [Module] = moduleArray(0)

            Dim myType As Type
            myType = myModule.GetType("ReflectionModule_Examples.MySimpleAttribute")
            Console.WriteLine("IsDefined(MySimpleAttribute) = {0}", myModule.IsDefined(myType, False))
        End Sub 'Main
    End Class 'MyMainClass

    'A very simple custom attribute.
    <AttributeUsage(AttributeTargets.Class Or AttributeTargets.Module)> _
    Public Class MySimpleAttribute
        Inherits Attribute
        Private name As String

        Public Sub New(ByVal newName As String)
            name = newName
        End Sub 'NewNew
    End Class 'MySimpleAttribute
End Namespace 'ReflectionModule_Examples

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