Created
December 17, 2019 12:43
-
-
Save skalinets/1c4e5dbb4e86bd72bf491675901de5ad to your computer and use it in GitHub Desktop.
Poor man's PrivateObject implementation for .NET Standard
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
public class PrivateObject | |
{ | |
private readonly object o; | |
public PrivateObject(object o) | |
{ | |
this.o = o; | |
} | |
public object Invoke(string methodName, params object[] args) | |
{ | |
var methodInfo = o.GetType().GetMethod(methodName, BindingFlags.NonPublic | BindingFlags.Instance); | |
if (methodInfo == null) | |
{ | |
throw new Exception($"Method'{methodName}' not found is class '{o.GetType()}'"); | |
} | |
return methodInfo.Invoke(o, args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment