Search code examples
c#.netvisual-studionuget.net-assembly

Reference different assembly version per build?


I have a program that is primarily an addin to another program. I have my core logic, UI, etc. in a single project which can also be used independently of the parent program. Then I have a 'connector' piece that connects my program to the parent program and is essentially a communicator between the two. I also support multiple versions of the parent software.

Now here is the issue. I have my core program that uses System.Windows.Interactivity.dll. It seems the parent program also uses this, but earlier supported versions of the parent use the .NET 4.0 version of interactivity and later supported versions use .NET 4.5 version.

My core software works if I compile with the 4.0 version or the 4.5 version, but when it's running inside the parent, if I have the 4.5 version it breaks in earlier versions and if I have 4.0 installed it breaks in later versions...

Is there a way I can create two different builds that compile on the different dll files? I guess I'm going to need to have two different install locations based on which one I want?


Solution

  • This will do what you want:

    1) Add new configurations for each (where you normally pick between Debug and Release).

    enter image description here

    In my example I'll go with TheNet40one and TheNet45One.

    2) Edit your project's .csproj file and add the following (I'll just demo AnyCPU):

    <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'TheNet40One|AnyCPU' ">
        <PlatformTarget>AnyCPU</PlatformTarget>
        <DebugType>pdbonly</DebugType>
        <Optimize>true</Optimize>
        <OutputPath>bin\TheNet40One\</OutputPath>
        ...
    </PropertyGroup>
    

    3) Then add conditional references (obviously I am guessing at your .dll names):

    <ItemGroup>
        <Reference Include="System" />
        <Reference Include="System.Core" />
        ...
        <Reference Include="40.dll" Condition="'$(Configuration)'=='TheNet40One'" />
        <Reference Include="45.dll" Condition="'$(Configuration)'=='TheNet45One'" />
    </ItemGroup>
    

    I've used ... to replace other common settings and references - don't forget to replace them.

    Now you can switch between the two build configurations, knowing they will each link in the correct DLL file.

    enter image description here