C# Type GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[])

Description

Type GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[]) searches for a constructor whose parameters match the specified argument types and modifiers, using the specified binding constraints.

Syntax

Type.GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[]) has the following syntax.


[ComVisibleAttribute(true)]//  w  w  w .j  av a  2 s.  c  om
public ConstructorInfo GetConstructor(
  BindingFlags bindingAttr,
  Binder binder,
  Type[] types,
  ParameterModifier[] modifiers
)

Returns

Type.GetConstructor(BindingFlags, Binder, Type[], ParameterModifier[]) method returns A ConstructorInfo object representing the constructor that matches the specified requirements, if found; otherwise, null.

Example

The following program obtains the type of MyClass1 class, gets the ConstructorInfo object matching the specified binding flags, and displays the signature of the constructor.


using System;/*from  w w w  .  j a  va 2  s.com*/
using System.Reflection;
using System.Security;


public class MyClass1
{
    public MyClass1(int i){}
    public static void Main()
    {
            Type myType = typeof(MyClass1);
            Type[] types = new Type[1];
            types[0] = typeof(int);

            ConstructorInfo constructorInfoObj = myType.GetConstructor(
                BindingFlags.Instance | BindingFlags.Public, null, types, null);
            if (constructorInfoObj != null )
            {
                Console.WriteLine(constructorInfoObj.ToString());
            }
            else
            {
                Console.WriteLine("The constructor of the MyClass1 that is public " +
                    "and takes an integer as a parameter is not available.");
            }
    }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version