Export (0) Print
Expand All

Type.GetConstructors Method ()

.NET Framework 1.1

Returns all the public constructors defined for the current Type.

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

Return Value

An array of ConstructorInfo objects representing all the public constructors defined for the current Type, including the type initializer if it is defined.

-or-

An empty array of type ConstructorInfo, if no public constructors are defined for the current Type.

Remarks

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. 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. 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. 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.

This method calls GetConstructors(BindingFlags.Public | BindingFlags.Instance). It will not find static constructors unless you explicitly pass BindingFlags.Static, as shown in the examples.

Class initializers are available through GetMember, GetMembers, FindMembers, and GetConstructors.

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

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Type Class | Type Members | System Namespace | Type.GetConstructors Overload List | ConstructorInfo | GetConstructor

Show:
© 2014 Microsoft