Attribute.IsDefined Method (Module, Type)
.NET Framework 4
Determines whether any custom attributes of a specified type are applied to a module. Parameters specify the module, and the type of the custom attribute to search for.
Assembly: mscorlib (in mscorlib.dll)
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.
Return Value
Type: System.Booleantrue if a custom attribute of type attributeType is applied to element; otherwise, false.
| Exception | Condition |
|---|---|
| ArgumentNullException |
element or attributeType is null. |
| ArgumentException |
attributeType is not derived from Attribute. |
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. */
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.