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.