Consider following code –
namespace N1
{
class A
{
//some implementation
}
class B
{
//some implemntation
}
}
namespace N2
{
class A
{
//slightly different implementation
}
class B
{
//slightly different implementation
}
}
Obviously we can’t use both assemblies together –
And the resolution is, to use fully qualified class names. When refactoring 1000s of lines of code which uses N1 namespace classes. Decision is made once at the startup of process, whethere N1 has to be used or N2.
Something a bit more advanced than conditional compilation feature in C++
#ifdef debug
#include <N1.h>
#elif
#include <N2.h>
#endif
Sorry if it sounds impossible, but out of my curiosity I wonder is it possible to do –
if (...)
{
using N1;
}
else
{
using N2;
}
The
using-based approach you are describing will not work, however. “Using a namespace” is really just a compile time construct, so it has no effect on the runtime behavior. At runtime, the fully qualified types are used.One typical approach to handling this is to use form of IoC, via a library like MEF.
If you make your types in both namespaces derive from the same base classes, or implement the same interfaces, you can
[Export]the types from two separate assemblies. The decision of which assembly to compose can be made at runtime. This allows you to then[Import]types as needed, and you’ll get the appropriate types.