C# different method code by symbol

47 Views Asked by At

I am in a situation where a method code implementation depends on the chosen Visual Studio solution configuration.

Each configuration targets a different external DLL that provides a different API version to allow me to differentiate them. These DLLs are for Autodesk Revit API if that information could help.

As you may think, each API version, although very similar, can have subtle differences between them forcing me to have different code depending on which version I am targeting.

I have defined different symbols (REVIT_2014, REVIT_2015, etc.) for each configuration knowing that I can rely on good old #ifdef directives but first I wanted to explore if there is a more elegant solution.

I have read about ConditionalAttribute but I cannot use it in the way I wanted, i.e.:

class Test
{
    [Conditional("REVIT_2015")]
    bool TestMethod
    {
        //Revit 2015 specific code
    }

    [Conditional("REVIT_2016")]
    bool TestMethod
    {
        //Revit 2016 specific code
    }
}

As ConditionalAttribute does not allow to override methods, nor it allows methods to return values.

Before I start plaguing my code with ugly #ifdef directives, is there any other solution I can look into?

Thanks in advance.

0

There are 0 best solutions below