C# TypeInfo GetConstructor(Type[])
Description
TypeInfo GetConstructor(Type[])
Searches for a public
instance constructor whose parameters match the types in the specified
array.
Syntax
TypeInfo.GetConstructor(Type[])
has the following syntax.
[ComVisibleAttribute(true)]
public ConstructorInfo GetConstructor(
Type[] types
)
Parameters
TypeInfo.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
TypeInfo.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
using System;//w ww . ja v a 2 s.com
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());
}
}
}
The code above generates the following result.