The post Reflection in C#: 4 Code Simple But Powerful Code Examples appeared first on Dev Leader.
Reflection in C# is a powerful feature that allo...
For further actions, you may consider blocking this person and/or reporting abuse
Great !!!
So we can use reflection to test a private method for example? Indeed we often don't need to test private method but just the public caller.
Is it a bad approach?
In my opinion - Yes, this is a very very bad approach. 99% of the time this is absolutely not what you want to be doing.
But I am very glad that you asked this question. If you find that you need to use reflection to test a private method, I would highly suggest you reconsider how your class has been designed. If you truly need unit-test style coverage on a private method and the only way to access it is via other methods, it might be an opportunity to extract this via refactoring and put it onto another dedicated dependency.
In my professional experience, the only times I ever need to use reflection for testing private things:
In either of these cases, or in any situation where you're circumventing the access modifiers: You are going against how something was designed to be used. You should expect that this type of code will be extremely brittle due to changes.
I hope that helps! But please let me know if you have any questions!