.NET Framework Class Library
AttributeGetCustomAttribute Method (Module, Type)

Retrieves a custom attribute applied to a module. Parameters specify the module, and the type of the custom attribute to search for.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)
Syntax
Public Shared Function GetCustomAttribute ( _
	element As [%$TOPIC/w11a4f55_en-us_VS_110_1_0_0_0_0%], _
	attributeType As [%$TOPIC/w11a4f55_en-us_VS_110_1_0_0_0_1%] _
) As [%$TOPIC/w11a4f55_en-us_VS_110_1_0_0_0_2%]
public static [%$TOPIC/w11a4f55_en-us_VS_110_1_0_1_0_0%] GetCustomAttribute(
	[%$TOPIC/w11a4f55_en-us_VS_110_1_0_1_0_1%] element,
	[%$TOPIC/w11a4f55_en-us_VS_110_1_0_1_0_2%] attributeType
)
public:
static [%$TOPIC/w11a4f55_en-us_VS_110_1_0_2_0_0%]^ GetCustomAttribute(
	[%$TOPIC/w11a4f55_en-us_VS_110_1_0_2_0_1%]^ element, 
	[%$TOPIC/w11a4f55_en-us_VS_110_1_0_2_0_2%]^ attributeType
)
static member GetCustomAttribute : 
        element:[%$TOPIC/w11a4f55_en-us_VS_110_1_0_3_0_0%] * 
        attributeType:[%$TOPIC/w11a4f55_en-us_VS_110_1_0_3_0_1%] -> [%$TOPIC/w11a4f55_en-us_VS_110_1_0_3_0_2%]

Parameters

element
Type: System.ReflectionModule

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

attributeType
Type: SystemType

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

Return Value

Type: SystemAttribute
A reference to the single custom attribute of type attributeType that is applied to element, or if there is no such attribute.
Exceptions
ExceptionCondition
ArgumentNullException

element or attributeType is .

ArgumentException

attributeType is not derived from Attribute.

AmbiguousMatchException

More than one of the requested attributes was found.

Examples

The following code example illustrates the use of the GetCustomAttribute method taking a Module as a parameter.

Imports System
Imports System.Reflection
Imports System.Diagnostics

' Add the Debuggable attribute to the module.
<Module: Debuggable(True, False)> 

Module DemoModule
    Sub Main()
        ' Get the module type information to access its metadata. 
        Dim modType As Type = GetType(DemoModule)
        ' See if the Debuggable attribute is defined. 
        Dim isDef As Boolean = Attribute.IsDefined(modType.Module, _
                               GetType(DebuggableAttribute))
        Dim strDef As String 
        If isDef = True Then
            strDef = "is" 
        Else
            strDef = "is not" 
        End If 
        ' Display the result
        Console.WriteLine("The debuggable attribute {0} defined for " & _
                          "module {1}.", strDef, modType.Name)
        ' If the attribute is defined, display the JIT settings. 
        If isDef = True Then 
            ' Retrieve the attribute itself. 
            Dim attr As Attribute = _
                Attribute.GetCustomAttribute(modType.Module, _
                GetType(DebuggableAttribute))
            If Not attr Is Nothing And TypeOf attr Is DebuggableAttribute Then 
                Dim dbgAttr As DebuggableAttribute = _
                    CType(attr, DebuggableAttribute)
                Console.WriteLine("JITTrackingEnabled is {0}.", _
                    dbgAttr.IsJITTrackingEnabled.ToString())
                Console.WriteLine("JITOptimizerDisabled is {0}.", _
                    dbgAttr.IsJITOptimizerDisabled.ToString())
            Else
                Console.WriteLine("The Debuggable attribute could " & _
                                  "not be retrieved.")
            End If 
        End If 
    End Sub 
End Module 

' Output: 
' The debuggable attribute is defined for module DemoModule. 
' JITTrackingEnabled is True. 
' JITOptimizerDisabled is False.
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.
 */
using namespace System;
using namespace System::Diagnostics;

// Add the Debuggable attribute to the module.
[module:Debuggable(true,false)];
namespace IsDef2CS
{
   ref class DemoClass
   {
   public:
      static void Main()
      {

         // Get the class type to access its metadata.
         Type^ clsType = DemoClass::typeid;

         // See if the Debuggable attribute is defined for this module. 
         bool isDef = Attribute::IsDefined( clsType->Module, DebuggableAttribute::typeid );

         // Display the result.
         Console::WriteLine( "The Debuggable attribute {0} " 
         "defined for Module {1}.", isDef ? (String^)"is" : "is not", clsType->Module->Name );

         // If the attribute is defined, display the JIT settings. 
         if ( isDef )
         {

            // Retrieve the attribute itself.
            DebuggableAttribute^ dbgAttr = dynamic_cast<DebuggableAttribute^>(Attribute::GetCustomAttribute( clsType->Module, DebuggableAttribute::typeid ));
            if ( dbgAttr != nullptr )
            {
               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.
 */
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8
Platforms

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.