Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ConstructorInfo Class

Discovers the attributes of a class constructor and provides access to constructor metadata.

For a list of all members of this type, see ConstructorInfo Members.

System.Object
   System.Reflection.MemberInfo
      System.Reflection.MethodBase
         System.Reflection.ConstructorInfo
            System.Reflection.Emit.ConstructorBuilder

[Visual Basic]
<Serializable>
<ClassInterface(ClassInterfaceType.AutoDual)>
MustInherit Public Class ConstructorInfo
   Inherits MethodBase
[C#]
[Serializable]
[ClassInterface(ClassInterfaceType.AutoDual)]
public abstract class ConstructorInfo : MethodBase
[C++]
[Serializable]
[ClassInterface(ClassInterfaceType::AutoDual)]
public __gc __abstract class ConstructorInfo : public MethodBase
[JScript]
public
   Serializable
 ClassInterface(ClassInterfaceType.AutoDual)
abstract class ConstructorInfo extends MethodBase

Thread Safety

This type is safe for multithreaded operations.

Remarks

ConstructorInfo is used to discover the attributes of a constructor as well as to invoke a constructor. Objects are created by calling Invoke on a ConstructorInfo returned by either the GetConstructors or GetConstructor method of a Type object.

Notes to Inheritors:  When you inherit from ConstructorInfo, you must override the following member: Invoke(BindingFlags, Binder, Object, CultureInfo).

Example

[Visual Basic, C#, C++] The following example uses ConstructorInfo with GetConstructor and BindingFlags to find the constructors that match the specified search criteria.

[Visual Basic] 
Public Class MyClass1
    Public Sub New(ByVal i As Integer)
    End Sub
    Public Shared Sub Main()
        Try
            Dim myType As Type = GetType(MyClass1)
            Dim types(0) As Type
            types(0) = GetType(Integer)
            ' Get the public instance constructor that takes an integer parameter.
            Dim constructorInfoObj As ConstructorInfo = _
                        myType.GetConstructor(BindingFlags.Instance Or _
                        BindingFlags.Public, Nothing, _
                        CallingConventions.HasThis, types, Nothing)
            If Not (constructorInfoObj Is Nothing) Then
                Console.WriteLine("The constructor of MyClass1 that " + _
                                  "is a public instance method and takes an " + _
                                  "integer as a parameter is: ")
                Console.WriteLine(constructorInfoObj.ToString())
            Else
                Console.WriteLine("The constructor MyClass1 that " + _
                                  "is a public instance method and takes an " + _
                                  "integer as a parameter is not available.")
            End If
        Catch e As ArgumentNullException
            Console.WriteLine("ArgumentNullException: " + e.Message)
        Catch e As ArgumentException
            Console.WriteLine("ArgumentException: " + e.Message)
        Catch e As SecurityException
            Console.WriteLine("SecurityException: " + e.Message)
        Catch e As Exception
            Console.WriteLine("Exception: " + e.Message)
        End Try
    End Sub
End Class

[C#] 
using System;
using System.Reflection;
using System.Security;

public class 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 public instance constructor that takes an integer parameter.
            ConstructorInfo constructorInfoObj = myType.GetConstructor(
                BindingFlags.Instance | BindingFlags.Public, null,
                CallingConventions.HasThis, types, null);
            if(constructorInfoObj != null)
            {
                Console.WriteLine("The constructor of MyClass1 that is a public " +
                    "instance method and takes an integer as a parameter is: ");
                Console.WriteLine(constructorInfoObj.ToString());
            }
            else
            {
                Console.WriteLine("The constructor of MyClass1 that is a public instance " +
                    "method and takes an integer as a parameter is not available.");
            }
        }
        catch(ArgumentNullException e)
        {
            Console.WriteLine("ArgumentNullException: " + e.Message);
        }
        catch(ArgumentException e)
        {
            Console.WriteLine("ArgumentException: " + e.Message);
        }
        catch(SecurityException e)
        {
            Console.WriteLine("SecurityException: " + e.Message);
        }
        catch(Exception e)
        {
            Console.WriteLine("Exception: " + e.Message);
        }
    }
}

[C++] 
#using <mscorlib.dll>

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

public __gc class MyClass1 {
public:
   MyClass1(int i) {}
}
;
int main() {
   try {
      Type*  myType = __typeof(MyClass1);
      Type* types[] = new Type*[1];
      types->Item[0] = __typeof(int);
      // Get the public instance constructor that takes an integer parameter.
      ConstructorInfo*  constructorInfoObj = myType->GetConstructor(static_cast<BindingFlags>(BindingFlags::Instance | BindingFlags::Public), 0,
         CallingConventions::HasThis, types, 0);
      if (constructorInfoObj != 0) {
         Console::WriteLine(S"The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is: ");
         Console::WriteLine(constructorInfoObj);
      } else {
         Console::WriteLine(S"The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is not available.");
      }
   } catch (ArgumentNullException* e) {
      Console::WriteLine(S"ArgumentNullException: {0}", e->Message);
   } catch (ArgumentException* e) {
      Console::WriteLine(S"ArgumentException: {0}", e->Message);
   } catch (SecurityException* e) {
      Console::WriteLine(S"SecurityException: {0}", e->Message);
   } catch (Exception* e) {
      Console::WriteLine(S"Exception: {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

Namespace: System.Reflection

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

Assembly: Mscorlib (in Mscorlib.dll)

See Also

ConstructorInfo Members | System.Reflection Namespace

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft