Skip to content

Instantly share code, notes, and snippets.

@n3dst4
Created March 21, 2011 15:10
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 n3dst4/879587 to your computer and use it in GitHub Desktop.
Save n3dst4/879587 to your computer and use it in GitHub Desktop.
How to use reflection to bypass access modifiers and execute a private method of an ancestor.
using System.Reflection;
private object ExecutePrivateMethod(string name, params object[] args)
{
MethodInfo method =
GetType().BaseType.GetMethod(name, BindingFlags.NonPublic | BindingFlags.Instance);
if (method == null)
throw new NotImplementedException("Cannot find method " + name);
return method.Invoke(this, args);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment