Export (0) Print
Expand All

MethodBase::Invoke Method (Object, array<Object>)

Invokes the method or constructor represented by the current instance, using the specified parameters.

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

[DebuggerStepThroughAttribute]
[DebuggerHiddenAttribute]
public:
virtual Object^ Invoke(
	Object^ obj, 
	array<Object^>^ parameters
) sealed

Parameters

obj
Type: System::Object

The object on which to invoke the method or constructor. If a method is static, this argument is ignored. If a constructor is static, this argument must be nullptr or an instance of the class that defines the constructor.

parameters
Type: array<System::Object>

An argument list for the invoked method or constructor. This is an array of objects with the same number, order, and type as the parameters of the method or constructor to be invoked. If there are no parameters, parameters should be nullptr.

If the method or constructor represented by this instance takes a ref parameter (ByRef in Visual Basic), no special attribute is required for that parameter in order to invoke the method or constructor using this function. 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 nullptr. For value-type elements, this value is 0, 0.0, or false, depending on the specific element type.

Return Value

Type: System::Object
An object containing the return value of the invoked method, or nullptr in the case of a constructor.
Caution noteCaution

Elements of the parameters array that represent parameters declared with the ref or out keyword may also be modified.

Implements

_MethodBase::Invoke(Object, array<Object>)

ExceptionCondition
TargetException

The obj parameter is nullptr and the method is not static.

-or-

The method is not declared or inherited by the class of obj.

-or-

A static constructor is invoked, and obj is neither nullptr nor an instance of the class that declared the constructor.

ArgumentException

The elements of the parametersarray do not match the signature of the method or constructor reflected by this instance.

TargetInvocationException

The invoked method or constructor throws an exception.

-or-

The current instance is a DynamicMethod that contains unverifiable code. See the "Verification" section in Remarks for DynamicMethod.

TargetParameterCountException

The parameters array does not have the correct number of arguments.

MethodAccessException
NoteNote

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

The caller does not have permission to execute the method or constructor that is represented by the current instance.

InvalidOperationException

The type that declares the method is an open generic type. That is, the Type::ContainsGenericParameters property returns true for the declaring type.

NotSupportedException

The current instance is a MethodBuilder.

This is a convenience method that calls the Invoke(Object, BindingFlags, Binder, array<Object>, CultureInfo) method overload, passing Default for invokeAttr and nullptr for binder and culture.

If the invoked method throws an exception, the Exception::GetBaseException method returns the exception.

To invoke a static method using its MethodInfo object, pass nullptr for obj.

NoteNote

If this method overload is used to invoke an instance constructor, the object supplied for obj is reinitialized; that is, all instance initializers are executed. The return value is nullptr. If a class constructor is invoked, the class is reinitialized; that is, all class initializers are executed. The return value is nullptr.

NoteNote

Starting with the .NET Framework 2.0 Service Pack 1, 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 the .NET Framework 3.5 or later.

If a parameter of the current method is a value type, and the corresponding argument in parameters is nullptr, the runtime passes a zero-initialized instance of the value type.

The following code example demonstrates dynamic method lookup using reflection. Note that you cannot use the MethodInfo object from the base class to invoke the overridden method in the derived class, because late binding cannot resolve overrides.

using namespace System;
using namespace System::Reflection;

public ref class MagicClass
{
private:
    int magicBaseValue;

public:
    MagicClass()
    {
        magicBaseValue = 9;
    }

    int ItsMagic(int preMagic)
    {
        return preMagic * magicBaseValue;
    }
};

public ref class TestMethodInfo
{
public:
    static void Main()
    {
        // Get the constructor and create an instance of MagicClass

        Type^ magicType = Type::GetType("MagicClass");
        ConstructorInfo^ magicConstructor = magicType->GetConstructor(Type::EmptyTypes);
        Object^ magicClassObject = magicConstructor->Invoke(gcnew array<Object^>(0));

        // Get the ItsMagic method and invoke with a parameter value of 100

        MethodInfo^ magicMethod = magicType->GetMethod("ItsMagic");
        Object^ magicValue = magicMethod->Invoke(magicClassObject, gcnew array<Object^>(1){100});

        Console::WriteLine("MethodInfo.Invoke() Example\n");
        Console::WriteLine("MagicClass.ItsMagic() returned: {0}", magicValue);
    }
};

int main()
{
    TestMethodInfo::Main();
}

// The example program gives the following output: 
// 
// MethodInfo.Invoke() Example 
// 
// MagicClass.ItsMagic() returned: 900

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft