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 MethodInfoExtensionsQuick Lookup
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)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.
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().
For static methods, the target parameter is ignored — the method checks methodInfo.IsStatic and omits it from the delegate creation call.
Invoke
Invokes the method on a target object. Convenience wrapper around the built-in MethodInfo.Invoke() using params.
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().
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).
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.
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
InvalidCastException on typed Invoke
Invoke<T>() and InvokeStatic<T>() perform a direct cast (TReturnValue)result. If the method returns a type that is not castable to TReturnValue, an InvalidCastException is thrown at runtime.
Null parameters in TakesParameters
TakesParameters() calls GetType() on each parameter value. Passing null in the array will cause a NullReferenceException.
See Also
TypeExtensions —
IsOfType()used byHasParameters()InputActionCallback — uses
CreateDelegate()andInvokeStatic<T>()InputActionMapBinding — uses
CreateDelegate()for callback wiring
Last updated