Type.GetConstructor 方法

定义

获取当前 Type 的特定构造函数。Gets a specific constructor of the current Type.

重载

GetConstructor(Type[])

搜索其参数与指定数组中的类型匹配的公共实例构造函数。Searches for a public instance constructor whose parameters match the types in the specified array.

GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[])

使用指定绑定约束搜索其参数与指定自变量类型和修饰符匹配的构造函数。Searches for a constructor whose parameters match the specified argument types and modifiers, using the specified binding constraints.

GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

用指定绑定约束和指定调用约定,搜索其参数与指定参数类型及修饰符匹配的构造函数。Searches for a constructor whose parameters match the specified argument types and modifiers, using the specified binding constraints and the specified calling convention.

GetConstructor(Type[])

搜索其参数与指定数组中的类型匹配的公共实例构造函数。Searches for a public instance constructor whose parameters match the types in the specified array.

public:
 virtual System::Reflection::ConstructorInfo ^ GetConstructor(cli::array <Type ^> ^ types);
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.ConstructorInfo GetConstructor (Type[] types);
abstract member GetConstructor : Type[] -> System.Reflection.ConstructorInfo
override this.GetConstructor : Type[] -> System.Reflection.ConstructorInfo
Public Function GetConstructor (types As Type()) As ConstructorInfo

参数

types
Type[]

表示需要的构造函数的参数个数、顺序和类型的 Type 对象的数组。An array of Type objects representing the number, order, and type of the parameters for the desired constructor.

-or- Type 对象的空数组,用于获取不带参数的构造函数。An empty array of Type objects, to get a constructor that takes no parameters. 这样的空数组由 static 字段 EmptyTypes 提供。Such an empty array is provided by the static field EmptyTypes.

返回

为表示某个公共实例构造函数(该构造函数的参数与参数类型数组中的类型匹配)的对象(如果找到的话);否则为 nullAn object representing the public instance constructor whose parameters match the types in the parameter type array, if found; otherwise, null.

实现

属性

异常

typesnulltypes is null.

-or- types 的其中一个元素为 nullOne of the elements in types is null.

types 是多维的。types is multidimensional.

示例

下面的示例获取 MyClass的类型,获取 ConstructorInfo 对象,并显示构造函数签名。The following example obtains the type of MyClass, gets the ConstructorInfo object, and displays the constructor signature.

using namespace System;
using namespace System::Reflection;
using namespace System::Security;
public ref class MyClass1
{
public:
   MyClass1(){}

   MyClass1( int i ){}

};

int main()
{
   try
   {
      Type^ myType = MyClass1::typeid;
      array<Type^>^types = gcnew array<Type^>(1);
      types[ 0 ] = int::typeid;
      
      // Get the constructor that takes an integer as a parameter.
      ConstructorInfo^ constructorInfoObj = myType->GetConstructor( types );
      if ( constructorInfoObj != nullptr )
      {
         Console::WriteLine( "The constructor of MyClass1 that takes an integer as a parameter is: " );
         Console::WriteLine( constructorInfoObj );
      }
      else
      {
         Console::WriteLine( "The constructor of MyClass1 that takes an integer as a parameter is not available." );
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception caught." );
      Console::WriteLine( "Source: {0}", e->Source );
      Console::WriteLine( "Message: {0}", e->Message );
   }
}

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

public class MyClass1
{
    public 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 takes an integer as a parameter.
            ConstructorInfo constructorInfoObj = myType.GetConstructor(types);
            if (constructorInfoObj != null)
            {
                Console.WriteLine("The constructor of MyClass1 that takes an " + 
                    "integer as a parameter is: "); 
                Console.WriteLine(constructorInfoObj.ToString());
            }
            else
            {
                Console.WriteLine("The constructor of MyClass1 that takes an integer " +
                    "as a parameter is not available."); 
            }
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception caught.");
            Console.WriteLine("Source: " + e.Source);
            Console.WriteLine("Message: " + e.Message);
        }
    }
}
Imports System.Reflection
Imports System.Security

Public Class MyClass1

    Public Sub New()
    End Sub

    Public Sub New(ByVal i As Integer)
    End Sub

    Public Shared Sub Main()
        Try
            Dim myType As Type = GetType(MyClass1)
            Dim types(0) As Type
            types(0) = GetType(Int32)
            ' Get the constructor that takes an integer as a parameter.
            Dim constructorInfoObj As ConstructorInfo = myType.GetConstructor(types)
            If Not (constructorInfoObj Is Nothing) Then
                Console.WriteLine("The constructor of MyClass that takes an integer as a parameter is: ")
                Console.WriteLine(constructorInfoObj.ToString())
            Else
                Console.WriteLine("The constructor of MyClass that takes no " + "parameters is not available.")
            End If

        Catch e As Exception
            Console.WriteLine("Exception caught.")
            Console.WriteLine(("Source: " + e.Source))
            Console.WriteLine(("Message: " + e.Message))
        End Try
    End Sub
End Class

注解

此方法重载查找公共实例构造函数,不能用于获取类初始值设定项(静态构造函数)。This method overload looks for public instance constructors and cannot be used to obtain a class initializer (static constructor). 若要获取类初始值设定项,请使用采用 BindingFlags的重载,并|指定 BindingFlags.Static BindingFlags.NonPublicBindingFlags.StaticOr中的 BindingFlags.NonPublic)。To get a class initializer, use an overload that takes BindingFlags, and specify BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.NonPublic in Visual Basic). 还可以使用 TypeInitializer 属性获取类初始值设定项。You can also get the class initializer using the TypeInitializer property.

如果请求的构造函数是非公共的,则此方法返回 nullIf the requested constructor is non-public, this method returns null.

备注

在查找构造函数和方法时,不能忽略参数。You cannot omit parameters when looking up constructors and methods. 在调用时,只能省略参数。You can only omit parameters when invoking.

如果当前 Type 表示构造泛型类型,则此方法将返回类型参数替换为相应类型参数的 ConstructorInfoIf the current Type represents a constructed generic type, this method returns the ConstructorInfo with the type parameters replaced by the appropriate type arguments. 如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此方法始终返回 nullIf the current Type represents a type parameter in the definition of a generic type or generic method, this method always returns null.

另请参阅

GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[])

使用指定绑定约束搜索其参数与指定自变量类型和修饰符匹配的构造函数。Searches for a constructor whose parameters match the specified argument types and modifiers, using the specified binding constraints.

public:
 virtual System::Reflection::ConstructorInfo ^ GetConstructor(System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.ConstructorInfo GetConstructor (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, Type[] types, System.Reflection.ParameterModifier[] modifiers);
abstract member GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
override this.GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo

参数

bindingAttr
BindingFlags

枚举值的按位组合,这些值指定如何进行搜索。A bitwise combination of the enumeration values that specify how the search is conducted.

-or- 若为 Default,则返回 nullDefault to return null.

binder
Binder

一个对象,该对象定义一组属性并启用绑定,而绑定可能涉及选择重载方法、强制参数类型和通过反射调用成员。An object that defines a set of properties and enables binding, which can involve selection of an overloaded method, coercion of argument types, and invocation of a member through reflection.

-or- 要使用 Nothing 的空引用(在 Visual Basic 中为 DefaultBinder)。A null reference (Nothing in Visual Basic), to use the DefaultBinder.

types
Type[]

Type 对象的数组,表示构造函数要获取的参数的个数、顺序和类型。An array of Type objects representing the number, order, and type of the parameters for the constructor to get.

-or- 获取不使用参数的构造函数的 Type 类型的空数组(即 Type[] types = new Type[0])。An empty array of the type Type (that is, Type[] types = new Type[0]) to get a constructor that takes no parameters.

-or- EmptyTypesEmptyTypes.

modifiers
ParameterModifier[]

ParameterModifier 对象的数组,表示与参数类型数组中的相应元素关联的特性。An array of ParameterModifier objects representing the attributes associated with the corresponding element in the parameter type array. 默认的联编程序不处理此参数。The default binder does not process this parameter.

返回

表示符合指定需求的构造函数的 ConstructorInfo 对象(如果找到的话);否则为 nullA ConstructorInfo object representing the constructor that matches the specified requirements, if found; otherwise, null.

实现

属性

异常

typesnulltypes is null.

-or- types 的其中一个元素为 nullOne of the elements in types is null.

types 是多维的。types is multidimensional.

-or- modifiers 是多维的。modifiers is multidimensional.

-or- typesmodifiers 的长度不相同。types and modifiers do not have the same length.

示例

下面的程序获取 MyClass1 类的类型,获取与指定的绑定标志匹配的 ConstructorInfo 对象,并显示该构造函数的签名。The following program obtains the type of MyClass1 class, gets the ConstructorInfo object matching the specified binding flags, and displays the signature of the constructor.

using namespace System;
using namespace System::Reflection;
using namespace System::Security;
public ref class MyClass1
{
public:
   MyClass1( int i ){}

};

int main()
{
   try
   {
      Type^ myType = MyClass1::typeid;
      array<Type^>^types = gcnew array<Type^>(1);
      types[ 0 ] = int::typeid;
      
      // Get the constructor that is public and takes an integer parameter.
      ConstructorInfo^ constructorInfoObj = myType->GetConstructor( static_cast<BindingFlags>(BindingFlags::Instance | BindingFlags::Public), nullptr, types, nullptr );
      if ( constructorInfoObj != nullptr )
      {
         Console::WriteLine( "The constructor of MyClass1 that is public and takes an integer as a parameter is:" );
         Console::WriteLine( constructorInfoObj );
      }
      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: {0}", e->Message );
   }
   catch ( ArgumentException^ e ) 
   {
      Console::WriteLine( "ArgumentException: {0}", e->Message );
   }
   catch ( SecurityException^ e ) 
   {
      Console::WriteLine( "SecurityException: {0}", e->Message );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception: {0}", e->Message );
   }
}
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);
        }
    }
}
Imports System.Reflection
Imports System.Security


Public Class MyClass1
    Public Sub New(ByVal i As Integer)
    End Sub

    Public Shared Sub Main()
        Try
            Dim myType As Type = GetType(MyClass1)
            Dim types(0) As Type
            types(0) = GetType(Integer)
            ' Get the constructor that is public and takes an integer parameter.
            Dim constructorInfoObj As ConstructorInfo = _
                     myType.GetConstructor(BindingFlags.Instance Or _
                     BindingFlags.Public, Nothing, types, Nothing)
            If Not (constructorInfoObj Is Nothing) Then
                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 MyClass1 that is " + _
                  "public and takes an integer as a parameter is not available.")
            End If
        Catch e As ArgumentNullException
            Console.WriteLine("ArgumentNullException: " + e.Message)
        Catch e As ArgumentException
            Console.WriteLine("ArgumentException: " + e.Message)
        Catch e As SecurityException
            Console.WriteLine("SecurityException: " + e.Message)
        Catch e As Exception
            Console.WriteLine("Exception: " + e.Message)
        End Try
    End Sub
End Class

注解

如果不存在完全匹配项,binder 将尝试强制转换 types 数组中指定的参数类型,以便选择匹配项。If an exact match does not exist, the binder will attempt to coerce the parameter types specified in the types array in order to select a match. 如果 binder 无法选择匹配项,则返回 nullIf the binder is unable to select a match, then null is returned.

以下 BindingFlags 筛选器标志可用于定义要包括在搜索中的构造函数:The following BindingFlags filter flags can be used to define which constructors to include in the search:

  • 若要获取返回,必须指定 BindingFlags.InstanceBindingFlags.StaticYou must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.

  • 指定 BindingFlags.Public 以在搜索中包括公共构造函数。Specify BindingFlags.Public to include public constructors in the search.

  • 指定 BindingFlags.NonPublic 以在搜索中包括非公共构造函数(即私有、内部和受保护的构造函数)。Specify BindingFlags.NonPublic to include non-public constructors (that is, private, internal, and protected constructors) in the search.

有关更多信息,请参见System.Reflection.BindingFlagsSee System.Reflection.BindingFlags for more information.

若要使用此方法重载获取类初始值设定项(静态构造函数),必须|指定 BindingFlags.Static BindingFlags.NonPublicBindingFlags.StaticOrBindingFlags.NonPublic 中的 Visual Basic)。To get the class initializer (static constructor) using this method overload, you must specify BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.NonPublic in Visual Basic). 还可以使用 TypeInitializer 属性获取类初始值设定项。You can also get the class initializer using the TypeInitializer property.

备注

在查找构造函数和方法时,不能忽略参数。You cannot omit parameters when looking up constructors and methods. 在调用时,只能省略参数。You can only omit parameters when invoking.

如果当前 Type 表示构造泛型类型,则此方法将返回类型参数替换为相应类型参数的 ConstructorInfoIf the current Type represents a constructed generic type, this method returns the ConstructorInfo with the type parameters replaced by the appropriate type arguments. 如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此方法始终返回 nullIf the current Type represents a type parameter in the definition of a generic type or generic method, this method always returns null.

另请参阅

GetConstructor(BindingFlags, Binder, CallingConventions, Type[], ParameterModifier[])

用指定绑定约束和指定调用约定,搜索其参数与指定参数类型及修饰符匹配的构造函数。Searches for a constructor whose parameters match the specified argument types and modifiers, using the specified binding constraints and the specified calling convention.

public:
 virtual System::Reflection::ConstructorInfo ^ GetConstructor(System::Reflection::BindingFlags bindingAttr, System::Reflection::Binder ^ binder, System::Reflection::CallingConventions callConvention, cli::array <Type ^> ^ types, cli::array <System::Reflection::ParameterModifier> ^ modifiers);
[System.Runtime.InteropServices.ComVisible(true)]
public System.Reflection.ConstructorInfo GetConstructor (System.Reflection.BindingFlags bindingAttr, System.Reflection.Binder binder, System.Reflection.CallingConventions callConvention, Type[] types, System.Reflection.ParameterModifier[] modifiers);
abstract member GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo
override this.GetConstructor : System.Reflection.BindingFlags * System.Reflection.Binder * System.Reflection.CallingConventions * Type[] * System.Reflection.ParameterModifier[] -> System.Reflection.ConstructorInfo

参数

bindingAttr
BindingFlags

枚举值的按位组合,这些值指定如何进行搜索。A bitwise combination of the enumeration values that specify how the search is conducted.

-or- 若为 Default,则返回 nullDefault to return null.

binder
Binder

一个对象,该对象定义一组属性并启用绑定,而绑定可能涉及选择重载方法、强制参数类型和通过反射调用成员。An object that defines a set of properties and enables binding, which can involve selection of an overloaded method, coercion of argument types, and invocation of a member through reflection.

-or- 要使用 Nothing 的空引用(在 Visual Basic 中为 DefaultBinder)。A null reference (Nothing in Visual Basic), to use the DefaultBinder.

callConvention
CallingConventions

对象,用于指定要使用的一套规则,这些规则涉及参数的顺序和布局、传递返回值的方式、用于参数的寄存器和清理堆栈的方式。The object that specifies the set of rules to use regarding the order and layout of arguments, how the return value is passed, what registers are used for arguments, and the stack is cleaned up.

types
Type[]

Type 对象的数组,表示构造函数要获取的参数的个数、顺序和类型。An array of Type objects representing the number, order, and type of the parameters for the constructor to get.

-or- 获取不使用参数的构造函数的 Type 类型的空数组(即 Type[] types = new Type[0])。An empty array of the type Type (that is, Type[] types = new Type[0]) to get a constructor that takes no parameters.

modifiers
ParameterModifier[]

ParameterModifier 对象的数组,表示与 types 数组中的相应元素关联的特性。An array of ParameterModifier objects representing the attributes associated with the corresponding element in the types array. 默认的联编程序不处理此参数。The default binder does not process this parameter.

返回

表示符合指定需求的构造函数的对象(如果找到的话);否则为 nullAn object representing the constructor that matches the specified requirements, if found; otherwise, null.

实现

属性

异常

typesnulltypes is null.

-or- types 的其中一个元素为 nullOne of the elements in types is null.

types 是多维的。types is multidimensional.

-or- modifiers 是多维的。modifiers is multidimensional.

