I want to test private methods.
I tried the fifth solution. ie : using reflection
using System;
using System.Reflection;
namespace tobeDEleted
{
static class classA {
private static void method1() { Console.WriteLine(1); }
}
class classB
{
private void method2() { Console.WriteLine(2); }
private static void method3() { Console.WriteLine(3); }
}
class Program
{
static void Main(string[] args)
{
var objb = new classB();
MethodInfo methodInfo1 = typeof(classA).GetMethod ("method1", BindingFlags.NonPublic | BindingFlags.Instance);
MethodInfo methodInfo2 = typeof(classB).GetMethod("method2", BindingFlags.NonPublic | BindingFlags.Instance);
MethodInfo methodInfo3 = typeof(classB).GetMethod("method3", BindingFlags.NonPublic | BindingFlags.Instance);
object[] parameters = { };
methodInfo2.Invoke(obj, parameters);
methodInfo3.Invoke(obj, parameters);
}
}
}
as expected methodInfo3 throw a exception:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Is there a similar method as GetMethod for static methods.
I don't know too, how to test an static class.