ConstructorInfo Class
Discovers the attributes of a class constructor and provides access to constructor metadata.
Assembly: mscorlib (in mscorlib.dll)
'Declaration <SerializableAttribute> _ <ComVisibleAttribute(True)> _ <ClassInterfaceAttribute(ClassInterfaceType.None)> _ <PermissionSetAttribute(SecurityAction.InheritanceDemand, Name := "FullTrust")> _ Public MustInherit Class ConstructorInfo _ Inherits MethodBase _ Implements _ConstructorInfo 'Usage Dim instance As ConstructorInfo
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 Nothing. |
When you inherit from ConstructorInfo, you must override the following member overload: Invoke(BindingFlags, Binder, Object(), CultureInfo).
The following example uses ConstructorInfo with GetConstructor and BindingFlags to find the constructors that match the specified search criteria.
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
System.Reflection.MemberInfo
System.Reflection.MethodBase
System.Reflection.ConstructorInfo
Microsoft.JScript.JSConstructor
System.Reflection.Emit.ConstructorBuilder
Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune
The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Note: