Search code examples
c#.netwrapperusing

C# proper (and easy) implementation for changing `using` statements?


I've recently stubled across a seemingly very simple problem I can't wrap my head around:

I am currently building a simple tool to access some USB Oscilloscopes. Language Version is C#7.3

Following the rough Idea of the manufacturer, I am using a class implementend in their C# dll wrapper (c-sharp-wrapper ). So the basic structure of my access classs is like this:

using PS3000AImports;


namespace PicoScope
{
    internal class Scope
    {
        private short hPicoScope; //Handle initialized somewhere

        // little example function
        public Boolean disconnect()
        {
            uint status;
            status = Imports.CloseUnit(hPicoScope); //**Thats the demo call to the wrapper**//
            if (status != StatusCodes.PICO_OK) { return false; };
            return true;
        }

       // and so on and on..
   

Which works fine. Now I want to use another scope series, which has its own dll, slightly different calls (god knows why) and therefore an own C# dll wrapper class PS5000AImports (also in the github repo if you're interested).

By altering my using directive to PS5000AImports I can sucessfully use them (simplifying things slightly, since some calls are different I had to write a wrapper for the wrapper to give the functions all the same names).

Now to my Issue: What if I want to use both PS3000 devices and PS5000 devices in my code? I can't instanciate the scope class with different using statements.. Or is there some trick to do so?!

I first thougt about interfaces, but as the wrappers are quite large and contain enums, delegates and other stuff it is not straightforward to implement an interface (not to speak of static members which can't be used in C# 7.3 interfaces)..

Well, now what comes to my mind is a complete copy of the file just with the other using statement and an altered class name to use the other scope. But this is for sure a very dumb idea. There has to be a smart way to do this! Its after all just one using line of code that hast to be altered..

Tried:

  • Abstract class (bad idea, lots of extra code)
  • Interface (different problems with static functions, delegates, different enum implementations in the wrappers..)

Solution

  • Do you need to use using directives at all? If you fully qualify your usage of the PS3000AImports namespace whenever you use it, you should be able to rewrite your existing code as follows:

    namespace PicoScope
    {
        internal class Scope
        {
            private short hPicoScope; //Handle initialized somewhere
    
            // little example function
            public Boolean disconnect()
            {
                uint status;
                status = PS3000AImports.Imports.CloseUnit(hPicoScope); //**Thats the demo call to the wrapper**//
                if (status != PS3000AImports.StatusCodes.PICO_OK) { return false; };
                return true;
            }
    
           // and so on and on..
    

    Then, you can create a separate disconnect() method that uses equivalents from the PS5000AImports namespace.