Attribute.GetCustomAttributes Method (MemberInfo, Type)


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. Parameters specify the member, and the type of the custom attribute to search for.

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

public static Attribute[] GetCustomAttributes(
	MemberInfo element,
	Type type


Type: System.Reflection.MemberInfo

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

Type: System.Type

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

Return Value

Type: System.Attribute[]

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

Exception Condition

element or type is null.


type is not derived from Attribute.


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.


.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