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() 會顯示具有兩個實例的函式和一個靜態的函式之類別的多載輸出。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:

2
False
False

因為多載 GetConstructorsPublic 會使用和,所以靜態的函式 Instance 不會由運算式計算,也不會 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.Public BindingFlags.Static BindingFlags.NonPublic BindingFlags.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:

3
False
True
False

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

傳回

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 ,例如宣告順序。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.

下表顯示在反映型別時,方法會傳回基類的成員 GetThe 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. 依名稱和簽章會考慮簽章的所有部分,包括自訂修飾詞、傳回類型、參數類型、個 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 存取子,則衍生類別屬性會隱藏基類屬性,而且您將無法存取基類的 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). 您也可以使用屬性取得類別初始化運算式 TypeInitializerYou 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(int t1) Visual Basic) 中 c # (的 Sub New(ByVal t1 As Integer)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);
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[]
[<System.Runtime.InteropServices.ComVisible(true)>]
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- 要傳回空陣列的 DefaultDefault to return an empty array.

傳回

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.

實作

屬性

備註

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). 您也可以使用屬性取得類別初始化運算式 TypeInitializerYou 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) 在 Visual Basic) 中有一個 (的函式,則在中 Sub New(ByVal t1 As T) 呼叫 GetConstructors 會傳回 C<int> ConstructorInfo 代表 C(int t1) Visual Basic) 中 c # (的 Sub New(ByVal t1 As Integer)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.

另請參閱

適用於

產品 推出的版本