Attribute::GetCustomAttributes Method (MemberInfo^)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Retrieves an array of the custom attributes applied to a member of a type. A parameter specifies the member.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

static array<Attribute^>^ GetCustomAttributes(
	MemberInfo^ element


Type: System.Reflection::MemberInfo^

An object derived from the MemberInfo class that describes a constructor, event, field, method, or property member of a class.

Return Value

Type: array<System::Attribute^>^

An Attribute array that contains the custom attributes applied to element, or an empty array if no such custom attributes exist.

Exception Condition

element is null.


element is not a constructor, method, property, event, type, or field.


A custom attribute type cannot be loaded.

The return value contains the custom attributes for ancestors of element.


Starting with the .NET Framework version 2.0, this method returns security attributes on methods, constructors, and types if the attributes are stored in the new metadata format. Assemblies compiled with version 2.0 or later use the new format. Dynamic assemblies and assemblies compiled with earlier versions of the .NET Framework use the old XML format. See Emitting Declarative Security Attributes.

The following code example demonstrates the use of GetCustomAttribute, taking a MemberInfo as a parameter.

using namespace System;
using namespace System::Reflection;
using namespace System::Security;
using namespace System::Runtime::InteropServices;

namespace CustAttrs4CS
   // Create a class for Win32 imported unmanaged functions.
   public ref class Win32

      [DllImport("user32.dll", CharSet = CharSet::Unicode)]
      static int MessageBox( int hWnd, String^ text, String^ caption, UInt32 type );

   public ref class AClass

      // Add some attributes to the Win32CallMethod.

      [Obsolete("This method is obsolete. Use managed MsgBox instead.")]
      void Win32CallMethod()
         Win32::MessageBox( 0, "This is an unmanaged call.", "Be Careful!", 0 );


   ref class DemoClass
      static void Main()
         // Get the Class type to access its metadata.
         Type^ clsType = AClass::typeid;

         // Get the type information for the Win32CallMethod.
         MethodInfo^ mInfo = clsType->GetMethod( "Win32CallMethod" );
         if ( mInfo != nullptr )
            // Iterate through all the attributes for the method.
            System::Collections::IEnumerator^ myEnum3 = Attribute::GetCustomAttributes( mInfo )->GetEnumerator();
            while ( myEnum3->MoveNext() )
               Attribute^ attr = safe_cast<Attribute^>(myEnum3->Current);

               // Check for the Obsolete attribute.
               if ( attr->GetType() == ObsoleteAttribute::typeid )
                  Console::WriteLine( "Method {0} is obsolete. "
                  "The message is:", mInfo->Name );
                  Console::WriteLine( (dynamic_cast<ObsoleteAttribute^>(attr))->Message );
               // Check for the SuppressUnmanagedCodeSecurity attribute.

               // Check for the SuppressUnmanagedCodeSecurity attribute.
               if ( attr->GetType() == SuppressUnmanagedCodeSecurityAttribute::typeid )
                  Console::WriteLine( "This method calls unmanaged code "
                  "with no security check." );
                  Console::WriteLine( "Please do not use unless absolutely necessary." );
                  AClass^ myCls = gcnew AClass;

 * Output:
 * Method Win32CallMethod is obsolete. The message is:
 * This method is obsolete. Use managed MsgBox instead.
 * This method calls unmanaged code with no security check.
 * Please do not use unless absolutely necessary.

.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Return to top