This documentation is archived and is not being maintained.

Module.IsDefined Method

Determines if the specified attributeType is defined on this module.

[Visual Basic]
Public Overridable Function IsDefined( _
   ByVal attributeType As Type, _
   ByVal inherit As Boolean _
) As Boolean Implements ICustomAttributeProvider.IsDefined
[C#]
public virtual bool IsDefined(
 Type attributeType,
 bool inherit
);
[C++]
public: virtual bool IsDefined(
 Type* attributeType,
 bool inherit
);
[JScript]
public function IsDefined(
   attributeType : Type,
 inherit : Boolean
) : Boolean;

Parameters

attributeType
The Type object to which the custom attribute is applied.
inherit
This argument is ignored for objects of this type.

Return Value

true if one or more instance of attributeType is defined on this module; otherwise, false.

Implements

ICustomAttributeProvider.IsDefined

Exceptions

Exception Type Condition
ArgumentNullException attributeType is a null reference (Nothing in Visual Basic).

Example

[Visual Basic, C#, C++] The following example demonstrates a use of the IsDefined method.

[Visual Basic] 
Imports System
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 = [Assembly].GetExecutingAssembly().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 'New
    End Class 'MySimpleAttribute
End Namespace 'ReflectionModule_Examples

[C#] 
using System;
using System.Reflection;

//Define a module-level attribute.
[module: ReflectionModule_Examples.MySimpleAttribute("module-level")]

namespace ReflectionModule_Examples
{
    class MyMainClass
    {
        static void Main()
        {
            Module[] moduleArray;
            
            moduleArray = Assembly.GetExecutingAssembly().GetModules(false);
            
            //In a simple project with only one module, the module at index
            // 0 will be the module containing these classes.
            Module myModule = moduleArray[0];

            Type myType;
            myType = myModule.GetType("ReflectionModule_Examples.MySimpleAttribute");
            Console.WriteLine("IsDefined(MySimpleAttribute) = {0}", myModule.IsDefined(myType, false));
        }
    }

    //A very simple custom attribute.
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Module)]
    public class MySimpleAttribute : Attribute
    {
        private string name;

        public MySimpleAttribute(string newName)
        {
            name = newName;
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;

namespace ReflectionModule_Examples
{
   //A very simple custom attribute.
   [AttributeUsage(AttributeTargets::Class | AttributeTargets::Module)]
   public __gc class MySimpleAttribute : public Attribute
   {
   private:
      String* name;

   public:
      MySimpleAttribute(String* newName)
      {
         name = newName;
      }
   };
}

//Define a module-level attribute.
[module: ReflectionModule_Examples::MySimpleAttribute(S"module-level")];

int main()
{
   System::Reflection::Module* moduleArray[];

   moduleArray = Assembly::GetExecutingAssembly()->GetModules(false);

   //In a simple project with only one module, the module at index
   // 0 will be the module containing these classes.
   System::Reflection::Module* myModule = moduleArray[0];

   Type* myType;
   myType = myModule->GetType(S"ReflectionModule_Examples.MySimpleAttribute");
   Console::WriteLine(S"IsDefined(MySimpleAttribute) = {0}", __box(myModule->IsDefined(myType, false)));
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

Module Class | Module Members | System.Reflection Namespace

Show: