Type.GetConstructors 方法

定义

获取当前 Type 的构造函数。Gets the constructors of the current Type.

重载

GetConstructors()

返回为当前 Type 定义的所有公共构造函数。Returns all the public constructors defined for the current Type.

GetConstructors(BindingFlags)

当在派生类中重写时,使用指定 Type 搜索为当前 BindingFlags 定义的构造函数。When overridden in a derived class, searches for the constructors defined for the current Type, using the specified BindingFlags.

GetConstructors()

返回为当前 Type 定义的所有公共构造函数。Returns all the public constructors defined for the current Type.

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

返回

ConstructorInfo 对象的数组,表示当前 Type 定义的所有公共实例构造函数,但不包括类型初始值设定项(静态构造函数)。An array of ConstructorInfo objects representing all the public instance constructors defined for the current Type, but not including the type initializer (static constructor). 如果没有为当前 Type 定义公共实例构造函数,或者当前 Type 表示泛型类型或泛型方法的定义中的类型参数,则返回 ConstructorInfo 类型的空数组。If no public instance constructors are defined for the current Type, or if the current Type represents a type parameter in the definition of a generic type or generic method, an empty array of type ConstructorInfo is returned.

实现

属性

示例

此示例显示具有两个实例构造函数和一个静态构造函数的类的 GetConstructors 重载的输出。This example shows the output of the GetConstructors overload from a class that has two instance constructors and one static constructor.

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

   static t(){}

   t( int /*i*/ ){}

};

int main()
{
   array<ConstructorInfo^>^p = t::typeid->GetConstructors();
   Console::WriteLine( p->Length );
   for ( int i = 0; i < p->Length; i++ )
   {
      Console::WriteLine( p[ i ]->IsStatic );

   }
}
using System;
using System.Reflection;

public class t {
    public t() {}
    static t() {}
    public t(int i) {}

    public static void Main() {
        ConstructorInfo[] p = typeof(t).GetConstructors();
        Console.WriteLine(p.Length);

        for (int i=0;i<p.Length;i++) {
            Console.WriteLine(p[i].IsStatic);
        }
    }
}
Imports System.Reflection

Public Class t
    
    Public Sub New()
    End Sub
    
    Shared Sub New()
    End Sub
    
    Public Sub New(i As Integer)
    End Sub
     
    Public Shared Sub Main()
        Dim p As ConstructorInfo() = GetType(t).GetConstructors()
        Console.WriteLine(p.Length)
        
        Dim i As Integer
        For i = 0 To p.Length - 1
            Console.WriteLine(p(i).IsStatic)
        Next i
    End Sub
End Class

此代码的输出为:The output of this code is:

22

FalseFalse

FalseFalse

因为 GetConstructors() 重载仅使用 BindingFlags.PublicBindingFlags.Instance,所以静态构造函数既不由 for 表达式进行计数,也不由 IsStatic计算。Because the GetConstructors() overload uses only BindingFlags.Public and BindingFlags.Instance, the static constructor is neither counted by the for expression nor evaluated by IsStatic.

若要查找静态构造函数,请使用 GetConstructors(BindingFlags) 重载,并传递 BindingFlags.PublicBindingFlags.StaticBindingFlags.NonPublicBindingFlags.Instance的组合(逻辑 OR),如下面的代码示例所示:To find static constructors, use the GetConstructors(BindingFlags) overload, and pass the combination (logical OR) of BindingFlags.Public, BindingFlags.Static, BindingFlags.NonPublic, BindingFlags.Instance, as shown in the following code example:

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

   t( int /*i*/ ){}

   static t(){}

};

int main()
{
   array<ConstructorInfo^>^p = t::typeid->GetConstructors( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Static | BindingFlags::NonPublic | BindingFlags::Instance) );
   Console::WriteLine( p->Length );
   for ( int i = 0; i < p->Length; i++ )
   {
      Console::WriteLine( p[ i ]->IsStatic );
   }
}
using System;
using System.Reflection;
 
public class t {
    public t() {}
    static t() {}
    public t(int i) {}

    public static void Main() {
        ConstructorInfo[] p = typeof(t).GetConstructors(
           BindingFlags.Public | BindingFlags.Static |
           BindingFlags.NonPublic | BindingFlags.Instance);
        Console.WriteLine(p.Length);

        for (int i=0;i<p.Length;i++) {
            Console.WriteLine(p[i].IsStatic);
        }
    }
}
Imports System.Reflection

Public Class t
    
    Public Sub New()
    End Sub
    
    Shared Sub New()
    End Sub
    
    Public Sub New(i As Integer)
    End Sub
     
    Public Shared Sub Main()
        Dim p As ConstructorInfo() = GetType(T).GetConstructors( _
           BindingFlags.Public Or _
           BindingFlags.Static Or _
           BindingFlags.NonPublic Or _
           BindingFlags.Instance)
        Console.WriteLine(p.Length)
        
        Dim i As Integer
        For i = 0 To p.Length - 1
            Console.WriteLine(p(i).IsStatic)
        Next i
    End Sub
End Class

输出如下:Now the output is:

33

FalseFalse

TrueTrue

FalseFalse

注解

GetConstructors 方法不按特定顺序(如声明顺序)返回构造函数。The GetConstructors method does not return constructors in a particular order, such as declaration order. 你的代码不能依赖于构造函数的返回顺序,因为该顺序会有所不同。Your code must not depend on the order in which constructors are returned, because that order varies.

下表显示了在类型上反射时,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.

此方法重载调用 GetConstructors(BindingFlags) 方法重载,BindingFlags.Public | BindingFlags.InstanceBindingFlags.PublicOr中的 BindingFlags.Instance Visual Basic)。This method overload calls the GetConstructors(BindingFlags) method overload, with BindingFlags.Public | BindingFlags.Instance (BindingFlags.PublicOrBindingFlags.Instance in Visual Basic). 它将找不到类初始值设定项(静态构造函数)。It will not find class initializers (static constructor). 若要查找类初始值设定项,请使用采用 BindingFlags的重载, |并指定 BindingFlags.Static BindingFlags.NonPublicBindingFlags.StaticOr中的 BindingFlags.NonPublic)。To find class initializers, 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.

如果当前 Type 表示构造泛型类型,则此方法返回 ConstructorInfo 对象,这些对象的类型参数被替换为相应的类型参数。If the current Type represents a constructed generic type, this method returns the ConstructorInfo objects with the type parameters replaced by the appropriate type arguments. 例如,如果类 C<T> 具有构造函数 C(T t1) (在 Visual Basic 中为Sub New(ByVal t1 As T)),则对 GetConstructors 调用 C<int> 将返回表示 ConstructorInfo 的C# C(int t1)Sub New(ByVal t1 As Integer) 中的 Visual Basic)。For example, if class C<T> has a constructor C(T t1) (Sub New(ByVal t1 As T) in Visual Basic), calling GetConstructors on C<int> returns a ConstructorInfo that represents C(int t1) in C# (Sub New(ByVal t1 As Integer) in Visual Basic).

如果当前 Type 表示泛型类型参数,则 GetConstructors 方法返回空数组。If the current Type represents a generic type parameter, the GetConstructors method returns an empty array.

另请参阅

GetConstructors(BindingFlags)

当在派生类中重写时,使用指定 Type 搜索为当前 BindingFlags 定义的构造函数。When overridden in a derived class, searches for the constructors defined for the current Type, using the specified BindingFlags.

public:
 abstract cli::array <System::Reflection::ConstructorInfo ^> ^ GetConstructors(System::Reflection::BindingFlags bindingAttr);
[System.Runtime.InteropServices.ComVisible(true)]
public abstract System.Reflection.ConstructorInfo[] GetConstructors (System.Reflection.BindingFlags bindingAttr);
abstract member GetConstructors : System.Reflection.BindingFlags -> System.Reflection.ConstructorInfo[]
Public MustOverride Function GetConstructors (bindingAttr As BindingFlags) As ConstructorInfo()

参数

bindingAttr
BindingFlags

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

-or- 若为 Default,则返回空数组。Default to return an empty array.

返回

表示为当前 ConstructorInfo 定义的匹配指定绑定约束的所有构造函数的 Type 对象数组,包括类型初始值设定项(如果定义的话)。An array of ConstructorInfo objects representing all constructors defined for the current Type that match the specified binding constraints, including the type initializer if it is defined. 如果当前 ConstructorInfo 没有定义构造函数,或者定义的构造函数都不符合绑定约束,或者当前 Type 表示泛型类型或泛型方法定义的类型参数,则返回 Type 类型的空数组。Returns an empty array of type ConstructorInfo if no constructors are defined for the current Type, if none of the defined constructors match the binding constraints, or if the current Type represents a type parameter in the definition of a generic type or generic method.

实现

属性

示例

此示例显示具有两个实例构造函数和一个静态构造函数的类的 GetConstructors() 重载的输出。This example shows the output of the GetConstructors() overload from a class that has two instance constructors and one static constructor.

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

   static t(){}

   t( int /*i*/ ){}

};

int main()
{
   array<ConstructorInfo^>^p = t::typeid->GetConstructors();
   Console::WriteLine( p->Length );
   for ( int i = 0; i < p->Length; i++ )
   {
      Console::WriteLine( p[ i ]->IsStatic );

   }
}
using System;
using System.Reflection;

public class t {
    public t() {}
    static t() {}
    public t(int i) {}

    public static void Main() {
        ConstructorInfo[] p = typeof(t).GetConstructors();
        Console.WriteLine(p.Length);

        for (int i=0;i<p.Length;i++) {
            Console.WriteLine(p[i].IsStatic);
        }
    }
}
Imports System.Reflection

Public Class t
    
    Public Sub New()
    End Sub
    
    Shared Sub New()
    End Sub
    
    Public Sub New(i As Integer)
    End Sub
     
    Public Shared Sub Main()
        Dim p As ConstructorInfo() = GetType(t).GetConstructors()
        Console.WriteLine(p.Length)
        
        Dim i As Integer
        For i = 0 To p.Length - 1
            Console.WriteLine(p(i).IsStatic)
        Next i
    End Sub
End Class

此代码的输出为:The output of this code is:

22

FalseFalse

FalseFalse

因为 GetConstructors 重载仅使用 PublicInstance,所以静态构造函数既不由 for 表达式进行计数,也不由 IsStatic计算。Because the GetConstructors overload uses only Public and Instance, the static constructor is neither counted by the for expression nor evaluated by IsStatic.

若要查找静态构造函数,请使用 GetConstructors 重载,并向其传递 BindingFlags.PublicBindingFlags.StaticBindingFlags.NonPublicBindingFlags.Instance的组合(逻辑或),如下面的代码示例所示:To find static constructors, use the GetConstructors overload, and pass it the combination (logical OR) of BindingFlags.Public, BindingFlags.Static, BindingFlags.NonPublic, BindingFlags.Instance, as shown in the following code example:

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

   t( int /*i*/ ){}

   static t(){}

};

int main()
{
   array<ConstructorInfo^>^p = t::typeid->GetConstructors( static_cast<BindingFlags>(BindingFlags::Public | BindingFlags::Static | BindingFlags::NonPublic | BindingFlags::Instance) );
   Console::WriteLine( p->Length );
   for ( int i = 0; i < p->Length; i++ )
   {
      Console::WriteLine( p[ i ]->IsStatic );
   }
}
using System;
using System.Reflection;
 
public class t {
    public t() {}
    static t() {}
    public t(int i) {}

    public static void Main() {
        ConstructorInfo[] p = typeof(t).GetConstructors(
           BindingFlags.Public | BindingFlags.Static |
           BindingFlags.NonPublic | BindingFlags.Instance);
        Console.WriteLine(p.Length);

        for (int i=0;i<p.Length;i++) {
            Console.WriteLine(p[i].IsStatic);
        }
    }
}
Imports System.Reflection

Public Class t
    
    Public Sub New()
    End Sub
    
    Shared Sub New()
    End Sub
    
    Public Sub New(i As Integer)
    End Sub
     
    Public Shared Sub Main()
        Dim p As ConstructorInfo() = GetType(T).GetConstructors( _
           BindingFlags.Public Or _
           BindingFlags.Static Or _
           BindingFlags.NonPublic Or _
           BindingFlags.Instance)
        Console.WriteLine(p.Length)
        
        Dim i As Integer
        For i = 0 To p.Length - 1
            Console.WriteLine(p(i).IsStatic)
        Next i
    End Sub
End Class

输出如下:Now the output is:

33

FalseFalse

TrueTrue

FalseFalse

注解

bindingAttr 可用于指定是只返回公共构造函数还是返回公共和非公共构造函数。bindingAttr can be used to specify whether to return only public constructors or both public and non-public constructors.

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

  • 指定 BindingFlags.StaticBindingFlags.NonPublic 以检索类初始值设定项(静态构造函数)。Specify BindingFlags.Static along with BindingFlags.NonPublic to retrieve the class initializer (static constructor). 还可以使用 TypeInitializer 属性获取类初始值设定项。You can also get the class initializer using the TypeInitializer property.

  • 指定 BindingFlags.Instance 以及用于检索实例构造函数的一个或两个 BindingFlags.PublicBindingFlags.NonPublicSpecify BindingFlags.Instance along with one or both of BindingFlags.Public and BindingFlags.NonPublic to retrieve instance constructors.

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

GetConstructors 方法不按特定顺序(如声明顺序)返回构造函数。The GetConstructors method does not return constructors in a particular order, such as declaration order. 你的代码不能依赖于构造函数的返回顺序,因为该顺序会有所不同。Your code must not depend on the order in which constructors are returned, because that order varies.

如果当前 Type 表示构造泛型类型,则此方法返回 ConstructorInfo 对象,这些对象的类型参数被替换为相应的类型参数。If the current Type represents a constructed generic type, this method returns the ConstructorInfo objects with the type parameters replaced by the appropriate type arguments. 例如,如果类 C<T> 具有构造函数 C(T t1) (在 Visual Basic 中为Sub New(ByVal t1 As T)),则对 GetConstructors 调用 C<int> 将返回表示 ConstructorInfo 的C# C(int t1)Sub New(ByVal t1 As Integer) 中的 Visual Basic)。For example, if class C<T> has a constructor C(T t1) (Sub New(ByVal t1 As T) in Visual Basic), calling GetConstructors on C<int> returns a ConstructorInfo that represents C(int t1) in C# (Sub New(ByVal t1 As Integer) in Visual Basic).

如果当前 Type 表示泛型类型参数,则 GetConstructors 方法返回空数组。If the current Type represents a generic type parameter, the GetConstructors method returns an empty array.

另请参阅

适用于