Legend

Class
Struct
Enum
Interface
Delegate
Constructor
Method
Property
Event
Field

Extension method: object InvokeDirect(this MethodInfo, object, params object[])

Declaration

public static object InvokeDirect(
    this MethodInfo method,
    object instance,
    params object[] parameters
)

Summary

Invokes method method on object instance passing it the specified parameters. Unlike MethodBase.Invoke(object, object[]), which wraps any exceptions in the target method into a TargetInvocationException, this will invoke the target method in such a way that any exceptions will propagate just like they would if the method had been invoked "directly" rather than via reflection. See Remarks.

Parameters

this MethodInfomethod The method to invoke. Must be an instance method.
objectinstance The instance on which to invoke the method. Must not be null, as static methods are not supported.
object[]parameters Parameters to pass into the method.

Returns

What the target method returned, or null if its return type is void.

Remarks

There is a good reason why TargetInvocationException is used: when using this method, you cannot tell apart an exception that occurred in the invoker (e.g. you passed in a null for method or wrong number of parameter) from an exception that occurred in the target method. But it also means that the debugger will always stop at the Invoke call and not at the actual exception, because the inner exception is considered handled. One solution to this is to configure VS to stop on all exceptions, which has its own downsides. The other one is to use NUnitDirect.InvokeDirect(this MethodInfo, object, params object[]). In practice, distinguishing target exceptions from invoker exceptions is usually not very important, so the annoyance of having to debug exceptions differently becomes a bigger problem.