I want to be able to change which namespace I use based on a user selection. I have two nearly identical namespaces from the same API. The methods within the namespaces have the same names. If I could dynamically change the using directive, I wouldn't have to go back through the code and write the namespace in front of all the methods. I want to write something like this:
if (radioIFC2X3.IsChecked == true)
{
using Xbim.Ifc2x3.Kernel;
using Xbim.Ifc2x3.Interfaces;
}
else
{
using Xbim.Ifc4.Kernel;
using Xbim.Ifc4.Interfaces;
}
I can't get this to work, so I thought you would maybe have some ideas.
CodePudding user response:
No. "using namespaces" is a compile time thing. There is #pragma if that can include code sections at compile time, but it would not help if you want to do something at runtime.
If you need to switch between implementations at runtime you need to use runtime constructs. Typically you should have something like
IKernel myKernel;
IInterface myInterfaces;
if (radioIFC2X3.IsChecked == true)
{
myKernel= new Xbim.Ifc2x3.Kernel();
myInterfaces= new Xbim.Ifc2x3.Interfaces();
}
else
{
myKernel= new Xbim.Ifc4.Kernel();
myInterfaces= new Xbim.Ifc4.Interfaces();
}
This would obviously require that you have actual objects you can switch out.
So if you have two nearly identical APIs a common solution is to provide thin wrappers around each that implement a common interface so they can actually be used interchangeably.