0

I am working in .NET Framework and am using C#. I am referencing external dlls. One particular method in available only if the dlls version is 12 or up. Is there a way to condition code compilation based on the version of dlls.

I can get the version of the dlls, but if it is on version 11 or lower, how can I restrict a chuck of code from compiling.

Below is the code I'm currently using, when I refer dlls of version 11 or lower, the GetUnsortedFaces() shows error and it won't let me compile.

List<IVctFace> IVctEdge.AllFaces()
    {
        Face[] faces;
        Version version = System.Reflection.Assembly.GetAssembly(typeof(Edge)).GetName().Version;

        faces = this.Edge.GetFaces();

        if (version.Major > 11)
        {
            faces = this.Edge.GetUnsortedFaces();
        }
        List<IVctFace> vctFaces = new List<IVctFace>();
        foreach (Face face in faces)
        {
            vctFaces.Add(new VctFace(face, this.Parent.Parent));
        }
        return vctFaces;
    }

I do know about #define but they needs to be created outside the namespace.

1 Answers1

0

I have a suggestion: declare a dynamic variable to reference the Edge object (see https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/using-type-dynamic) You can then call the GetUnsortedFaces function inside a try..catch sequence or use reflection to test whether the GetUnsortedFaces function is implemented by the Edge object before calling that function.

Dan
  • 1,927
  • 2
  • 24
  • 35