I’m trying to unit test some code which utilises a library which has a dependency hidden internally via the ‘internal’ class modifier.
During the setup of the tests I would like to be able to create an instance of the internal class. Normally I would try something like:
var type = typeof (FooBar);
var parameterlessCtor = (from c in type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)
where c.GetParameters().Length == 0 select c).FirstOrDefault();
object instance;
if(parameterlessCtor != null)
{
instance = parameterlessCtor.Invoke(null);
}
The obvious problem with this is ‘typeof(FooBar)’ – I can’t do typeof on an internal type.
Any ideas if I can get round this anyway?
Oh and I can’t rebuild the assembly with internally visible attribute…
No but you can retrieve the type from its full name using GetType