ConstructorInfo Class

Definition

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

[System.Runtime.InteropServices.ClassInterface(System.Runtime.InteropServices.ClassInterfaceType.None)]
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class ConstructorInfo : System.Reflection.MethodBase, System.Runtime.InteropServices._ConstructorInfo
Inheritance
Derived
Attributes
Implements

Inherited Members

System.Object

System.Reflection.MemberInfo

System.Reflection.MethodBase

Examples

The following example uses ConstructorInfo with GetConstructor and BindingFlags to find the constructors that match the specified search criteria.

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

};

int main()
{
   try
   {
      Type^ myType = MyClass1::typeid;
      array<Type^>^types = gcnew array<Type^>(1);
      types[ 0 ] = int::typeid;
      
      // Get the public instance constructor that takes an integer parameter.
      ConstructorInfo^ constructorInfoObj = myType->GetConstructor( static_cast<BindingFlags>(BindingFlags::Instance | BindingFlags::Public), nullptr, CallingConventions::HasThis, types, nullptr );
      if ( constructorInfoObj != nullptr )
      {
         Console::WriteLine( "The constructor of MyClass1 that is a public instance method and takes an integer as a parameter is: " );
         Console::WriteLine( constructorInfoObj );
      }
      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: {0}", e->Message );
   }
   catch ( ArgumentException^ e ) 
   {
      Console::WriteLine( "ArgumentException: {0}", e->Message );
   }
   catch ( SecurityException^ e ) 
   {
      Console::WriteLine( "SecurityException: {0}", e->Message );
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "Exception: {0}", e->Message );
   }
}
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);
        }
    }
}
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

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.

Note

ConstructorInfo inherits from MethodBase several members, such as IsGenericMethod, that can be used to examine generic methods. In the .NET Framework version 2.0 constructors cannot be generic, so these members return false or null.

Constructors

ConstructorInfo()

Initializes a new instance of the ConstructorInfo class.

Fields

ConstructorName

Represents the name of the class constructor method as it is stored in metadata. This name is always ".ctor". This field is read-only.

TypeConstructorName

Represents the name of the type constructor method as it is stored in metadata. This name is always ".cctor". This property is read-only.

Properties

MemberType

Gets a MemberTypes value indicating that this member is a constructor.

Methods

Equals(Object)

Returns a value that indicates whether this instance is equal to a specified object.

GetHashCode()

Returns the hash code for this instance.

Invoke(Object[])

Invokes the constructor reflected by the instance that has the specified parameters, providing default values for the parameters not commonly used.

Invoke(BindingFlags, Binder, Object[], CultureInfo)

When implemented in a derived class, invokes the constructor reflected by this ConstructorInfo with the specified arguments, under the constraints of the specified Binder.

Operators

Equality(ConstructorInfo, ConstructorInfo)

Indicates whether two ConstructorInfo objects are equal.

Inequality(ConstructorInfo, ConstructorInfo)

Indicates whether two ConstructorInfo objects are not equal.

Explicit Interface Implementations

_ConstructorInfo.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

Maps a set of names to a corresponding set of dispatch identifiers.

_ConstructorInfo.GetType()

Gets a Type object representing the ConstructorInfo type.

_ConstructorInfo.GetTypeInfo(UInt32, UInt32, IntPtr)

Retrieves the type information for an object, which can then be used to get the type information for an interface.

_ConstructorInfo.GetTypeInfoCount(UInt32)

Retrieves the number of type information interfaces that an object provides (either 0 or 1).

_ConstructorInfo.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Provides access to properties and methods exposed by an object.

_ConstructorInfo.Invoke_2(Object, BindingFlags, Binder, Object[], CultureInfo)

Provides COM objects with version-independent access to the Invoke(Object, BindingFlags, Binder, Object[], CultureInfo) method.

_ConstructorInfo.Invoke_3(Object, Object[])

Provides COM objects with version-independent access to the Invoke(Object, Object[]) method.

_ConstructorInfo.Invoke_4(BindingFlags, Binder, Object[], CultureInfo)

Provides COM objects with version-independent access to the Invoke(BindingFlags, Binder, Object[], CultureInfo) method.

_ConstructorInfo.Invoke_5(Object[])

Provides COM objects with version-independent access to the Invoke(Object[]) method.

Extension Methods

GetCustomAttribute(MemberInfo, Type)
GetCustomAttribute(MemberInfo, Type, Boolean)
GetCustomAttribute<T>(MemberInfo)
GetCustomAttribute<T>(MemberInfo, Boolean)
GetCustomAttributes(MemberInfo)
GetCustomAttributes(MemberInfo, Boolean)
GetCustomAttributes(MemberInfo, Type)
GetCustomAttributes(MemberInfo, Type, Boolean)
GetCustomAttributes<T>(MemberInfo)
GetCustomAttributes<T>(MemberInfo, Boolean)
IsDefined(MemberInfo, Type)
IsDefined(MemberInfo, Type, Boolean)
GetMetadataToken(MemberInfo)
HasMetadataToken(MemberInfo)

Thread Safety

This type is thread safe.