ConstructorInfo Class
Assembly: mscorlib (in mscorlib.dll)
'Declaration <SerializableAttribute> _ <ComVisibleAttribute(True)> _ <ClassInterfaceAttribute(ClassInterfaceType.None)> _ Public MustInherit Class ConstructorInfo Inherits MethodBase Implements _ConstructorInfo 'Usage Dim instance As ConstructorInfo
/** @attribute SerializableAttribute() */ /** @attribute ComVisibleAttribute(true) */ /** @attribute ClassInterfaceAttribute(ClassInterfaceType.None) */ public abstract class ConstructorInfo extends MethodBase implements _ConstructorInfo
SerializableAttribute ComVisibleAttribute(true) ClassInterfaceAttribute(ClassInterfaceType.None) public abstract class ConstructorInfo extends MethodBase implements _ConstructorInfo
Not applicable.
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 a null reference (Nothing in Visual Basic). |
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
import System.*;
import System.Reflection.*;
import System.Security.*;
public class MyClass1
{
public MyClass1(int i)
{
} //MyClass1
public static void main(String[] args)
{
try {
Type myType = MyClass1.class.ToType();
Type types[] = new Type[1];
types.set_Item(0, int.class.ToType());
// 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.get_Message());
}
catch (ArgumentException e) {
Console.WriteLine("ArgumentException: " + e.get_Message());
}
catch (SecurityException e) {
Console.WriteLine("SecurityException: " + e.get_Message());
}
catch (System.Exception e) {
Console.WriteLine("Exception: " + e.get_Message());
}
} //main
} //MyClass1
System.Reflection.MemberInfo
System.Reflection.MethodBase
System.Reflection.ConstructorInfo
Microsoft.JScript.JSConstructor
System.Reflection.Emit.ConstructorBuilder
Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.
Note: