Skip to content

Instantly share code, notes, and snippets.

@skalinets
Created December 17, 2019 12:43
Show Gist options
  • Save skalinets/1c4e5dbb4e86bd72bf491675901de5ad to your computer and use it in GitHub Desktop.
Save skalinets/1c4e5dbb4e86bd72bf491675901de5ad to your computer and use it in GitHub Desktop.
Poor man's PrivateObject implementation for .NET Standard
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