Export (0) Print
Expand All

Type.GetConstructors Method

Gets the constructors of the current Type.

Overload List

Returns all the public constructors defined for the current Type.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public Function GetConstructors() As ConstructorInfo()
[C#] public ConstructorInfo[] GetConstructors();
[C++] public: ConstructorInfo* GetConstructors() [];
[JScript] public function GetConstructors() : ConstructorInfo[];

When overridden in a derived class, searches for the constructors defined for the current Type, using the specified BindingFlags.

Supported by the .NET Compact Framework.

[Visual Basic] Overloads Public MustOverride Function GetConstructors(BindingFlags) As ConstructorInfo()
[C#] public abstract ConstructorInfo[] GetConstructors(BindingFlags);
[C++] public: virtual ConstructorInfo* GetConstructors(BindingFlags) [] = 0;
[JScript] public abstract function GetConstructors(BindingFlags) : ConstructorInfo[];

Example

This example shows the output of GetConstructors() from a class that has two instance constructors and one static constructor.

[Visual Basic] 
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

[C#] 
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);
        }
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;

public __gc class t {
public:
    t() {}
    static t() {}
    t(int /*i*/) {}
};

int main() {
    ConstructorInfo* p[] = __typeof(t)->GetConstructors();
    Console::WriteLine(p->Length);

    for (int i=0;i<p->Length;i++) {
        Console::WriteLine(p[i]->IsStatic);
    }
}

[JScript] 
import System;
import System.Reflection;

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

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

        for (var i : int =0;i<p.Length;i++) {
            Console.WriteLine(p[i].IsStatic);
        }
    }
}
t.Main();

The output of this code is:

2

False

False

Because GetConstructors() only calls the Public and Instance BindingFlags, the static constructor is neither counted by the for expression nor evaluated by IsStatic.

Use GetConstructors (BindingFlags) and pass it BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance to find static constructors, as follows:

[Visual Basic] 
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

[C#] 
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);
        }
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Reflection;

public __gc class t {
public:
    t() {}
    t(int /*i*/) {}
    static t() {}
};

int main() {
    ConstructorInfo* p[] = __typeof(t)->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);
    }
}

[JScript] 
import System;
import System.Reflection;
 
public class t {
    public function t() {}
    static t {}
    public function t(i : int) {}

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

        for (var i:int=0;i<p.Length;i++) {
            Console.WriteLine(p[i].IsStatic);
        }
    }
}
t.Main();

Now the output is:

3

False

True

False

See Also

Type Class | Type Members | System Namespace

Show:
© 2014 Microsoft