ReflectorInvokeMethod(Object, String, Type, Object) Method

Invokes an instance method of an object represented by the specified methodName.

Definition

Namespace: KGySoft.Reflection
Assembly: KGySoft.CoreLibraries (in KGySoft.CoreLibraries.dll) Version: 10.0.0
C#
public static Object? InvokeMethod(
	Object instance,
	string methodName,
	Type[]? genericParameters,
	params Object?[]? parameters
)

Parameters

instance  Object
An instance whose method is about to be invoked.
methodName  String
The name of the method to be invoked.
genericParameters  Type
Type parameters if methodName refers to a generic method definition. Otherwise, this parameter is ignored.
parameters  Object
The parameters to be used for invoking the method.

Return Value

Object
The return value of the method.

Remarks

methodName can refer public and non-public methods. To avoid ambiguity this method gets all of the methods of the same name and chooses the first one for which the provided genericParameters and parameters match.

If you already have a MethodInfo instance use the InvokeMethod(Object, MethodInfo, Type, ReflectionWays, Object) method for better performance.

If you are not sure whether a method with the specified methodName exists, then you can use the TryInvokeMethod methods instead.

For invoking the method this method uses the DynamicDelegate reflection way.

  Tip

To preserve the changes of a mutable value type embed it into a variable of Object type and pass it to the instance parameter of this method.

See Also