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)

'Declaration
<ComVisibleAttribute(True)> _
Public Function GetConstructor ( _
	types As Type() _
) As ConstructorInfo

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, Nothing.

Implements

_Type.GetConstructor(Type())

ExceptionCondition
ArgumentNullException

types is Nothing.

-or-

One of the elements in types is Nothing.

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 Nothing.

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 Nothing.

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

Imports System
Imports System.Reflection
Imports System.Security
Imports Microsoft.VisualBasic

Public Class MyClass1

    Public Sub New()
    End Sub 'New 

    Public Sub New(ByVal i As Integer)
    End Sub 'New 

    Public Shared Sub Main()
        Try 
            Dim myType As Type = GetType(MyClass1)
            Dim types(0) As Type
            types(0) = GetType(Int32)
            ' Get the constructor that takes an integer as a parameter. 
            Dim constructorInfoObj As ConstructorInfo = myType.GetConstructor(types)
            If Not (constructorInfoObj Is Nothing) Then
                Console.WriteLine("The constructor of MyClass that takes an integer as a parameter is: ")
                Console.WriteLine(constructorInfoObj.ToString())
            Else
                Console.WriteLine("The constructor of MyClass that takes no " + "parameters is not available.")
            End If 

        Catch e As Exception
            Console.WriteLine("Exception caught.")
            Console.WriteLine(("Source: " + e.Source))
            Console.WriteLine(("Message: " + e.Message))
        End Try 
    End Sub 'Main
End Class 'MyClass1

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 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

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft