Type.GetConstructors() Method in C# with Examples (original) (raw)

Last Updated : 03 Feb, 2023

Type.GetConstructors() Method is used to get the constructors of the Type object. There are 2 methods in the overload list of this method as follows:

Type.GetConstructors() Method

This method is used to returns all the public constructors defined for the current Type.
Syntax:

public System.Reflection.ConstructorInfo[] GetConstructors ();

Returns: This method returns ConstructorInfo array objects representing all the public instance constructors defined for the current Type, but it does not include type initializer(static constructor).
Below programs illustrate the use of Type.GetConstructors() Method:
Example 1:

csharp

using System;

using System.Globalization;

using System.Reflection;

class GFG {

`` public static void Main()

`` {

`` object obj = new Object();

`` Type type = obj.GetType();

`` ConstructorInfo[] info = type.GetConstructors();

`` Console.WriteLine( "All constructors are shown below" );

`` for ( int i = 0; i < info.Length; i++)

`` Console.WriteLine(info[i]);

`` }

}

Output:

All constructors are shown below Void .ctor()

Example 2:

csharp

using System;

using System.Globalization;

using System.Reflection;

class GFG {

`` public static void Main()

`` {

`` Type type = typeof ( string );

`` ConstructorInfo[] info = type.GetConstructors();

`` Console.WriteLine( "All constructors are shown below" );

`` for ( int i = 0; i < info.Length; i++)

`` Console.WriteLine(info[i]);

`` }

}

Output:

All constructors are shown below Void .ctor(Char[]) Void .ctor(Char[], Int32, Int32) Void .ctor(Char*) Void .ctor(Char*, Int32, Int32) Void .ctor(SByte*) Void .ctor(SByte*, Int32, Int32) Void .ctor(SByte*, Int32, Int32, Encoding) Void .ctor(Char, Int32) Void .ctor(ReadOnlySpan`1)

Type.GetConstructors(BindingFlags) Method

This method is used to return constructors defined for the current Type, using the specified BindingFlags when overridden in a derived class,
Syntax:

public abstract System.Reflection.ConstructorInfo[] GetConstructors (System.Reflection.BindingFlags bindingAttr);

Here, it takes a bitmask comprised of one or more BindingFlags that specify at which point the attend is conducted. or Zero, to return null.
Below are some BindingFlags filter flags that can be used to define which constructors to include in the search:

Return Value: This method returns 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.
Below programs illustrate the use of the above-discussed method:
Example 1:

csharp

using System;

using System.Globalization;

using System.Reflection;

class GFG {

`` public static void Main()

`` {

`` Type type = typeof ( string );

`` BindingFlags bf = BindingFlags.Public

`` | BindingFlags.Static

`` | BindingFlags.NonPublic

`` | BindingFlags.Instance;

`` ConstructorInfo[] info = type.GetConstructors(bf);

`` Console.WriteLine( "All constructors are shown below" );

`` for ( int i = 0; i < info.Length; i++)

`` Console.WriteLine(info[i]);

`` }

}

Output:

All constructors are shown below Void .ctor(Char[]) Void .ctor(Char[], Int32, Int32) Void .ctor(Char*) Void .ctor(Char*, Int32, Int32) Void .ctor(SByte*) Void .ctor(SByte*, Int32, Int32) Void .ctor(SByte*, Int32, Int32, Encoding) Void .ctor(Char, Int32) Void .ctor(ReadOnlySpan`1)

Example 2:

csharp

using System;

using System.Globalization;

using System.Reflection;

class GFG {

`` public static void Main()

`` {

`` object obj = new Object();

`` Type type = obj.GetType();

`` BindingFlags bf = BindingFlags.Public

`` | BindingFlags.Static

`` | BindingFlags.NonPublic

`` | BindingFlags.Instance;

`` ConstructorInfo[] info = type.GetConstructors(bf);

`` Console.WriteLine( "All constructors are shown below" );

`` for ( int i = 0; i < info.Length; i++)

`` Console.WriteLine(info[i]);

`` }

}

Output:

All constructors are shown below Void .ctor()

Reference: