Skip to content

Instantly share code, notes, and snippets.

@nikomoravec
Created February 6, 2018 16:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikomoravec/b56709ebc8a32965c2246448e4e925bd to your computer and use it in GitHub Desktop.
Save nikomoravec/b56709ebc8a32965c2246448e4e925bd to your computer and use it in GitHub Desktop.
Call private methods in C#
using System;
using System.Reflection;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
A a = new A();
System.Reflection.MethodInfo method = a.GetType().GetMethod("Foo", BindingFlags.NonPublic | BindingFlags.Instance);
method.Invoke(a, new object[] { });
}
}
public class A
{
void Foo ()
{
Console.WriteLine("Hello from reflection");
Console.ReadKey();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment