Module.IsDefined Method

Determines if the specified attributeType is defined on this module.

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

public virtual bool IsDefined (
	Type attributeType,
	bool inherit
)
public boolean IsDefined (
	Type attributeType, 
	boolean inherit
)
public function IsDefined (
	attributeType : Type, 
	inherit : boolean
) : boolean
Not applicable.

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.

Exception typeCondition

ArgumentNullException

attributeType is a null reference (Nothing in Visual Basic).

ArgumentException

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.

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;
        }
    }
}

package ReflectionModule_Examples;

import System.*;
import System.Reflection.*;

//Define a module-level attribute.
/** @module ReflectionModule_Examples.MySimpleAttribute("module-level")
 */
class MyMainClass
{
    public static void main(String[] args)
    {
        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 = (Module)moduleArray.get_Item(0);

        Type myType;
        myType = myModule.GetType("ReflectionModule_Examples.MySimpleAttribute");
        Console.WriteLine("IsDefined(MySimpleAttribute) = {0}", 
            System.Convert.ToString(myModule.IsDefined(myType, false)));
    } //main
} //MyMainClass

//A very simple custom attribute.
/** @attribute AttributeUsage(AttributeTargets.Class | AttributeTargets.Module)
 */
public class MySimpleAttribute extends Attribute
{
    private String name;

    public MySimpleAttribute(String newName)
    {
        name = newName;
    } //MySimpleAttribute
} //MySimpleAttribute

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0
Show: