Conditional Reference
Asked Answered
G

1

4

I have an application that I was writing that communicates with a third-party application via a Component Object Model library. I must reference this COM library within the Visual Studio project itself in order for the application I am writing to work. There is also a .NET wrapper library that I must reference in the Visual Studio project in order to communicate with the COM library.

Is there a way to to create a conditional initialization of a class, in order to use a method within a .NET class within the .NET wrapper library, that will work in a later version of the third-party COM library itself.

The problem I ran into was that I was trying to reference a feature of the COM library that only existed in a later version. The version of the wrapper itself was identical because it was backwards compatible. When I attempted to access this new feature the program I was writing would silently close when I started it when the previous version of the third-party application was installed.

Is there a way I could have avoided this behavior without changing how the way the application itself was built?

Glaab answered 17/10, 2011 at 18:39 Comment(0)
O
14

Not sure whether I understand term "reference a class".

You can do a conditional referencing of an entire Assembly (DLL)

<Reference 
        Include="LegacyServices.dll" 
        Condition="$(AppVersion == '2.0')" />

or conditionally include a source file into a project

<Compile 
       Include="LegacyServices.cs" 
       Condition="$(AppVersion == '2.0')" />

Both using MSBuild Condition in csproj file.

Otis answered 17/10, 2011 at 18:46 Comment(3)
The thing you posted doesn't work. It says condition evaluated to '' instead of bool. Even msdn page you are refering to says it has to be $(AppVersion) == '2.0'. Also such references confuse visual studio (references appears even when condition is false), meanwhile that answer works fine with visual studio.Santana
Could you try Condition=" '$(AppVersion)' == '2.0'"Otis
That would work too, but I don't know how visual studio will treat that and I don't care because <Choose><When> worked fine and it's much better for multiple conditional references I have.Santana

© 2022 - 2024 — McMap. All rights reserved.