Export (0) Print
Expand All

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
)
/** @attribute ComVisibleAttribute(true) */ 
public final ConstructorInfo GetConstructor (
	Type[] types
)
ComVisibleAttribute(true) 
public final function GetConstructor (
	types : Type[]
) : ConstructorInfo

Parameters

types

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

A ConstructorInfo object representing the public instance constructor whose parameters match the types in the parameter type array, if found; otherwise, a null reference (Nothing in Visual Basic).

Exception typeCondition

ArgumentNullException

types is a null reference (Nothing in Visual Basic).

-or-

One of the elements in types is a null reference (Nothing in Visual Basic).

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.StaticOrBindingFlags.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 a null reference (Nothing in Visual Basic).

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 a null reference (Nothing in Visual Basic).

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);
        }
    }
}

import System.*;
import System.Reflection.*;
import System.Security.*;

public class MyClass1
{
    public MyClass1()
    {
    } //MyClass1

    public MyClass1(int i)
    {
    } //MyClass1

    public static void main(String[] args)
    {
        try {
            Type myType = MyClass1.class.ToType();
            Type types[] = new Type[1];
            types.set_Item(0, int.class.ToType());
            // 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 (System.Exception e) {
            Console.WriteLine("Exception caught.");
            Console.WriteLine("Source: " + e.get_Source());
            Console.WriteLine("Message: " + e.get_Message());
        }
    } //main
} //MyClass1

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft