This topic has not yet been rated - Rate this topic

Attribute::GetCustomAttributes Method (Module, Type, Boolean)

Retrieves an array of the custom attributes 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)
static array<Attribute^>^ GetCustomAttributes(
	Module^ element, 
	Type^ attributeType, 
	bool inherit


Type: System.Reflection::Module

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

Type: System::Type

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

Type: System::Boolean

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

Return Value

Type: array<System::Attribute>
An Attribute array that contains the custom attributes of type attributeType applied to element, or an empty array if no such custom attributes exist.

element or attributeType is nullptr.


attributeType is not derived from Attribute.

The return value contains the custom attributes for ancestors of element if inherit is true.

The following code example demonstrates the use of GetCustomAttributes, taking a Module as a parameter.

using namespace System;
using namespace System::Reflection;
using namespace System::ComponentModel;

// Assign some attributes to the module. 
// Set the module's CLSCompliant attribute to false 
// The CLSCompliant attribute is applicable for /target:module.
[module:Description("A sample description")];
namespace CustAttrs2CS
   ref class DemoClass
      static void Main()
         Type^ clsType = DemoClass::typeid;

         // Get the Module type to access its metadata.
         Module^ module = clsType->Module;

         // Iterate through all the attributes for the module.
         System::Collections::IEnumerator^ myEnum1 = Attribute::GetCustomAttributes( module )->GetEnumerator();
         while ( myEnum1->MoveNext() )
            Attribute^ attr = safe_cast<Attribute^>(myEnum1->Current);

            // Check for the Description attribute. 
            if ( attr->GetType() == DescriptionAttribute::typeid )
                        Console::WriteLine( "Module {0} has the description \"{1}\".", module->Name, (dynamic_cast<DescriptionAttribute^>(attr))->Description );
            // Check for the CLSCompliant attribute. 

            // Check for the CLSCompliant attribute. 
            if ( attr->GetType() == CLSCompliantAttribute::typeid )
                        Console::WriteLine( "Module {0} {1} CLSCompliant.", module->Name, (dynamic_cast<CLSCompliantAttribute^>(attr))->IsCompliant ? (String^)"is" : "is not" );

 * Output:
 * Module CustAttrs2CS.exe is not CLSCompliant.
 * Module CustAttrs2CS.exe has the description "A sample description".

.NET Framework

Supported in: 4.5.1, 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 8, Silverlight 8.1

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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.