-or- typesmodifiers 的长度不相同。types and modifiers do not have the same length.

示例

下面的示例获取 MyClass1的类型,获取与指定的绑定标志匹配的 ConstructorInfo 对象,并显示构造函数签名。The following example obtains the type of MyClass1, gets the ConstructorInfo object that matches the specified binding flags, and displays the constructor signature.

using namespace System;
using namespace System::Reflection;
using namespace System::Security;
public ref class MyClass1
{
public:
   MyClass1( int i ){}

};

int main()
{
   try
   {
      Type^ myType = MyClass1::typeid;
      array<Type^>^types = gcnew array<Type^>(1);
      types[ 0 ] = int::typeid;
      
      // Get the public instance constructor that takes an integer parameter.
      ConstructorInfo^ constructorInfoObj = myType->GetConstructor( static_cast<BindingFlags>(BindingFlags::Instance | BindingFlags::Public), nullptr, CallingConventions::HasThis, types, nullptr );
      if ( constructorInfoObj != nullptr )
      {
         Console::WriteLine( "The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is: " );
         Console::WriteLine( constructorInfoObj );
      }
      else
      {
         Console::WriteLine( "The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is not available." );
      }
   }
   catch ( ArgumentNullException^ e ) 
   {
      Console::WriteLine( "ArgumentNullException: {0}", e->Message );
   }
   catch ( ArgumentException^ e ) 
   {
      Console::WriteLine( "ArgumentException: {0}", e->Message );
   }
   catch ( SecurityException^ e ) 
   {
      Console::WriteLine( "SecurityException: {0}", e->Message );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception: {0}", e->Message );
   }
}
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 public instance constructor that takes an integer parameter.
            ConstructorInfo constructorInfoObj = myType.GetConstructor(
                BindingFlags.Instance | BindingFlags.Public, null,
                CallingConventions.HasThis, types, null);
            if(constructorInfoObj != null)
            {
                Console.WriteLine("The constructor of MyClass1 that is a public " +
                    "instance method and takes an integer as a parameter is: ");
                Console.WriteLine(constructorInfoObj.ToString());
            }
            else
            {
                Console.WriteLine("The constructor of MyClass1 that is a public instance " +
                    "method 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);
        }
    }
}
Public Class MyClass1
    Public Sub New(ByVal i As Integer)
    End Sub
    Public Shared Sub Main()
        Try
            Dim myType As Type = GetType(MyClass1)
            Dim types(0) As Type
            types(0) = GetType(Integer)
            ' Get the public instance constructor that takes an integer parameter.
            Dim constructorInfoObj As ConstructorInfo = _
                        myType.GetConstructor(BindingFlags.Instance Or _
                        BindingFlags.Public, Nothing, _
                        CallingConventions.HasThis, types, Nothing)
            If Not (constructorInfoObj Is Nothing) Then
                Console.WriteLine("The constructor of MyClass1 that " + _
                                  "is a public instance method and takes an " + _
                                  "integer as a parameter is: ")
                Console.WriteLine(constructorInfoObj.ToString())
            Else
                Console.WriteLine("The constructor MyClass1 that " + _
                                  "is a public instance method and takes an " + _
                                  "integer as a parameter is not available.")
            End If
        Catch e As ArgumentNullException
            Console.WriteLine("ArgumentNullException: " + e.Message)
        Catch e As ArgumentException
            Console.WriteLine("ArgumentException: " + e.Message)
        Catch e As SecurityException
            Console.WriteLine("SecurityException: " + e.Message)
        Catch e As Exception
            Console.WriteLine("Exception: " + e.Message)
        End Try
    End Sub
End Class

注解

尽管默认联编程序不处理 ParameterModifiermodifiers 参数),但你可以使用 abstract System.Reflection.Binder 类编写处理 modifiers的自定义联编程序。Although the default binder does not process ParameterModifier (the modifiers parameter), you can use the abstract System.Reflection.Binder class to write a custom binder that does process modifiers. 仅当通过 COM 互操作进行调用时才使用 ParameterModifier,且仅处理通过引用传递的参数。ParameterModifier is only used when calling through COM interop, and only parameters that are passed by reference are handled.

如果不存在完全匹配项,binder 将尝试强制转换 types 数组中指定的参数类型,以便选择匹配项。If an exact match does not exist, the binder will attempt to coerce the parameter types specified in the types array in order to select a match. 如果 binder 无法选择匹配项,则返回 nullIf the binder is unable to select a match, then null is returned.

以下 BindingFlags 筛选器标志可用于定义要包括在搜索中的构造函数:The following BindingFlags filter flags can be used to define which constructors to include in the search:

  • 若要获取返回,必须指定 BindingFlags.InstanceBindingFlags.StaticYou must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.

  • 指定 BindingFlags.Public 以在搜索中包括公共构造函数。Specify BindingFlags.Public to include public constructors in the search.

  • 指定 BindingFlags.NonPublic 以在搜索中包括非公共构造函数(即私有、内部和受保护的构造函数)。Specify BindingFlags.NonPublic to include non-public constructors (that is, private, internal, and protected constructors) in the search.

有关更多信息,请参见System.Reflection.BindingFlagsSee System.Reflection.BindingFlags for more information.

若要使用此方法获取类初始值设定项(静态构造函数),必须|指定 BindingFlags.Static BindingFlags.NonPublicBindingFlags.StaticOrBindingFlags.NonPublic 中的 Visual Basic)。To get the class initializer (static constructor) using this method, you must specify BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.StaticOrBindingFlags.NonPublic in Visual Basic). 还可以使用 TypeInitializer 属性获取类初始值设定项。You can also get the class initializer using the TypeInitializer property.

下表显示了在类型上反射时,Get 方法返回基类的成员。The following table shows what members of a base class are returned by the Get methods when reflecting on a type.

成员类型Member Type StaticStatic 非静态Non-Static
构造函数Constructor NoNo NoNo
字段Field NoNo 可以。Yes. 字段始终按名称和签名隐藏。A field is always hide-by-name-and-signature.
事件Event 不适用Not applicable 通用类型系统规则是指继承与实现属性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反射将属性视为隐藏的名称和签名。Reflection treats properties as hide-by-name-and-signature. 请参阅下面的注释2。See note 2 below.
方法Method NoNo 可以。Yes. 方法(虚拟和非虚拟)可按名称隐藏或按名称和签名隐藏。A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
嵌套类型Nested Type NoNo NoNo
属性Property 不适用Not applicable 通用类型系统规则是指继承与实现属性的方法相同。The common type system rule is that the inheritance is the same as that of the methods that implement the property. 反射将属性视为隐藏的名称和签名。Reflection treats properties as hide-by-name-and-signature. 请参阅下面的注释2。See note 2 below.
  1. 按名称和签名隐藏将考虑签名的所有部分,包括自定义修饰符、返回类型、参数类型、个 sentinel 和非托管调用约定。Hide-by-name-and-signature considers all of the parts of the signature, including custom modifiers, return types, parameter types, sentinels, and unmanaged calling conventions. 这是二进制比较。This is a binary comparison.

  2. 对于反射,属性和事件是按名称和签名隐藏的。For reflection, properties and events are hide-by-name-and-signature. 如果在基类中同时具有 get 访问器和 set 访问器的属性,但派生类只有 get 访问器,则派生类属性将隐藏基类属性,并且你将无法访问基类的资源库。If you have a property with both a get and a set accessor in the base class, but the derived class has only a get accessor, the derived class property hides the base class property, and you will not be able to access the setter on the base class.

  3. 自定义属性不属于通用类型系统。Custom attributes are not part of the common type system.

备注

在查找构造函数和方法时,不能忽略参数。You cannot omit parameters when looking up constructors and methods. 在调用时,只能省略参数。You can only omit parameters when invoking.

如果当前 Type 表示构造泛型类型,则此方法将返回类型参数替换为相应类型参数的 ConstructorInfoIf the current Type represents a constructed generic type, this method returns the ConstructorInfo with the type parameters replaced by the appropriate type arguments. 如果当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则此方法始终返回 nullIf the current Type represents a type parameter in the definition of a generic type or generic method, this method always returns null.

另请参阅

适用于