简体   繁体   English

如何轻松切换构建的参考路径?

[英]How to easily switch reference paths for build?

I have a project that is for a piece of software. 我有一个项目是一个软件。 So I reference DLL libraries for that software in my project so I can code and make some nice plugins and extensions for the software via their API. 所以我在我的项目中引用该软件的DLL库,这样我就可以通过他们的API编写代码并为软件制作一些不错的插件和扩展。

The problem is that the software has many kinds of versions: Enterprise, Lite, version 1.6, version 1.7, version 2.0, etc. If I want my project to work for all these different versions, I have to duplicate my project and re-point the DLL references to the respective software version's DLL library (I am doing this now). 问题是该软件有多种版本:Enterprise,Lite,1.6版,1.7版,2.0版等。如果我希望我的项目适用于所有这些不同版本,我必须复制我的项目并重新指向DLL引用相应软件版本的DLL库(我现在正在这样做)。 This is really annoying because my code base is the same for all versions, so when I make any updates, I have to synchronize all my duplicated projects so I have a build for each software version. 这真的很烦人,因为我的代码库对于所有版本都是相同的,所以当我进行任何更新时,我必须同步所有重复的项目,所以我有每个软件版本的构建。

Is there a way that I can have a single project, but before I build, pick which software version to build for? 有没有办法让我可以拥有一个项目,但在我构建之前,选择要构建的软件版本? I think I am looking for an easy way to update the paths of the DLL references in my project. 我想我正在寻找一种简单的方法来更新项目中DLL引用的路径。 Any ideas or tips would be greatly appreciated. 任何想法或提示将不胜感激。

(I can use Visual Studio 2008 or 2010 and .NET 3.5 or 4.0 if this helps) (如果有帮助,我可以使用Visual Studio 2008或2010和.NET 3.5或4.0)

I have very limited experience working with project files directly but I'm pretty sure you could add conditions to many of the different settings. 我直接使用项目文件的经验非常有限,但我很确定你可以为许多不同的设置添加条件。 In your case, you could add a condition to either a Reference or associated ItemGroup . 在您的情况下,您可以向Reference或关联的ItemGroup添加条件。

Then you could do something like: 然后你可以这样做:

<ItemGroup>
  <Reference Include="System" />
  <Reference Include="System.Core" />
  <Reference Include="System.Data.Linq" />
  <Reference Include="System.Xml.Linq" />
  <Reference Include="MyLibrary" Condition=" '$(ProjectVersion)'=='4' ">
    <HintPath>..\..\..\..\..\..\..\Libv4\MyLibrary.dll</HintPath>
  </Reference>
  <Reference Include="MyLibrary" Condition=" '$(ProjectVersion)'=='5' ">
    <HintPath>..\..\..\..\..\..\..\Libv5\MyLibrary.dll</HintPath>
  </Reference>
  ...
</ItemGroup>

The syntax may be wrong but the idea is there. 语法可能有误,但想法就在那里。 I believe it's doable this way. 我相信这样做是可行的。

Modify the HintPath Element in the Project File (as it mentioned in other answers is IMO not a very good idea because Visual Studio uses this Field to store where it should look if in all other "good" nothing was found). 修改项目文件中的HintPath元素(正如在其他答案中提到的那样,IMO不是一个好主意,因为如果在所有其他“好”中找不到任何内容,Visual Studio会使用此字段来存储它应该看起来的位置)。 In our Company we have a similar Situation. 在我们公司,我们有类似的情况。 We solved this Problem by setting the ReferencePath of every Project in the Solution. 我们通过在解决方案中设置每个项目的ReferencePath来解决此问题。

Do this by hand is very annoying if you have to do it for large Solutions. 如果你必须为大型解决方案做这件事,那么这样做是非常烦人的。 Because of this we have written an AddIn for VS that will do this for us. 因此我们为VS编写了一个AddIn for VS. Setting the ReferencePath of a Project is very easy so i think that this task could also be done by a VS Macro. 设置Project的ReferencePath很容易,所以我认为这个任务也可以由VS Macro完成。

Here's the code to set the ReferencePath by a Marco: 这是由Marco设置ReferencePath的代码:

    Sub SetReferencePath()
    Dim project As Project

    For Each project In DTE.Solution.Projects

        If project.Kind = CodeModelLanguageConstants.vsCMLanguageCSharp Then
            project.Properties.Item("ReferencePath").Value = "PATH1;PATH2;..."

        End If

    Next
    End Sub

The value of the ReferencePath Property is a Semicolon seperated List of Paths. ReferencePath属性的值是以分号分隔的路径列表。 Another advantage of using the ReferencePath, instead of setting the Hint Element, is that you can use absolute Paths on different Drives which is IMHO not possible with the Hint Element. 使用ReferencePath而不是设置提示元素的另一个优点是,您可以在不同的驱动器上使用绝对路径,这是使用提示元素无法实现的恕我直言。

I think you can just create a number of configurations, and add some msbuild logic to the proj file to conditionally set a reference path property and have your references use that property. 我认为您可以创建许多配置,并向proj文件添加一些msbuild逻辑,以有条件地设置引用路径属性并让您的引用使用该属性。 If you are familiar with editing the XML in the project file, then this will not be hard; 如果您熟悉在项目文件中编辑XML,那么这并不难; if you are not, then... hopefully someone else will help with the details. 如果你不是,那么......希望其他人能帮助解决细节问题。

Visual Studio 2008 and 2008 .csproj files are XML files. Visual Studio 2008和2008 .csproj文件是XML文件。 Reference paths are simple strings in these XML files, if you open a .csproj with a text editor, you can check this out (look for "HintPath", or check this http://geekswithblogs.net/murraybgordon/archive/2005/10/25/58103.aspx ). 参考路径是这些XML文件中的简单字符串,如果您使用文本编辑器打开.csproj,您可以检查它(查找“HintPath”,或查看此http://geekswithblogs.net/murraybgordon/archive/2005/ 10/25 / 58103.aspx )。

What you could do is write a small program that automates the build for different reference paths. 你可以做的是编写一个小程序,自动构建不同的引用路径。 Visual Studio build can be automated using DEVENV.EXE, see http://msdn.microsoft.com/en-us/library/xee0c8y7(VS.80).aspx for more on this. Visual Studio构建可以使用DEVENV.EXE自动完成,有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/xee0c8y7(VS.80).aspx

use $(configuration) and $(ProjectVersion) in reference path to switch references. 在引用路径中使用$(configuration)$(ProjectVersion)来切换引用。

  1. $(configuration) is "Debug" or "Release" or any other customized configure. $(configuration)是“Debug”或“Release”或任何其他自定义配置。
  2. $(ProjectVersion) is your environment virable. $(ProjectVersion)是你的环境可行的。

so you can finish all reference in one line, and it's more firm on debugging and releasing: 所以你可以在一行中完成所有引用,并且它在调试和发布时更加坚定:

    <Reference Include="YourLibrary">
        <HintPath>..\bin\$(configuration)\$(ProjectVersion)\YourLibrary.dll</HintPath>
    </Reference>

if your folders have prefix or suffix, you can free to attach it in line, for example: 如果您的文件夹有前缀或后缀,您可以自由地将其附加到行中,例如:

    <Reference Include="YourLibrary">
        <HintPath>..\bin\$(configuration)-test\Lib-$(ProjectVersion)\YourLibrary.dll</HintPath>
    </Reference>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM