C# Type GetConstructor(Type[])
Description
Type GetConstructor(Type[])
searches for a public instance
constructor whose parameters match the types in the specified array.
Syntax
Type.GetConstructor(Type[])
has the following syntax.
[ComVisibleAttribute(true)]
public ConstructorInfo GetConstructor(
Type[] types
)
Parameters
Type.GetConstructor(Type[])
has the following parameters.
types
- An array of Type objects representing the number, order, and type of the parameters for the desired constructor.types
- -or-types
- An empty array of Type objects, to get a constructor that takes no parameters. Such an empty array is provided by the static field Type.EmptyTypes.
Returns
Type.GetConstructor(Type[])
method returns An object representing the public instance constructor whose parameters
match the types in the parameter type array, if found; otherwise, null.
Example
The following example obtains the type of MyClass, gets the ConstructorInfo object, and displays the constructor signature.
using System;//from w w w .ja va 2 s. c om
using System.Reflection;
using System.Security;
public class MyClass1
{
public 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(types);
if (constructorInfoObj != null)
{
Console.WriteLine("The constructor of MyClass1 that takes an " +
"integer as a parameter is: ");
Console.WriteLine(constructorInfoObj.ToString());
}
else
{
Console.WriteLine("The constructor of MyClass1 that takes an integer " +
"as a parameter is not available.");
}
}
}
The code above generates the following result.