Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

Type.GetConstructor Method (Type[])

.NET Framework 1.1

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

[Visual Basic]
Overloads Public Function GetConstructor( _
   ByVal types() As Type _
) As ConstructorInfo
[C#]
public ConstructorInfo GetConstructor(
 Type[] types
);
[C++]
public: ConstructorInfo* GetConstructor(
 Type* types[]
);
[JScript]
public function GetConstructor(
   types : Type[]
) : ConstructorInfo;

Parameters

types
An array of Type objects representing the number, order, and type of the parameters for the constructor to get.

-or-

An empty array of the type Type to get a constructor that takes no parameters.

-or-

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

Exceptions

Exception Type Condition
ArgumentNullException types is a null reference (Nothing in Visual Basic).

-or-

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

ArgumentException types is multidimensional.

Remarks

GetConstructor looks for public instance constructors and cannot be used to obtain a class initializer. Class initializers are available through GetMember, GetMembers, FindMembers, GetConstructors, and TypeInitializer.

If the requested constructor is non-public, this method returns a null reference (Nothing in Visual Basic).

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

Example

[Visual Basic, C#, C++] The following example obtains the type of MyClass, gets the ConstructorInfo object, and displays the constructor signature.

[Visual Basic] 
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

[C#] 

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

[C++] 

#using <mscorlib.dll>

using namespace System;
using namespace System::Reflection;
using namespace System::Security;

public __gc class MyClass1 {
public:
   MyClass1() {}
   MyClass1(int i) {}
};

int main() {
   try {
      Type*  myType = __typeof(MyClass1);
      Type* types[] = new Type*[1];
      types->Item[0] = __typeof(int);
      // Get the constructor that takes an integer as a parameter.
      ConstructorInfo*  constructorInfoObj = myType->GetConstructor(types);
      if (constructorInfoObj != 0) {
         Console::WriteLine(S"The constructor of MyClass1 that takes an integer as a parameter is: ");
         Console::WriteLine(constructorInfoObj);
      } else {
         Console::WriteLine(S"The constructor of MyClass1 that takes an integer as a parameter is not available.");
      }
   } catch (Exception* e) {
      Console::WriteLine(S"Exception caught.");
      Console::WriteLine(S"Source: {0}", e->Source);
      Console::WriteLine(S"Message: {0}", e->Message);
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

Type Class | Type Members | System Namespace | Type.GetConstructor Overload List | ConstructorInfo | DefaultBinder | ReflectionPermission | GetConstructorImpl | GetConstructors

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.