Skip to content

Methods

The ByteCobra.Reflection.MethodExtensions class provides extension methods for invoking methods via reflection, with optional thread safety. This allows you to dynamically invoke instance and static methods, including generic methods, on objects and types. The thread-safe versions of the methods ensure that the method invocations are protected from concurrent access, avoiding potential race conditions.

InvokeMethod<T>

Invokes an instance method on the specified object.

Parameters

  • classInstance: The object instance on which to invoke the method.
  • name: The name of the method to invoke.
  • flags: Optional. The binding flags used in the method lookup. Default is BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic.
  • parameters: The parameters to pass to the method.

Returns: The result of the method if found and invoked successfully; otherwise, the default value of the return type T.

Example

var testInstance = new TestClass();
var result = testInstance.InvokeMethod<int>("Add", BindingFlags.Public, 3, 5);
// result will be 8

InvokeMethodThreadSafe<T>

Invokes an instance method on the specified object with thread safety.

Parameters

  • classInstance: The object instance on which to invoke the method.
  • name: The name of the method to invoke.
  • timeout: The maximum time to wait for the method access lock.
  • flags: Optional. The binding flags used in the method lookup. Default is BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic.
  • parameters: The parameters to pass to the method.

Returns: The result of the method if found and invoked successfully; otherwise, the default value of the return type T.

Example

var testInstance = new TestClass();
var result = testInstance.InvokeMethodThreadSafe<int>("Add", TimeSpan.FromSeconds(1), BindingFlags.Public, 3, 5);
// result will be 8

InvokeStaticMethod<T>

Invokes a static method on the specified type.

Parameters

  • type: The type containing the static method.
  • name: The name of the static method to invoke.
  • flags: Optional. The binding flags used in the method lookup. Default is BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic.
  • parameters: The parameters to pass to the method.

Returns: The result of the method if found and invoked successfully; otherwise, the default value of the return type T.

Example

var result = typeof(Math).InvokeStaticMethod<double>("Sqrt", BindingFlags.Public, 16);
// result will be 4.0

InvokeStaticMethodThreadSafe<T>

Invokes a static method on the specified type with thread safety.

Parameters

  • type: The type containing the static method.
  • name: The name of the static method to invoke.
  • flags: Optional. The binding flags used in the method lookup. Default is BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic.
  • parameters: The parameters to pass to the method.

Returns: The result of the method if found and invoked successfully; otherwise, the default value of the return type T.

Example

var result = typeof(MyStaticClass).InvokeStaticMethodThreadSafe<string>("GetMessage", TimeSpan.FromSeconds(1), BindingFlags.Public);
// result will be "Hello, World!"

InvokeGenericMethod<T>

Invokes a generic instance method on the specified object.

Parameters

  • classInstance: The object instance on which to invoke the method.
  • name: The name of the generic method to invoke.
  • genericTypes: The array of generic type arguments for the method.
  • flags: Optional. The binding flags used in the method lookup. Default is BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic.
  • parameters: The parameters to pass to the method.

Returns: The result of the method if found and invoked successfully; otherwise, the default value of the return type T.

Example

var myList = new List<int> { 1, 2, 3 };
var count = myList.InvokeGenericMethod<int>("Count", new Type[] { typeof(int) });
// count will be 3

InvokeGenericMethodThreadSafe<T>

Invokes a generic instance method on the specified object with thread safety.

Parameters

  • classInstance: The object instance on which to invoke the method.
  • name: The name of the generic method to invoke.
  • genericTypes: The array of generic type arguments for the method.
  • timeout: The maximum time to wait for the method access lock.
  • flags: Optional. The binding flags used in the method lookup. Default is BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic.
  • parameters: The parameters to pass to the method.

Returns: The result of the method if found and invoked successfully; otherwise, the default value of the return type T.

Example

var myList = new List<int> { 1, 2, 3 };
var count = myList.InvokeGenericMethodThreadSafe<int>("Count", new Type[] { typeof(int) }, TimeSpan.FromSeconds(1));
// count will be 3

InvokeGenericStaticMethod<T>

Invokes a generic static method on the specified type.

Parameters

  • type: The type containing the static method.
  • name: The name of the generic static method to invoke.
  • genericTypes: The array of generic type arguments for the method.
  • flags: Optional. The binding flags used in the method lookup. Default is BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic.
  • parameters: The parameters to pass to the method.

Returns: The result of the method if found and invoked successfully; otherwise, the default value of the return type T.

Example

var result = typeof(MyGenericClass).InvokeGenericStaticMethod<string>("GetMessage", new Type[] { typeof(int) });
// result will be "Hello, 42!"

InvokeGenericStaticMethodThreadSafe<T>

Invokes a generic static method on the specified type with thread safety.

Parameters

  • type: The type containing the static method.
  • name: The name of the generic static method to invoke.
  • genericTypes: The array of generic type arguments for the method.
  • timeout: The maximum time to wait for the method access lock.
  • flags: Optional. The binding flags used in the method lookup. Default is BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic.
  • parameters: The parameters to pass to the method.

Returns: The result of the method if found and invoked successfully; otherwise, the default value of the return type T.

Example

var result = typeof(MyGenericClass).InvokeGenericStaticMethodThreadSafe<string>("GetMessage", new Type[] { typeof(int) }, TimeSpan.FromSeconds(1));
// result will be "Hello, 42!"