Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Type.GetConstructors Method (BindingFlags)

.NET Framework 1.1

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

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

Parameters

bindingAttr
A bitmask comprised of one or more BindingFlags that specify how the search is conducted.

-or-

Zero, to return a null reference (Nothing in Visual Basic).

Return Value

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.

-or-

An empty array of type ConstructorInfo, if no constructors are defined for the current Type, or if none of the defined constructors match the binding constraints.

Remarks

bindingAttr can be used to specify whether to return only public constructors or both public and non-public constructors.

If an exact match does not exist, the binder will attempt to coerce the parameter types specified in the types array in order to select a match. If the binder is unable to select a match, then a null reference (Nothing in Visual Basic) is returned.

The following BindingFlags filter flags can be used to define which constructors to include in the search:

  • You must specify either BindingFlags.Instance or BindingFlags.Static in order to get a return.
  • Specify BindingFlags.Public to include public constructors in the search.
  • Specify BindingFlags.NonPublic to include non-public constructors (that is, private and protected constructors) in the search.

See System.Reflection.BindingFlags for more information.

If the requested type is non-public and the caller does not have ReflectionPermission to reflect non-public objects outside the current assembly, this method returns a null reference (Nothing).

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

.NET Framework Security: 

See Also

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.