Skip to content

Instantly share code, notes, and snippets.

@asarnaout
Last active November 29, 2018 07:05
Show Gist options
  • Save asarnaout/65cf45cc014099aef3383fefd0a17e5d to your computer and use it in GitHub Desktop.
Save asarnaout/65cf45cc014099aef3383fefd0a17e5d to your computer and use it in GitHub Desktop.
Invoking Private Methods During Runtime
/// <summary>
/// A simple library to invoke private methods during runtime. Could be used while unit testing private functions.
/// </summary>
public class MethodInvoker
{
private static MethodInfo GetMethod<T>(string methodName) =>
typeof(T).GetMethod(methodName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance);
/// <summary>
/// Invokes a private void method.
/// </summary>
/// <typeparam name="TType">The type which holds the private method</typeparam>
/// <param name="methodName">The method name</param>
/// <param name="instance">An instance of TType on which this method will be called, should be null in case the method is static</param>
/// <param name="parameters">The arguments to be passed to the method</param>
public static void InvokeAction<TType>(string methodName, TType instance = default(TType),
params object[] parameters)
{
GetMethod<TType>(methodName).Invoke(instance, BindingFlags.Static | BindingFlags.Instance,
default(Binder), parameters, CultureInfo.InvariantCulture);
}
/// <summary>
/// Invokes a private method which returns a value
/// </summary>
/// <typeparam name="TType">The type which holds the private method</typeparam>
/// <param name="methodName">The method name</param>
/// <param name="instance">An instance of TType on which this method will be called, should be null in case the method is static</param>
/// <param name="parameters">The arguments to be passed to the method</param>
/// <returns>The expected value returned from the private method</returns>
public static object InvokeFunc<TType>(string methodName, TType instance = default(TType),
params object[] parameters)
{
return GetMethod<TType>(methodName).Invoke(instance, BindingFlags.Static | BindingFlags.Instance,
default(Binder), parameters, CultureInfo.InvariantCulture);
}
}
public class Person
{
public string Name { get; set; }
public Person(string name)
{
Name = name;
}
private static void SomePrivateStaticActionNoParams()
{
Console.WriteLine("Some Private Static Action No Params");
}
private void SomePrivateInstanceActionNoParams()
{
Console.WriteLine($"Some Private Instance Action No Params Invoked on: {this.Name}");
}
private static void SomePrivateStaticActionWithParams(int x, int y)
{
Console.WriteLine($"Some Private Static Action With Params: {x}, {y}");
}
private void SomePrivateInstanceActionWithParams(int x, int y)
{
Console.WriteLine($"Some Private Instance Action With Params Invoked on: {this.Name}, params: {x}, {y}");
}
private static int SomePrivateStaticFuncNoParams()
{
Console.WriteLine("Some Private Static Func No Params");
return 1;
}
private bool SomePrivateInstanceFuncNoParams()
{
Console.WriteLine($"Some Private Instance Func No Params Invoked on: {this.Name}");
return true;
}
private static string SomePrivateStaticFuncWithParams(int x, int y)
{
Console.WriteLine($"Some Private Static Func With Params: {x}, {y}");
return "Success";
}
private char SomePrivateInstanceFuncWithParams(int x, int y)
{
Console.WriteLine($"Some Private Instance Func With Params Invoked on: {this.Name}, params: {x}, {y}");
return 'c';
}
}
public class Demo
{
public static void Main(string[] args)
{
Person p1 = new Person("Ahmed");
Person p2 = new Person("Ali");
MethodInvoker.InvokeAction<Person>("SomePrivateStaticActionNoParams");
MethodInvoker.InvokeAction<Person>("SomePrivateInstanceActionNoParams", p1);
MethodInvoker.InvokeAction<Person>("SomePrivateStaticActionWithParams", null, 1, 2);
MethodInvoker.InvokeAction<Person>("SomePrivateInstanceActionWithParams", p2, 3, 4);
var result1 = (int)MethodInvoker.InvokeFunc<Person>("SomePrivateStaticFuncNoParams");
var result2 = (bool)MethodInvoker.InvokeFunc<Person>("SomePrivateInstanceFuncNoParams", p1);
var result3 = (string)MethodInvoker.InvokeFunc<Person>("SomePrivateStaticFuncWithParams", null, 1, 2);
var result4 = (char)MethodInvoker.InvokeFunc<Person>("SomePrivateInstanceFuncWithParams", p2, 3, 4);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment