System.Attribute.GetCustomAttributes 方法 (ParameterInfo, Type, Boolean)

方法描述

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

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

public static Attribute[] GetCustomAttributes(
	ParameterInfo element,
	Type attributeType,
	bool inherit
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
element System-Reflection-ParameterInfo 一个从 ParameterInfo 类派生的对象,该类描述类成员的参数。
attributeType System-Type 要搜索的自定义特性的类型,即基类型。
inherit System-Boolean 如果为 true,则指定还在 element 的祖先中搜索自定义特性。
返回值 System.Attribute[] 一个 Attribute 数组,包含应用于 element 的 attributeType 类型的自定义特性;如果不存在此类自定义特性,则为空数组。

提示和注释

如果 element 表示派生类型的方法中的参数,则返回值将包含应用于重写基方法中的同一参数的可继承自定义特性。

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

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

using System;
using System.Reflection;
using System.ComponentModel;

namespace CustAttrs5CS {
    public class AClass {
        public void ParamArrayAndDesc(
            // Add ParamArray (with the keyword) and Description attributes.
            [Description("This argument is a ParamArray")]
            params int[] args)
        {}
    }

    class DemoClass {
        static void Main(string[] args) {
            // Get the Class type to access its metadata.
            Type clsType = typeof(AClass);
            // Get the type information for the method.
            MethodInfo mInfo = clsType.GetMethod("ParamArrayAndDesc");
            if (mInfo != null) {
                // Get the parameter information.
                ParameterInfo[] pInfo = mInfo.GetParameters();
                if (pInfo != null) {
                    // Iterate through all the attributes for the parameter.
                    foreach(Attribute attr in 
                        Attribute.GetCustomAttributes(pInfo[0])) {
                        // Check for the ParamArray attribute.
                        if (attr.GetType() == typeof(ParamArrayAttribute))
                            Console.WriteLine("Parameter {0} for method {1} " +
                                "has the ParamArray attribute.",
                                pInfo[0].Name, mInfo.Name);
                        // Check for the Description attribute.
                        else if (attr.GetType() == 
                            typeof(DescriptionAttribute)) {
                            Console.WriteLine("Parameter {0} for method {1} " +
                                "has a description attribute.",
                                pInfo[0].Name, mInfo.Name);
                            Console.WriteLine("The description is: \"{0}\"",
                                ((DescriptionAttribute)attr).Description);
                        }
                    }
                }
            }
        }
    }
}

/*
 * Output:
 * Parameter args for method ParamArrayAndDesc has a description attribute.
 * The description is: "This argument is a ParamArray"
 * Parameter args for method ParamArrayAndDesc has the ParamArray attribute.
 */

异常

异常 异常描述
ArgumentNullException element 或 attributeType 为 null。
ArgumentException attributeType 不从 Attribute 派生。
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 系统要求。