-
Using reflection to unit-test private methods
Situation There is a class called “MyClass” which has a private method called “MyPrivateMethod” (which expects a single string parameter) that you want to unit-test. Problem You can’t access private methods from the test class by just calling them. Solution Use reflection to invoke the method. typeof(MyClass).GetMethod(“MyPrivateMethod”, BindingFlags.NonPublic | BindingFlags.Instance) .Invoke(null, new object[] { “MyStringParameter” }); Hint: Static methods require the BindingFlag…