Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Attribute.GetCustomAttribute Method (Module, Type, Boolean)

Retrieves a custom attribute 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 Attribute GetCustomAttribute (
	Module element,
	Type attributeType,
	bool inherit
)
public static Attribute GetCustomAttribute (
	Module element, 
	Type attributeType, 
	boolean inherit
)
public static function GetCustomAttribute (
	element : Module, 
	attributeType : Type, 
	inherit : boolean
) : Attribute
Not applicable.

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

A reference to the single custom attribute of type attributeType that is applied to element, or a null reference (Nothing in Visual Basic) if there is no such attribute.

Exception typeCondition

ArgumentNullException

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

ArgumentException

attributeType is not derived from Attribute.

AmbiguousMatchException

More than one of the requested attributes was found.

The following code example illustrates the use of the GetCustomAttribute method 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 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, 1.0

XNA Framework

Supported in: 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft