Type.GetConstructors Type.GetConstructors Type.GetConstructors Type.GetConstructors Method

定义

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

重载

GetConstructors() GetConstructors() GetConstructors() GetConstructors()

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

GetConstructors(BindingFlags) GetConstructors(BindingFlags) GetConstructors(BindingFlags) GetConstructors(BindingFlags)

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

GetConstructors() GetConstructors() GetConstructors() 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
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表达式的计算,也不IsStaticBecause 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)重载,并将传递组合 (逻辑OR) 的BindingFlags.PublicBindingFlags.StaticBindingFlags.NonPublicBindingFlags.Instance,下面的代码示例中所示: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
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
PropertyProperty 不适用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. 按名称和签名隐藏会考虑所有的签名,包括自定义修饰符,部分返回类型、 参数类型、 标记和非托管调用约定。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.Instance (BindingFlags.Public Or 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.NonPublic (BindingFlags.Static Or BindingFlags.NonPublic在 Visual Basic 中)。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)(Sub New(ByVal t1 As T)在 Visual Basic 中),则调用GetConstructorsC<int>返回ConstructorInfo表示C(int t1)C# 中 (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) GetConstructors(BindingFlags) GetConstructors(BindingFlags) 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 BindingFlags BindingFlags 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
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表达式的计算,也不IsStaticBecause 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
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.Static连同BindingFlags.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.NonPublic检索实例构造函数。Specify 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)(Sub New(ByVal t1 As T)在 Visual Basic 中),则调用GetConstructorsC<int>返回ConstructorInfo表示C(int t1)C# 中 (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.

另请参阅

适用于