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

Description

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

Syntax

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


[ComVisibleAttribute(true)]/*from  w ww.  jav a2 s.  c o m*/
public ConstructorInfo GetConstructor(
  BindingFlags bindingAttr,
  Binder binder,
  CallingConventions callConvention,
  Type[] types,
  ParameterModifier[] modifiers
)

Returns

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

Example

The following example obtains the type of MyClass1, gets the ConstructorInfo object that matches the specified binding flags, and displays the constructor signature.


using System;/*from  ww w.  j  a  v a2  s . c o m*/
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,
                CallingConventions.HasThis, types, null);
            if(constructorInfoObj != null)
            {
                Console.WriteLine(constructorInfoObj.ToString());
            }
            else
            {
                Console.WriteLine("The constructor of MyClass1 that is a public instance " +
                    "method 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