Skip to content

Instantly share code, notes, and snippets.

@NineMvp
Last active May 4, 2018 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NineMvp/838f21e81007e8e34ad518fefbe54607 to your computer and use it in GitHub Desktop.
Save NineMvp/838f21e81007e8e34ad518fefbe54607 to your computer and use it in GitHub Desktop.
Simple PrivateObject
// Test Class
public class Demo {
private string send (String msg) => $"{msg} : Sent.";
}
// Test Util
public class PrivateObject<T> where T : class, new () {
private T obj;
public PrivateObject () {
obj = new T ();
}
public TR callPrivate<TR> (string methodName, params object[] values) {
return (TR) typeof (T).InvokeMember (methodName,
BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Public |
BindingFlags.Instance, null, obj, values);
}
}
// Call Test
var result = new PrivateObject<Demo>().callPrivate<String>("send", "Hello");
Console.WriteLine($"{result}");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment