Freigeben über


ConstructorInfo Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

Inheritance Hierarchy

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

Namespace:  System.Reflection
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.None)> _
Public MustInherit Class ConstructorInfo _
    Inherits MethodBase
[ComVisibleAttribute(true)]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
public abstract class ConstructorInfo : MethodBase

The ConstructorInfo type exposes the following members.

Constructors

  Name Description
Protected method ConstructorInfo Initializes a new instance of the ConstructorInfo class.

Top

Properties

  Name Description
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Attributes Gets the attributes associated with this method. (Inherited from MethodBase.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 CallingConvention Gets a value that indicates the calling conventions for this method. (Inherited from MethodBase.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 ContainsGenericParameters Gets a value that indicates whether the generic method contains unassigned generic type parameters. (Inherited from MethodBase.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 DeclaringType Gets the class that declares this member. (Inherited from MemberInfo.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsAbstract Gets a value that indicates whether the method is abstract (MustInherit in Visual Basic). (Inherited from MethodBase.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsAssembly Gets a value that indicates whether the potential visibility of this method or constructor is described by MethodAttributes.Assembly; that is, the method or constructor is visible at most to other types in the same assembly, and is not visible to derived types outside the assembly. (Inherited from MethodBase.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsConstructor Gets a value that indicates whether the method is a constructor. (Inherited from MethodBase.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsFamily Gets a value that indicates whether the visibility of this method or constructor is described by MethodAttributes.Family; that is, the method or constructor is visible only within its class and derived classes. (Inherited from MethodBase.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsFamilyAndAssembly Gets a value that indicates whether the visibility of this method or constructor is described by MethodAttributes.FamANDAssem; that is, the method or constructor can be called by derived classes, but only if they are in the same assembly. (Inherited from MethodBase.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsFamilyOrAssembly Gets a value that indicates whether the potential visibility of this method or constructor is described by MethodAttributes.FamORAssem; that is, the method or constructor can be called by derived classes wherever they are, and by classes in the same assembly. (Inherited from MethodBase.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsFinal Gets a value that indicates whether this method is final (NotOverridable in Visual Basic). (Inherited from MethodBase.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsGenericMethod Gets a value that indicates whether the method is generic. (Inherited from MethodBase.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsGenericMethodDefinition Gets a value that indicates whether the method is a generic method definition. (Inherited from MethodBase.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsHideBySig Gets a value that indicates whether only a member of the same kind with exactly the same signature is hidden in the derived class. (Inherited from MethodBase.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsPrivate Gets a value that indicates whether this member is private. (Inherited from MethodBase.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsPublic Gets a value that indicates whether this is a public method. (Inherited from MethodBase.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsSpecialName Gets a value that indicates whether this method has a special name. (Inherited from MethodBase.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsStatic Gets a value that indicates whether the method is static (Shared in Visual Basic). (Inherited from MethodBase.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsVirtual Gets a value that indicates whether the method is virtual (Overridable in Visual Basic). (Inherited from MethodBase.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberType Gets a MemberTypes value indicating that this member is a constructor. (Overrides MemberInfo.MemberType.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 MetadataToken Gets a value that identifies a metadata element. (Inherited from MemberInfo.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 MethodHandle Gets a handle to the internal metadata representation of a method. (Inherited from MethodBase.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Module Gets the module in which the type that declares the member represented by the current MemberInfo is defined. (Inherited from MemberInfo.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Name Gets the name of the current member. (Inherited from MemberInfo.)
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 ReflectedType Gets the class object that was used to obtain this instance of MemberInfo. (Inherited from MemberInfo.)

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetCustomAttributes(Boolean) When overridden in a derived class, returns an array of all custom attributes applied to this member. (Inherited from MemberInfo.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetCustomAttributes(Type, Boolean) When overridden in a derived class, returns an array of custom attributes applied to this member and identified by Type. (Inherited from MemberInfo.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetGenericArguments Returns an array of Type objects that represent the type arguments of a generic method or the type parameters of a generic method definition. (Inherited from MethodBase.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetMethodImplementationFlags When overridden in a derived class, returns the MethodImplAttributes flags. (Inherited from MethodBase.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetParameters When overridden in a derived class, gets the parameters of this method or constructor. (Inherited from MethodBase.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Invoke(array<Object[]) Invokes the current constructor with the specified arguments.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Invoke(Object, array<Object[]) Invokes the method or constructor represented by the current instance, using the specified parameters. (Inherited from MethodBase.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Invoke(BindingFlags, Binder, array<Object[], CultureInfo) When implemented in a derived class, invokes the current constructor with the specified arguments, using the specified binder and culture to validate the argument types.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Invoke(Object, BindingFlags, Binder, array<Object[], CultureInfo) When overridden in a derived class, invokes the reflected method or constructor with the given parameters. (Inherited from MethodBase.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 IsDefined When overridden in a derived class, indicates whether one or more attributes of the specified type or of its derived types is applied to this member. (Inherited from MemberInfo.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Fields

  Name Description
Public fieldStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 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.
Public fieldStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 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.

Top

Remarks

In Silverlight, you cannot derive from this type.

ConstructorInfo is used to discover the attributes of a constructor and 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.

NoteNote:

ConstructorInfo inherits several members from MethodBase, such as IsGenericMethod, that can be used to examine generic methods. Constructors cannot be generic, so these members return false or nulla null reference (Nothing in Visual Basic).

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 If you call a static constructor using reflection, the application throws ArgumentException instead of MethodAccessException.

Examples

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

Public Class Example
    Public Sub New(ByVal i As Integer)
    End Sub

    Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

         Dim myType As Type = GetType(Example)
         Dim parameters() As Type = { GetType(Int32) }

         ' Get the public instance constructor that takes an integer parameter.
         Dim ctor As ConstructorInfo = myType.GetConstructor( _
             BindingFlags.Instance Or BindingFlags.Public, _
             Type.DefaultBinder, _
             parameters, _
             Nothing)

         If ctor Is Nothing Then
             outputBlock.Text &= _
                 "There is no public instance constructor of MyClass " & _
                 "that takes an integer as a parameter." & vbCrLf
         Else
             outputBlock.Text &= _
                 "The public constructor of MyClass that takes an integer " & _
                 "as a parameter is: " & vbCrLf
             outputBlock.Text &= ctor.ToString() & vbCrLf
         End If

    End Sub 
End Class 

' This example produces the following output:
'
'The public constructor of MyClass that takes an integer as a parameter is:
'Void .ctor(Int32)
using System;
using System.Reflection;
using System.Security;

public class Example
{
    public Example(int i) { }

    public static void Demo(System.Windows.Controls.TextBlock outputBlock)
    {
        Type myType = typeof(Example);
        Type[] parameters = { typeof(int) };

        // Get the constructor that takes an integer as a parameter.
        ConstructorInfo ctor = myType.GetConstructor(
            BindingFlags.Instance | BindingFlags.Public, 
            Type.DefaultBinder,
            parameters,
            null);

        if (ctor == null)
        {
            outputBlock.Text += 
                "There is no public constructor of MyClass that takes an integer as a parameter.\n";
        }
        else
        {
            outputBlock.Text += 
                "The public constructor of MyClass that takes an integer as a parameter is:\n"; 
            outputBlock.Text += ctor.ToString() + "\n";
        }
    }
}

/* This example produces the following output:

The public constructor of MyClass that takes an integer as a parameter is:
Void .ctor(Int32)
 */

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference