Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Type.GetConstructor Method (Type[])

Searches for a public instance constructor whose parameters match the types in the specified array.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
[ComVisibleAttribute(true)]
public ConstructorInfo GetConstructor(
	Type[] types
)

Parameters

types
Type: System.Type[]
An array of Type objects representing the number, order, and type of the parameters for the desired constructor.
-or-
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.

Return Value

Type: System.Reflection.ConstructorInfo
An object representing the public instance constructor whose parameters match the types in the parameter type array, if found; otherwise, null.

Implements

_Type.GetConstructor(Type[])
ExceptionCondition
ArgumentNullException

types is null.

-or-

One of the elements in types is null.

ArgumentException

types is multidimensional.

This method overload looks for public instance constructors and cannot be used to obtain a class initializer (.cctor). To get a class initializer, use an overload that takes BindingFlags, and specify BindingFlags.Static | BindingFlags.NonPublic (BindingFlags.Static Or BindingFlags.NonPublic in Visual Basic). You can also get the class initializer using the TypeInitializer property.

If the requested constructor is non-public, this method returns null.

NoteNote

You cannot omit parameters when looking up constructors and methods. You can only omit parameters when invoking.

If the current Type represents a constructed generic type, this method returns the ConstructorInfo with the type parameters replaced by the appropriate type arguments. If the current Type represents a type parameter in the definition of a generic type or generic method, this method always returns null.

The following example obtains the type of MyClass, gets the ConstructorInfo object, and displays the constructor signature.



using System;
using System.Reflection;
using System.Security;

public class MyClass1
{
    public MyClass1(){}
    public MyClass1(int i){}

    public static void Main()
    {
        try
        {
            Type myType = typeof(MyClass1);
            Type[] types = new Type[1];
            types[0] = typeof(int);
            // Get the constructor that takes an integer as a parameter.
            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."); 
            }
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception caught.");
            Console.WriteLine("Source: " + e.Source);
            Console.WriteLine("Message: " + e.Message);
        }
    }
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.