This documentation is archived and is not being maintained.

Attribute.GetCustomAttributes Method (MemberInfo)

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)

public static 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: System.Attribute[]
An Attribute array that contains the custom attributes applied to element, or an empty array if no such custom attributes exist.


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 System;
using System.Reflection;
using System.Security;
using System.Runtime.InteropServices;

namespace CustAttrs4CS

    // Define an enumeration of Win32 unmanaged types 
    public enum UnmanagedType

    // Define the Unmanaged attribute. 
    public class UnmanagedAttribute : Attribute
        // Storage for the UnmanagedType value. 
        protected UnmanagedType thisType;

        // Set the unmanaged type in the constructor. 
        public UnmanagedAttribute(UnmanagedType type)
            thisType = type;

        // Define a property to get and set the UnmanagedType value. 
        public UnmanagedType Win32Type
            get { return thisType; }
            set { thisType = Win32Type; }

    // Create a class for an imported Win32 unmanaged function. 
    public class Win32 {
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        public static extern int MessageBox(int hWnd, String text,
            String caption, uint type);

    public class AClass {
        // Add some attributes to Win32CallMethod.
        [Obsolete("This method is obsolete. Use managed MsgBox instead.")]
        public void Win32CallMethod()
            Win32.MessageBox(0, "This is an unmanaged call.", "Caution!", 0);

    class DemoClass {
        static void Main(string[] args)
            // Get the AClass type to access its metadata.
            Type clsType = typeof(AClass);
            // Get the type information for Win32CallMethod.
            MethodInfo mInfo = clsType.GetMethod("Win32CallMethod");
            if (mInfo != null)
                // Iterate through all the attributes of the method. 
                foreach(Attribute attr in
                    Attribute.GetCustomAttributes(mInfo)) {
                    // Check for the Obsolete attribute. 
                    if (attr.GetType() == typeof(ObsoleteAttribute))
                        Console.WriteLine("Method {0} is obsolete. " +
                            "The message is:",
                        Console.WriteLine("  \"{0}\"",

                    // Check for the Unmanaged attribute. 
                    else if (attr.GetType() == typeof(UnmanagedAttribute))
                            "This method calls unmanaged code.");
                            String.Format("The Unmanaged attribute type is {0}.",
                        AClass myCls = new AClass();


This code example produces the following results.

First, the compilation yields the warning, "...This method is
obsolete. Use managed MsgBox instead."
Second, execution yields a message box with a title of "Caution!"
and message text of "This is an unmanaged call."
Third, the following text is displayed in the console window:

Method Win32CallMethod is obsolete. The message is:
  "This method is obsolete. Use managed MsgBox instead."
This method calls unmanaged code.
The Unmanaged attribute type is User.


Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0