System.Type.GetConstructor 方法 (BindingFlags, Binder, Type[], ParameterModifier[])

方法描述

使用指定绑定约束搜索其参数与指定参数类型和修饰符匹配的构造函数。

语法定义(C# System.Type.GetConstructor 方法 (BindingFlags, Binder, Type[], ParameterModifier[]) 的用法)

[ComVisibleAttribute(true)]
public ConstructorInfo GetConstructor(
	BindingFlags bindingAttr,
	Binder binder,
	Type[] types,
	ParameterModifier[] modifiers
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
bindingAttr System-Reflection-BindingFlags 一个位屏蔽,由一个或多个指定搜索执行方式的 BindingFlags 组成。- 或 -零,以返回 null。
binder System-Reflection-Binder 一个对象,该对象定义一组属性并启用绑定,而绑定可能涉及选择重载方法、强制参数类型和通过反射调用成员。- 或 -空引用(Visual Basic 中为 Nothing)将使用 DefaultBinder。
types System-Type[] Type 对象数组,表示构造函数要获取的参数的个数、顺序和类型。- 或 -获取不使用参数的构造函数的 Type 类型的空数组(即 Type[] types = new Type[0])。- 或 -EmptyTypes .
modifiers System-Reflection-ParameterModifier[] ParameterModifier 对象数组,表示与参数类型数组中的相应元素关联的特性。默认的联编程序不处理此参数。
返回值 System.Reflection.ConstructorInfo 表示符合指定要求的构造函数的 ConstructorInfo 对象(如果找到的话);否则为 null。

提示和注释

如果不存在完全匹配项,binder 将尝试强制 types 数组中指定的参数类型以便选择一个匹配项。 如果 binder 无法选择匹配项,则返回 null。

下列 BindingFlags 筛选标志可用于定义要包含在搜索中的构造函数:

为了获取返回值,必须指定 BindingFlags.Instance 或 BindingFlags.Static。

指定 BindingFlags.Public 可在搜索中包含公共构造函数。

指定 BindingFlags.NonPublic 可在搜索中包含非公共构造函数(即私有构造函数、内部构造函数和受保护的构造函数)。

有关更多信息,请参见 System.Reflection.BindingFlags。

若要使用此方法重载获取类初始值设定项 (.cctor),您必须指定 BindingFlags.Static。 | BindingFlags.NonPublic (isual Basic 中的 BindingFlags.StaticOrBindingFlags.NonPublic)。 还可以使用 TypeInitializer 属性获取类初始值设定项。

注意

查找构造函数和方法时不能省略参数。 只能在调用时省略参数。

如果当前 Type 表示构造泛型类型,则此方法将返回 ConstructorInfo,并且其类型形参替换为相应的类型实参。 如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此方法始终返回 null。

System.Type.GetConstructor 方法 (BindingFlags, Binder, Type[], ParameterModifier[])例子

下面的程序获取 MyClass1 类的类型,获取与指定绑定标志相匹配的 ConstructorInfo 对象,然后显示该构造函数的签名。

using System;
using System.Reflection;
using System.Security;


public class MyClass1
{
    public MyClass1(int i){}
    public static void Main()
    {
        try
        {
            Type myType = typeof(MyClass1);
            Type[] types = new Type[1];
            types[0] = typeof(int);
            // Get the constructor that is public and takes an integer parameter.
            ConstructorInfo constructorInfoObj = myType.GetConstructor(
                BindingFlags.Instance | BindingFlags.Public, null, types, null);
            if (constructorInfoObj != null )
            {
                Console.WriteLine("The constructor of MyClass1 that is public " +
                    "and takes an integer as a parameter is:");
                Console.WriteLine(constructorInfoObj.ToString());
            }
            else
            {
                Console.WriteLine("The constructor of the MyClass1 that is public " +
                    "and takes an integer as a parameter is not available.");
            }
        }
        catch(ArgumentNullException e)
        {
            Console.WriteLine("ArgumentNullException: " + e.Message);
        }
        catch(ArgumentException e)
        {
            Console.WriteLine("ArgumentException: " + e.Message);
        }
        catch(SecurityException e)
        {
            Console.WriteLine("SecurityException: " + e.Message);
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception: " + e.Message);
        }
    }
}

异常

异常 异常描述
ArgumentNullException
  • types 为 null。
  • types 中的一个元素为 null。
ArgumentException
  • types 是多维的。
  • modifiers 是多维的。
  • types 和 modifiers 的长度不同。

命名空间

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 系统要求。