I’ve got the following problem. Suppose, that there is a class, which contains a private class, which have to be tested. To cut off unnecessary comments and answers, yes it has to stay private and yes, it have to be tested.
Say, it looks like this:
public class PublicClass
{
private class InternalClass
{
void Method() { }
}
}
For ease and convenience of testing, I would like to create the following class:
public class InternalClassAccess
{
private object instance;
public InternalClassAccess()
{
// Use reflections to instantiate InternalClass
// and store it in instance field
}
public void Method()
{
// Use reflections to call Method() on
// stored instance
}
}
I can write such class manually, but I wonder, if there is some kind of automatic way of doing so? I have Professional version of VS 2012.
I don’t know of anything built into .NET for this – but you can write your class once for all such classes using
dynamic. You’d have a structure something like this:(Likewise
TryInvokeMemberfor methods, etc.)Then you can use it as:
(I agree with the comment that it’s odd to have to do this, mind you. You really can’t make it internal?)