MethodInfoExtensions

Extension methods for System.Reflection.MethodInfo that simplify delegate creation, method invocation, and parameter signature matching. Used extensively by the framework's reflection-based systems such as the Input System callback binding.

Definition

Namespace: Paragon Assembly: Paragon.dll

public static class MethodInfoExtensions

Quick Lookup

Goal
Method

Create a delegate from a method

methodInfo.CreateDelegate(target)

Invoke on a target (typed return)

methodInfo.Invoke<T>(target, args)

Invoke a static method

methodInfo.InvokeStatic(args)

Invoke a static method (typed return)

methodInfo.InvokeStatic<T>(args)

Check if method accepts given values

methodInfo.TakesParameters(objects)

Check if method matches param types

methodInfo.HasParameters(types)

Methods

CreateDelegate

Creates a delegate from a MethodInfo, automatically inferring the delegate type from the method's parameter and return types via Expression.GetDelegateType().

public static Delegate CreateDelegate(this MethodInfo methodInfo, object target)
Parameter
Type
Description

methodInfo

MethodInfo

The method to create a delegate for

target

object

The instance to bind to (ignored for static methods)

Returns: A Delegate matching the method's signature.

circle-info

Unlike the built-in MethodInfo.CreateDelegate(Type), this extension auto-infers the delegate type. It constructs the type array as [param1Type, param2Type, ..., returnType] and passes it to Expression.GetDelegateType().

circle-exclamation

Invoke

Invokes the method on a target object. Convenience wrapper around the built-in MethodInfo.Invoke() using params.

Parameter
Type
Description

methodInfo

MethodInfo

The method to invoke

target

object

The instance to invoke on

parameters

object[]

Method arguments

Returns: The method's return value (boxed).

Invoke<TReturnValue>

Invokes the method on a target and casts the result to TReturnValue.

Returns: The method's return value cast to TReturnValue.

InvokeStatic

Invokes a static method (passes null as target).

Returns: The method's return value (boxed).

InvokeStatic<TReturnValue>

Invokes a static method and casts the result to TReturnValue.

Returns: The method's return value cast to TReturnValue.

TakesParameters

Checks whether the method accepts the given parameter values by extracting their runtime types and delegating to HasParameters().

Parameter
Type
Description

methodInfo

MethodInfo

The method to check

parameters

object[]

The parameter values to test against

Returns: true if the method's parameter signature is compatible with the given values.

HasParameters

Checks whether the method's parameter types match the given type list. Supports both exact type matching and assignability (including ref/out parameter types).

Parameter
Type
Description

methodInfo

MethodInfo

The method to check

types

IList<Type>

Expected parameter types

exactType

bool

If true, types must match exactly. If false (default), IsOfType() assignability check is used.

Returns: true if parameter count and types match.

circle-info

When exactType is false, both the direct type and its ByRef variant are checked. This means ref int and out int parameters match against typeof(int).

Common Pitfalls

circle-exclamation
circle-exclamation

See Also

Last updated