Conditional project or library reference in Visual Studio
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)
Conditional project or library reference in Visual Studio的更多相关文章
- [转]List of Visual Studio Project Type GUIDs
本文转自:http://www.codeproject.com/Reference/720512/List-of-Visual-Studio-Project-Type-GUIDs There isn' ...
- 将 project.json 项目转换为 Visual Studio 2015 解决方案
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
- Visual Studio各版本工程文件之间的转换
转载于:http://www.cnblogs.com/jmliao/p/5594179.html 由于VS版本比较多,低版本无法直接打开高版本的工程文件,通过对工程文件进行一些修改可以解决这些问题. ...
- [转]Cordova + Ionic in Visual Studio - 101 Tutorial [Part I]
本文转自:http://binarylies.ghost.io/cordova-ionic-in-visual-studio/ Hi everyone, I thought about lending ...
- (英文版)使用Visual Studio 2015 编写 MASM 汇编程序!
原文地址:http://kipirvine.com/asm/gettingStartedVS2015/index.htm#CreatingProject Getting Started with MA ...
- visual studio 2017 创建 android 本地共享库(.so) 并从 C# android 项目中调用
Developing Xamarin Android Native Applications ★★★★★ ★★★★ ★★★ ★★ ★ February 23, 2015 by Ankit Asthan ...
- [转]Visual Studio 项目类型 GUID 清单
转自:https://www.codeproject.com/Reference/720512/List-of-Visual-Studio-Project-Type-GUIDs Complete li ...
- Visual Studio各版本工程文件之间的转换 [转载]
原网址:http://www.cnblogs.com/jmliao/p/5594179.html Visual Studio各版本工程文件之间的转换 由于VS版本比较多,低版本无法直接打开高版本的 ...
- Visual Studio 2017中使用Libman管理客户端库
什么是Libman 微软在Visual Studio 2017 15.8版本中内嵌了一个新的工具Library Manager. Library Manager(简称Libman)是一个客户端库管理工 ...
随机推荐
- Swift-UIButton
// let bt = UIButton(frame:CGRectMake(10,20,50,50)) let button = UIButton(frame:CGRectMake(10, 150, ...
- marked.js简易手册
marked.js简易手册 本文介绍的是marked.js.秉持"来之即用"的原则,对它进行简要的翻译和归纳, 安装 在网上引用或者是引用本地文件即可.要么就用命令行: npm i ...
- 轮播插件unsilder 源码解析(一)---源码解析
jq扩展内容 $.fn.unslider = function(opts) { return this.each(function(index,elem) { var $this = $(elem); ...
- git误删文件找回方法/git版本回退方法
使用git命令 git rm css/\*.css 我删掉了css文件夹下所有以.css结尾的文件,那么要怎样才能把文件找回来呢,下面说说方法,删掉其他的文件也是一样的方式找回. 第一步:使用git ...
- SqlServer批量刷数据执行事务回滚语句备份
企业进行对数据库执行刷数据工作,一段很长的语句希望同时成功或者失败时用到. 1.建立测试环境 /**************************************************** ...
- struts1和struts2的区别
1. 在Action实现类方面的对比:Struts 1要求Action类继承一个抽象基类:Struts 1的一个具体问题是使用抽象类编程而不是接口.Struts 2 Action类可以实现一个Acti ...
- ORACLE_UNQNAME
之前在自己笔记本win7系统上安装了oracle 11g,当时为了节省资源,没启用EM. 此时查看EM状态: C:\Windows\system32>emctl status dbconsole ...
- js储存参数的数组arguments
js函数中有个储存参数的数组arguments ,所有函数获得的参数会被编译器挨个保存到这个数组中.于是我们的js版支持参数默认值的函数可以通过另外一种变通的方法实现 function simue ( ...
- 如何设置redis中hash的field的expire ?
redis > hset expire:me name tom (integer) redis > hget expire:me name "tom" redis &g ...
- R语言常用函数
统计: mean:平均数sd:Standard Deviation 标准差var:方差median:中位数cov:协方差cor:相关系数 #环境ls/objectsrmhelp() library() ...