System.Attribute.GetCustomAttribute 方法 (MemberInfo, Type, Boolean)

方法描述

检索应用于类型成员的自定义特性。 参数指定成员、要搜索的自定义特性的类型以及是否搜索成员的祖先。

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

public static Attribute GetCustomAttribute(
	MemberInfo element,
	Type attributeType,
	bool inherit
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
element System-Reflection-MemberInfo 一个从 MemberInfo 类派生的对象,该类描述类的构造函数、事件、字段、方法或属性成员。
attributeType System-Type 要搜索的自定义特性的类型,即基类型。
inherit System-Boolean 如果为 true,则指定还在 element 的祖先中搜索自定义特性。
返回值 System.Attribute 一个引用,指向应用于 element 的 attributeType 类型的单个自定义特性;如果没有此类特性,则为 null。

提示和注释

注意

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

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

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

using System;
using System.Reflection;

namespace IsDef4CS 
{
    public class TestClass 
    {
        // Assign the Obsolete attribute to a method.
        [Obsolete("This method is obsolete. Use Method2 instead.")]
        public void Method1()
        {}
        public void Method2()
        {}
    }

    public class DemoClass 
    {
        static void Main(string[] args) 
        {
            // Get the class type to access its metadata.
            Type clsType = typeof(TestClass);
            // Get the MethodInfo object for Method1.
            MethodInfo mInfo = clsType.GetMethod("Method1");
            // See if the Obsolete attribute is defined for this method.
            bool isDef = Attribute.IsDefined(mInfo, typeof(ObsoleteAttribute));
            // Display the result.
            Console.WriteLine("The Obsolete Attribute {0} defined for {1} of class {2}.",
                isDef ? "is" : "is not", mInfo.Name, clsType.Name);
            // If it's defined, display the attribute's message.
            if (isDef) 
            {
                ObsoleteAttribute obsAttr = 
                                 (ObsoleteAttribute)Attribute.GetCustomAttribute( 
                                                    mInfo, typeof(ObsoleteAttribute));
                if (obsAttr != null)
                    Console.WriteLine("The message is: \"{0}\".",
                        obsAttr.Message);
                else
                    Console.WriteLine("The message could not be retrieved.");
            }
        }
    }
}

/*
 * Output:
 * The Obsolete Attribute is defined for Method1 of class TestClass.
 * The message is: "This method is obsolete. Use Method2 instead.".
 */

异常

异常 异常描述
ArgumentNullException element 或 attributeType 为 null。
ArgumentException attributeType 不从 Attribute 派生。
NotSupportedException element 不是构造函数、方法、属性、事件、类型或字段。
AmbiguousMatchException 找到了多个请求的特性。
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 系统要求。