System.Attribute.GetCustomAttributes 方法 (MemberInfo, Type)

方法描述

检索应用于类型的成员的自定义特性的数组。 参数指定成员,以及要搜索的自定义特性的类型。

语法定义(C# System.Attribute.GetCustomAttributes 方法 (MemberInfo, Type) 的用法)

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

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
element System-Reflection-MemberInfo 一个从 MemberInfo 类派生的对象,该类描述类的构造函数、事件、字段、方法或属性成员。
type System-Type 要搜索的自定义特性的类型,即基类型。
返回值 System.Attribute[] 一个 Attribute 数组,包含应用于 element 的 type 类型的自定义特性;如果不存在此类自定义特性,则为空数组。

提示和注释

返回值包含 element 的祖先的自定义特性。

注意

从 .NET Framework 2.0 版开始,如果方法、构造函数和类型的安全特性以新元数据格式存储,则此方法将返回这些特性。 用 2.0 版或更高版本编译的程序集使用新格式。 动态程序集和用 .NET Framework 的早期版本编译的程序集使用旧的 XML 格式。 请参见发出声明性安全特性。

System.Attribute.GetCustomAttributes 方法 (MemberInfo, Type)例子

下面的代码示例演示 GetCustomAttribute 的用法,采用 MemberInfo 作为参数。

using System;
using System.Reflection;
using System.Security;
using System.Runtime.InteropServices;

namespace CustAttrs4CS
{

    // Define an enumeration of Win32 unmanaged types
    public enum UnmanagedType
    {
        User,
        GDI,
        Kernel,
        Shell,
        Networking,
        Multimedia
    }

    // 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.")]
        [Unmanaged(UnmanagedType.User)]
        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:",
                            mInfo.Name);
                        Console.WriteLine("  \"{0}\"",
                            ((ObsoleteAttribute)attr).Message);
                    }

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

/*

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.

*/

异常

异常 异常描述
ArgumentNullException element 或 type 为 null。
ArgumentException type 不从 Attribute 派生。
NotSupportedException element 不是构造函数、方法、属性、事件、类型或字段。
TypeLoadException 无法加载自定义特性类型。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。