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)

public static bool IsDefined (
	Module element,
	Type attributeType,
	bool inherit
)
public static boolean IsDefined (
	Module element, 
	Type attributeType, 
	boolean inherit
)
public static function IsDefined (
	element : Module, 
	attributeType : Type, 
	inherit : boolean
) : boolean

Parameters

element

An object derived from the Module class that describes a portable executable file.

attributeType

The type, or a base type, of the custom attribute to search for.

inherit

This parameter is ignored, and does not affect the operation of this method.

Return Value

true if a custom attribute of type attributeType is applied to element; otherwise, false.

Exception typeCondition

ArgumentNullException

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

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.

using System;
using System.Diagnostics;

// Add the Debuggable attribute to the module.
[module:Debuggable(true, false)]
namespace IsDef2CS
{
    public class DemoClass
    {
        static void Main(string[] args)
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(DemoClass);
            // See if the Debuggable attribute is defined for this module.
            bool isDef = Attribute.IsDefined(clsType.Module, 
                typeof(DebuggableAttribute));
            // Display the result.
            Console.WriteLine("The Debuggable attribute {0} " +
                "defined for Module {1}.",
                isDef ? "is" : "is not",
                clsType.Module.Name);
            // If the attribute is defined, display the JIT settings.
            if (isDef)
            {
                // Retrieve the attribute itself.
                DebuggableAttribute dbgAttr = (DebuggableAttribute)
                    Attribute.GetCustomAttribute(clsType.Module, 
                    typeof(DebuggableAttribute));
                if (dbgAttr != null)
                {
                    Console.WriteLine("JITTrackingEnabled is {0}.",
                        dbgAttr.IsJITTrackingEnabled);
                    Console.WriteLine("JITOptimizerDisabled is {0}.",
                        dbgAttr.IsJITOptimizerDisabled);
                }
                else
                    Console.WriteLine("The Debuggable attribute " +
                        "could not be retrieved.");
            }
        }
    }
}

/*
 * Output:
 * The Debuggable attribute is defined for Module IsDef2CS.exe.
 * JITTrackingEnabled is True.
 * JITOptimizerDisabled is False.
 */

package IsDef2CS;
import System.*;
import System.Diagnostics.*;

// Add the Debuggable attribute to the module.
/** @module Debuggable(true, false)
 */
class DemoClass
{
    public static void main(String[] args)
    {
        // Get the class type to access its metadata.
        Type clsType = DemoClass.class.ToType();
        // See if the Debuggable attribute is defined for this module.
        boolean isDef = Attribute.IsDefined(clsType.get_Module(), 
            DebuggableAttribute.class.ToType());
        // Display the result.
        Console.WriteLine("The Debuggable attribute {0} " 
            + "defined for Module {1}.", (isDef) ? "is" : "is not", 
            clsType.get_Module().get_Name());
        // If the attribute is defined, display the JIT settings.
        if (isDef) {
            // Retrieve the attribute itself.
            DebuggableAttribute dbgAttr = ((DebuggableAttribute)
                (Attribute.GetCustomAttribute(clsType.get_Module(), 
                DebuggableAttribute.class.ToType())));
            if (dbgAttr != null) {
                Console.WriteLine("JITTrackingEnabled is {0}.", 
                    (System.Boolean)dbgAttr.get_IsJITTrackingEnabled());
                Console.WriteLine("JITOptimizerDisabled is {0}.", 
                    (System.Boolean)dbgAttr.get_IsJITOptimizerDisabled());
            }
            else {
                Console.WriteLine("The Debuggable attribute " 
                    + "could not be retrieved.");
            }
        }
    } //main
} //DemoClass

/*
   Output:
   The Debuggable attribute is defined for Module IsDef2CS.exe.
   JITTrackingEnabled is True.
   JITOptimizerDisabled is False.
*/

Windows 98, Windows 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 .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft