Export (0) Print
Expand All

Type::GetConstructor Method (array<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:
virtual ConstructorInfo^ GetConstructor(
	array<Type^>^ types
) sealed

Parameters

types
Type: array<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, nullptr.

Implements

_Type::GetConstructor(array<Type>)

ExceptionCondition
ArgumentNullException

types is nullptr.

-or-

One of the elements in types is nullptr.

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

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

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

using namespace System;
using namespace System::Reflection;
using namespace System::Security;
public ref class MyClass1
{
public:
   MyClass1(){}

   MyClass1( int i ){}

};

int main()
{
   try
   {
      Type^ myType = MyClass1::typeid;
      array<Type^>^types = gcnew array<Type^>(1);
      types[ 0 ] = int::typeid;

      // Get the constructor that takes an integer as a parameter.
      ConstructorInfo^ constructorInfoObj = myType->GetConstructor( types );
      if ( constructorInfoObj != nullptr )
      {
         Console::WriteLine( "The constructor of MyClass1 that takes an integer as a parameter is: " );
         Console::WriteLine( constructorInfoObj );
      }
      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: {0}", e->Source );
      Console::WriteLine( "Message: {0}", e->Message );
   }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Show:
© 2015 Microsoft