Type.GetConstructors() Method in C# with Examples

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:

  • GetConstructors() Method
  • GetConstructors(BindingFlags) Method

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetConstructors() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and intializing object
        object obj = new Object();
  
        // Getting the type of obj
        // using GetType() method
        Type type = obj.GetType();
  
        // Getting constructors of the current Type.
        // using GetArrayRank() Method
        ConstructorInfo[] info = type.GetConstructors();
  
        // Display all the public instance constructors
        Console.WriteLine("All constructors are shown below");
  
        for (int i = 0; i < info.Length; i++)
            Console.WriteLine(info[i]);
    }
}

chevron_right


Output:

All constructors are shown below
Void .ctor()

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetConstructors() Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and intializing Type object
        Type type = typeof(string);
  
        // Getting constructors of the current Type.
        // using GetConstructors() Method
        ConstructorInfo[] info = type.GetConstructors();
  
        // Display all the public instance constructors
        Console.WriteLine("All constructors are shown below");
  
        for (int i = 0; i < info.Length; i++)
            Console.WriteLine(info[i]);
    }
}

chevron_right


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:



  • 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, internal, and protected constructors) in the search. Constructors of base classes are not returned.

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetConstructors(BindingFlags) 
// Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Declaring and intializing Type object
        Type type = typeof(string);
  
        // Declaring and initializing BindingFlags
        // it is used to specify how the search
        // is conducted
        BindingFlags bf = BindingFlags.Public
                          | BindingFlags.Static
                          | BindingFlags.NonPublic
                          | BindingFlags.Instance;
  
        // Getting constructors of the current Type.
        // using GetConstructors() Method
        ConstructorInfo[] info = type.GetConstructors(bf);
  
        // Display all constructors
        Console.WriteLine("All constructors are shown below");
  
        for (int i = 0; i < info.Length; i++)
            Console.WriteLine(info[i]);
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Type.GetConstructors(BindingFlags)
// Method
using System;
using System.Globalization;
using System.Reflection;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // Creating and intializing object
        object obj = new Object();
  
        // Declaring and intializing Type object
        Type type = obj.GetType();
  
        // Declaring and intializing BindingFlags
        // it is used to specify how the search is conducted
        BindingFlags bf = BindingFlags.Public
                          | BindingFlags.Static
                          | BindingFlags.NonPublic
                          | BindingFlags.Instance;
  
        // Getting constructors of the current Type.
        // using GetConstructors() Method
        ConstructorInfo[] info = type.GetConstructors(bf);
  
        // Display all constructors
        Console.WriteLine("All constructors are shown below");
  
        for (int i = 0; i < info.Length; i++)
            Console.WriteLine(info[i]);
    }
}

chevron_right


Output:

All constructors are shown below
Void .ctor()

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.