Conditional project or library reference in Visual Studio

In case you were wondering why you haven’t heard from me in a while, I’ve been busy, which isn’t really of much importance unless you know me on a personal level. What is relevant is that I recently graduated with a master in Game and Media Technology and am now in the process of making the project which I have 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 near future.

The project uses two of my other open source libraries, located in separate repositories. Adding these projects to the solution and referencing them as project references has the advantage of easier debugging and facilitates making changes to them. However, I do not want to force anyone interested in making changes to the main project to having to download the other repositories as well. Therefore I opted to use DLL references. This has 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 be easy to use two separate solution files, one with project references, and one with DLL references?

The first problem you’ll encounter is project references aren’t stored in the .sln file but in the .csproj file, which makes sense really. Since the .csproj file is shared by both solutions, we’ll have to conditionally reference either our DLLs or our projects, depending on which solution the project is opened in. This is possible usingMSBuild, but you will 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 the two of them. The following is part of a .csproj file which conditionally loads a reference by checking whether the project configuration is set to ‘Debug With Project References’.

<Choose><When Condition="'$(Configuration)' == 'Debug With Project References'"><ItemGroup><ProjectReference Include="..\SomeProject\SomeProject.csproj"><Project>{6CA7AB2C-2D8D-422A-9FD4-2992BE62720A}</Project><Name>SomeProject</Name></ProjectReference></ItemGroup></When><Otherwise><ItemGroup><Reference Include="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 you 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 are using separate repositories here, and the referenced projects aren’t included in the repository of the main solution, so relative paths don’t necessarily work. It is possible to specify the paths in an external ‘configuration’ file, and importing it into the .csprojfile. Additionally, as a fallback when the project can’t be found at the given path, it is also useful to load the DLL instead. This way you can choose to load one or more, but not necessarily all references as a project reference.

The configuration file (ProjectReferences.txt):

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"><PropertyGroup Condition="$(Configuration) == 'Debug With Project References'"><SomeProject>..\SomeProject</SomeProject></PropertyGroup>
</Project>

Segment from the .csproj file:

<Import Project="..\ProjectReferences.txt" />
<Choose><When Condition="Exists($(SomeProject))"><ItemGroup><ProjectReference Include="$(SomeProject)\SomeProject.csproj"><Project>{6CA7AB2C-2D8D-422A-9FD4-2992BE62720A}</Project><Name>SomeProject</Name></ProjectReference></ItemGroup></When><Otherwise><ItemGroup><Reference Include="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’ configuration, thus in all other scenarios the DLL will be loaded instead since the ‘Exists()‘ check will fail.

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 now have 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 be 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)

转载于:https://www.cnblogs.com/zhahost/p/4796343.html

Conditional project or library reference in Visual Studio相关推荐

  1. MFC libraries are required for this project. Install them from the Visual Studio installer (Individu

    MFC libraries are required for this project. Install them from the Visual Studio installer (Individu ...

  2. 解决Unity3D 创建UWP应用出现Exception: Failed to build Visual Studio project using arguments

    解决Unity3D 创建UWP应用出现Exception: Failed to build Visual Studio project using arguments 解决方案在下方 本人在使用Uni ...

  3. visual studio 2017 创建 android 本地共享库(.so) 并从 C# android 项目中调用

    Developing Xamarin Android Native Applications ★★★★★ ★★★★ ★★★ ★★ ★ February 23, 2015 by Ankit Asthan ...

  4. Visual Studio各版本工程文件之间的转换 [转载]

    原网址:http://www.cnblogs.com/jmliao/p/5594179.html Visual Studio各版本工程文件之间的转换 由于VS版本比较多,低版本无法直接打开高版本的工程 ...

  5. [转]Visual Studio 项目类型 GUID 清单

    转自:https://www.codeproject.com/Reference/720512/List-of-Visual-Studio-Project-Type-GUIDs Complete li ...

  6. [转]Cordova + Ionic in Visual Studio - 101 Tutorial [Part I]

    本文转自:http://binarylies.ghost.io/cordova-ionic-in-visual-studio/ Hi everyone, I thought about lending ...

  7. Visual Studio 和 .NET Framework 词汇表(vs2008)

    本词汇表定义了 Visual Studio 2008 和 .NET Framework 中的关键术语. .NET Compact Framework 一种独立于硬件的环境,用于在资源受限制的计算设备上 ...

  8. Visual Studio 和 .NET Framework 词汇表

    本词汇表定义了 Visual Studio 2008 和 .NET Framework 中的关键术语. .NET Compact Framework 一种独立于硬件的环境,用于在资源受限制的计算设备上 ...

  9. msbuild 语法_MSBuild - Visual Studio | Microsoft Docs

    MSBuildMSBuild 11/04/2016 本文内容 Microsoft 生成引擎是一个用于生成应用程序的平台.The Microsoft Build Engine is a platform ...

最新文章

  1. 哪些人适合学习web前端?
  2. ThinkPHP连接数据库
  3. oracle 9i rac Linux,请教高手!能在linux下安装两套oracle 9i RAC 数据库软件与实例吗?...
  4. flask中的信号机制
  5. keepalived+haproxy(双主)+nginx(静态)+lamp(动态)部署phpBB
  6. DOCX file format element list
  7. a标签点击跳转失效--IE6、7的奇葩bug
  8. maven--私服的搭建(Nexus的使用)
  9. python创建数组并运行_python-Cython中从现有数组和变量创建新数组...
  10. sql 没有调试 菜单_关于ThinkPHP5.1+的Log无法记录SQL调试记录的小经历
  11. collections 模块:更多数据结构
  12. python中正则表达式的用法_详解Python中的正则表达式的用法
  13. 直接内存回收中的等待队列
  14. NV16 转 NV12
  15. 在不受支持的 Mac 上安装 macOS Monterey 12(OpenCore Patcher)
  16. linux qt程序崩溃_Qt程式异常崩溃处理技巧(Win)
  17. Madgwick AHRS算法笔记
  18. Linux内核及主流Linux发行版对应关系汇总
  19. Unity游戏动画 从入门到住院 1
  20. Markdown使用学习笔记(第一次,拉的很,别骂了,别骂了)

热门文章

  1. 【POJ - 3250 】Bad Hair Day (单调栈)
  2. ajax get请求成功,成功()函数的AJAX GET请求
  3. aix解锁oracle用户,aix用户被锁定的解决办法
  4. php fast cgi nginx,通过fast-cgi连接php-fpm和nginx之间的连接是持久的(保持活动)连接吗?...
  5. ca开头的车是什么牌子_微电调研 | 把高速车摘了牌子当低速车卖还很普遍
  6. Win7+VMware10.0+CentOS 6.4+Tomcat,Win7访问不了CentOS6.4上的Tomcat
  7. leetcode914. 卡牌分组
  8. leetcode212. 单词搜索 II
  9. leetcode268. 缺失数字
  10. Linux必懂知识大总结(下)