In Java you can invoke a private method like so:
Method m = obj.getClass().getDeclaredMethod("foo", null);
m.setAccessible(true);
m.invoke("bar");
What is the equivalence in C#? I need to call a private method in the Unit Test.
In Java you can invoke a private method like so:
Method m = obj.getClass().getDeclaredMethod("foo", null);
m.setAccessible(true);
m.invoke("bar");
What is the equivalence in C#? I need to call a private method in the Unit Test.
If you have access to the MSTest Framework, you can create a PrivateObject instance, which is a wrapper that can be used to access private methods and fields of an object instance.
You can use the PrivateObject.Invoke() methods to call private methods from the object in question.
As a side note, it is sometimes considered bad practice to have to access private methods in order to do unit tests. I know it's not always possible, but you should also consider re-evaluating your code architecture to make it more UnitTest friendly as to not require this kind of workaround.