ConstructorInfo.Invoke Method

Definition

Invokes the constructor reflected by this instance.

Overloads

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.

Invoke(Object[])

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

public:
 virtual System::Object ^ Invoke(cli::array <System::Object ^> ^ parameters);
public:
 System::Object ^ Invoke(cli::array <System::Object ^> ^ parameters);
public virtual object Invoke (object[] parameters);
public object Invoke (object?[]? parameters);
public object Invoke (object[] parameters);
override this.Invoke : obj[] -> obj
Public Overridable Function Invoke (parameters As Object()) As Object
Public Function Invoke (parameters As Object()) As Object

Parameters

parameters
Object[]

An array of values that matches the number, order and type (under the constraints of the default binder) of the parameters for this constructor. If this constructor takes no parameters, then use either an array with zero elements or null, as in Object[] parameters = new Object[0]. Any object in this array that is not explicitly initialized with a value will contain the default value for that object type. For reference-type elements, this value is null. For value-type elements, this value is 0, 0.0, or false, depending on the specific element type.

Returns

An instance of the class associated with the constructor.

Exceptions

The class is abstract.

-or-

The constructor is a class initializer.

The constructor is private or protected, and the caller lacks MemberAccess.

Note: In .NET for Windows Store apps or the Portable Class Library, catch the base class exception, MemberAccessException, instead.

The parameters array does not contain values that match the types accepted by this constructor.

The invoked constructor throws an exception.

An incorrect number of parameters was passed.

Creation of TypedReference, ArgIterator, and RuntimeArgumentHandle types is not supported.

The caller does not have the necessary code access permission.

Remarks

The number, type, and order of elements in the parameters array should be identical to the number, type, and order of parameters for the constructor reflected by this instance. Before calling the constructor, Invoke ensures that the caller has access permission and verifies that the parameters are valid.

Access restrictions are ignored for fully trusted code. That is, private constructors, methods, fields, and properties can be accessed and invoked using reflection whenever the code is fully trusted.

Note

To create an instance of a value type that has no instance constructors, use the CreateInstance method.

This method is a convenience method for the following overloaded version, using default values. This method cannot be overridden.

Note

Starting with .NET Framework 2.0, this method can be used to access non-public members if the caller has been granted ReflectionPermission with the ReflectionPermissionFlag.RestrictedMemberAccess flag and if the grant set of the non-public members is restricted to the caller's grant set, or a subset thereof. (See Security Considerations for Reflection.) To use this functionality, your application should target .NET Framework 3.5 or later.

Applies to

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.

public:
 abstract System::Object ^ Invoke(System::Reflection::BindingFlags invokeAttr, System::Reflection::Binder ^ binder, cli::array <System::Object ^> ^ parameters, System::Globalization::CultureInfo ^ culture);
public abstract object Invoke (System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder? binder, object?[]? parameters, System.Globalization.CultureInfo? culture);
public abstract object Invoke (System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] parameters, System.Globalization.CultureInfo culture);
override this.Invoke : System.Reflection.BindingFlags * System.Reflection.Binder * obj[] * System.Globalization.CultureInfo -> obj
Public MustOverride Function Invoke (invokeAttr As BindingFlags, binder As Binder, parameters As Object(), culture As CultureInfo) As Object

Parameters

invokeAttr
BindingFlags

One of the BindingFlags values that specifies the type of binding.

binder
Binder

A Binder that defines a set of properties and enables the binding, coercion of argument types, and invocation of members using reflection. If binder is null, then Binder.DefaultBinding is used.

parameters
Object[]

An array of type Object used to match the number, order and type of the parameters for this constructor, under the constraints of binder. If this constructor does not require parameters, pass an array with zero elements, as in Object[] parameters = new Object[0]. Any object in this array that is not explicitly initialized with a value will contain the default value for that object type. For reference-type elements, this value is null. For value-type elements, this value is 0, 0.0, or false, depending on the specific element type.

culture
CultureInfo

A CultureInfo used to govern the coercion of types. If this is null, the CultureInfo for the current thread is used.

Returns

An instance of the class associated with the constructor.

Exceptions

The parameters array does not contain values that match the types accepted by this constructor, under the constraints of the binder.

The invoked constructor throws an exception.

An incorrect number of parameters was passed.

Creation of TypedReference, ArgIterator, and RuntimeArgumentHandle types is not supported.

The caller does not have the necessary code access permissions.

The class is abstract.

-or-

The constructor is a class initializer.

The constructor is private or protected, and the caller lacks MemberAccess.

Remarks

The number, type, and order of elements in the parameters array should be identical to the number, type, and order of parameters for the constructor reflected by this instance.

Before calling the constructor, Invoke ensures that the caller has access permission and that the parameters are of the correct number, order and type.

Access restrictions are ignored for fully trusted code. That is, private constructors, methods, fields, and properties can be accessed and invoked using reflection whenever the code is fully trusted.

Note

To create an instance of a value type that has no instance constructors, use the CreateInstance method.

Note

Starting with .NET Framework 2.0, this method can be used to access non-public members if the caller has been granted ReflectionPermission with the ReflectionPermissionFlag.RestrictedMemberAccess flag and if the grant set of the non-public members is restricted to the caller's grant set, or a subset thereof. (See Security Considerations for Reflection.) To use this functionality, your application should target .NET Framework 3.5 or later.

See also

Applies to