Type.GetConstructors Method

Definition

取得目前 Type 的建構函式。Gets the constructors of the current Type.

Overloads

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

Returns

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.

Implements

Attributes

Examples

這個範例會顯示具有兩個實例的函式和一個靜態的函式之類別中,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

Remarks

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 靜態Static 非靜態Non-Static
建構函式Constructor No No
欄位Field No 是。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 No 是。Yes. 方法(虛擬和非虛擬)可以依名稱或逐名稱和簽章來隱藏。A method (both virtual and non-virtual) can be hide-by-name or hide-by-name-and-signature.
巢狀型別Nested Type No No
屬性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. [逐名稱] 和 [簽章] 會考慮簽章的所有部分,包括自訂修飾詞、傳回類型、參數類型、sentinels 和非受控呼叫慣例。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 存取子,則衍生類別屬性會隱藏基類屬性,而且您將無法存取基類上的 setter。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.PublicOrBindingFlags.Instance)。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.StaticOrBindingFlags.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)Sub New(ByVal t1 As T) 在 Visual Basic 中),則在 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.

See also

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);
public abstract 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()

Parameters

bindingAttr
BindingFlags

列舉值的位元組合,用來指定搜尋的執行方式。A bitwise combination of the enumeration values that specify how the search is conducted.

-或--or- 要傳回空陣列的 DefaultDefault to return an empty array.

Returns

ConstructorInfo[]

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.

Implements

Attributes

Examples

這個範例會顯示具有兩個實例的函式和一個靜態的函式之類別中,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的組合(邏輯 OR)傳遞給它,如下列程式碼範例所示: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

Remarks

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.NonPublicSpecify 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 中),則在 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.

See also

Applies to