Browse documentation

Class MethodInvoker

Namespace
HarmonyLib
Assembly
0Harmony.dll

A helper class to invoke method with delegates

public static class MethodInvoker
Inheritance
object
MethodInvoker

Methods

GetHandler(MethodInfo, bool)

Creates a fast invocation handler from a method

public static FastInvokeHandler GetHandler(MethodInfo methodInfo, bool directBoxValueAccess = false)

Parameters

methodInfo MethodInfo

The method to invoke

directBoxValueAccess bool

Controls if boxed value object is accessed/updated directly

Returns

FastInvokeHandler

The FastInvokeHandler

Remarks

The directBoxValueAccess option controls how value types passed by reference (e.g. ref int, out my_struct) are handled in the arguments array passed to the fast invocation handler. Since the arguments array is an object array, any value types contained within it are actually references to a boxed value object. Like any other object, there can be other references to such boxed value objects, other than the reference within the arguments array.

For example,

var val = 5;
var box = (object)val;
var arr = new object[] { box };
handler(arr); // for a method with parameter signature: ref/out/in int
            <p>
        If <code>directBoxValueAccess</code> is <code>true</code>, the boxed value object is accessed (and potentially updated) directly when the handler is called,
        such that all references to the boxed object reflect the potentially updated value.
        In the above example, if the method associated with the handler updates the passed (boxed) value to 10, both <code>box</code> and <code>arr[0]</code>
        now reflect the value 10. Note that the original <code>val</code> is not updated, since boxing always copies the value into the new boxed value object.
        </p>
            <p>
        If <code>directBoxValueAccess</code> is <code>false</code> (default), the boxed value object in the arguments array is replaced with a "reboxed" value object,
        such that potential updates to the value are reflected only in the arguments array.
        In the above example, if the method associated with the handler updates the passed (boxed) value to 10, only <code>arr[0]</code> now reflects the value 10.
        </p>
Harmony 3 preview

For the stable release, read the 2.x documentation.