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 No No
欄位Field No 可以。Yes. 欄位永遠是依名稱和簽章隱藏。A field is always hide-by-name-and-signature.
Event - 事件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 和 unmanaged 呼叫慣例。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.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- 要傳回空陣列的 DefaultDefault 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多載,並將它傳遞 (邏輯 OR) 的組合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.

另請參閱

適用於