Last active
May 4, 2018 06:04
-
-
Save NineMvp/838f21e81007e8e34ad518fefbe54607 to your computer and use it in GitHub Desktop.
Simple PrivateObject
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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