Conditional project or library reference in Visual Studio

Source: Internet
Author: User

Conditional project or library reference in Visual Studio

In case you were wondering why do you haven ' t heard from me in a while, I ' ve been busy, which isn ' t really of much importance Unless know me on a personal level. What's relevant is this I recently graduated with a master on Game and Media Technology and am now in the process of Maki NG the project which I has been working on for my thesis open source. I ' m very anxious to announce it, so you'll read a lot more about it in the the near future.

The project uses the other open source libraries, located in separate repositories. Adding these projects to the solution and  referencing them as project references  has the Advant Age of easier debugging and facilitates making changes to them. However, I don't want to force anyone interested in making changes to the main project to have to download the other re Positories as well. Therefore I opted to use  DLL references .  this have one major downside. Whenever I do make changes to one of the dependent libraries, I need to manually copy the newly compiled DLLs to the main Project. wouldn ' t It is easy to use  separate solution Files , one with project references, a nd one with DLL references?

The first problem you ' ll encounter are project references aren ' t stored in the . sln file and the . csproj file, which makes sense really. Since the . csproj file is a shared by both solutions, we'll have a conditionally reference either our DLLs or Our projects, depending on which solution the project are opened in. This is possible usingMSBuild, but you'll need to create a new project configuration. Setting the configuration of the project differently in one solution from the other allows you to differentiate between th E. them. The following is part of a . csproj file which conditionally loads a reference by checking whether the project con Figuration is set to 'Debug with Project References '.

<Choose>  < whenCondition= "' $ (Configuration) ' = = ' Debug with Project References '">    <ItemGroup>      <projectreferenceInclude=".. \someproject\someproject.csproj ">        <Project>{6CA7AB2C-2D8D-422A-9FD4-2992BE62720A}</Project>        <Name>Someproject</Name>      </projectreference>    </ItemGroup>  </ when>  <Otherwise>    <ItemGroup>      <ReferenceInclude= "Someproject">        <HintPath>.. \libraries\someproject.dll</HintPath>      </Reference>    </ItemGroup>  </Otherwise></Choose>

You could very well stop here, but I wanted to resolve another issue as well. Unless follow a really strict folder structure, and force everyone else who wants to open your solution to do the same The path used to reference the project can vary between people. Remember we is using separate repositories here, and the referenced projects aren ' t included in the repository O f The main solution, so relative paths don ' t necessarily work. It is possible to specify the paths in a external ' configuration ' file, and importing it into the . csprojfile. Additionally, as a fallback when the project can ' t being found at the given path, it's also useful to load the DLL instead. This is the can choose to load one or more and not necessarily all references as a project reference.

The configuration file (ProjectReferences.txt):

<Projectxmlns= "http://schemas.microsoft.com/developer/msbuild/2003">  <PropertyGroupCondition= "$ (Configuration) = = ' Debug with Project References '">    <Someproject>.. \someproject</Someproject>  </PropertyGroup></Project>

Segment from the . csproj File:

<ImportProject=".. \projectreferences.txt " /><Choose>  < whenCondition= "Exists ($ (someproject))">    <ItemGroup>      <projectreferenceInclude= "$ (someproject) \someproject.csproj">        <Project>{6CA7AB2C-2D8D-422A-9FD4-2992BE62720A}</Project>        <Name>Someproject</Name>      </projectreference>    </ItemGroup>  </ when>  <Otherwise>    <ItemGroup>      <ReferenceInclude= "Someproject">        <HintPath>.. \libraries\someproject.dll</HintPath>      </Reference>    </ItemGroup>  </Otherwise></Choose>

Notice the project configuration check now occurs in the configuration file. The ' $ (someproject) '  property is only set when using the ' Debug with Project References ' & Nbsp;configuration, thus in all other scenarios the DLL would be loaded instead since the ' Exists () ' check would FA Il.

One last issue remains. We still need to manually copy the latest compiled DLLs to the 'Libraries ' folder when changes were made For the solution which uses them to work as expected. We can exploit the fact that we are now having the relevant project paths available in the configuration file. Using a Post build event you can call a script which parses the XML data, and copies the DLLs to the correct location. This script should is called conditionally, only for the solution which includes the project references, and ideally also Only for a Releasebuild. I used a small Ruby script which offers a lot more flexibility than a Batch script.

The Post build event:

if " $ (solutionname) " " somesolution with Project References " if " $ (configurationname) " " Release " Ruby $ (SolutionDir) updatelibrarydlls.rb $ (SolutionDir)

Conditional project or library reference in Visual Studio

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.