C# .csproj 程序集的多个提示路径

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15666618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:31:37  来源:igfitidea点击:

.csproj multiple hint paths for an assembly

c#visual-studio-2010msbuildcsproj

提问by japreiss

I'm packaging example code for an SDK distribution. In the distribution, the relative path from code to the SDK assemblies is different from the build machine. For example:

我正在为 SDK 发行版打包示例代码。在发行版中,从代码到 SDK 程序集的相对路径与构建机器不同。例如:

Distribution

分配

csharp/bin/assembly.dll
example/ex1/ex1.csproj

Build Machine

构建机器

foo/sdk/csharp/bin/assembly.dll
bar/baz/quux/ex1/ex1.csproj

Assume that I can't move anything. Is there a way I can instruct ex1.csprojto look in both

假设我不能移动任何东西。有没有一种方法可以指示我同时ex1.csproj查看两者

../../csharp/bin/and../../../../foo/sdk/csharp/bin/for assembly.dll?

../../csharp/bin/../../../../foo/sdk/csharp/bin/assembly.dll

In C++ I'd put the dependency path in a standalone property sheet and distribute a different version with the SDK. But C# doesn't have property sheets, and I don't want to maintain two versions of the full project.

在 C++ 中,我将依赖路径放在一个独立的属性表中,并使用 SDK 分发不同的版本。但是C#没有属性表,我不想维护完整项目的两个版本。

I've seen this questionwhich states that I can't use multiple <HintPath>tags, so I'm looking for another way to approximate the same behavior.

我见过这个问题,它指出我不能使用多个<HintPath>标签,所以我正在寻找另一种方法来近似相同的行为。

采纳答案by japreiss

I found a hacky solution that works for my case, where the parent directory is guaranteed to be different somewhere up the tree:

我找到了一个适用于我的案例的 hacky 解决方案,其中父目录保证在树上的某个地方是不同的:

<Choose>
  <When Condition="Exists('$(MSBuildProjectDirectory)\..\..\example')">
    <ItemGroup>
      <Reference Include="Assembly ...">
        <HintPath>..\..\csharp\bin\assembly.dll</HintPath>
      </Reference>
    </ItemGroup>
  </When>
  <Otherwise>
    <ItemGroup>
      <Reference Include="Assembly ...">
         <HintPath>..\..\..\..\..\foo\sdk\csharp\bin\assembly.dll</HintPath>
      </Reference>
    </ItemGroup>
  </Otherwise>
</Choose>

回答by Axarydax

You could subst the /csharp/binfolder into a drive (differently on each machine), for example X:and then reference X:\or X:\binon both machines, as the path will now be the same.

例如,您可以将/csharp/bin文件夹添加到驱动器中(在每台机器上不同),X:然后引用X:\X:\bin在两台机器上,因为路径现在将相同。

回答by Filip De Vos

Add the secondary path as follows to the general property group. in the csproj file

将辅助路径如下添加到常规属性组。在 csproj 文件中

<PropertyGroup>
    <ReferencePath>..\..\..\..\..\foo\sdk\csharp\bin\</ReferencePath>
    ...
</PropertyGroup>

The ReferencePathproperty is intended to be specified when executing MsBuild, but it will work fine like this.

ReferencePath属性旨在在执行 MsBuild 时指定,但它会像这样正常工作。

回答by Ian Vink

Simply add the build server location of the DLLs as a Reference path on the project. Seems to do the trick nicely and is very simple. Works only if you know the build server's folder of the DLLs.

只需将 DLL 的构建服务器位置添加为项目上的参考路径。似乎很好地完成了这个技巧,而且非常简单。仅当您知道 DLL 的构建服务器文件夹时才有效。

enter image description here

在此处输入图片说明

回答by Wolf5

The simplest way since only ONE HintPath can be used is to use the all-so-nice Condition attribute like this:

由于只能使用 ONE HintPath,最简单的方法是使用非常好的 Condition 属性,如下所示:

<Reference Include="TheAssembly">
    <HintPath Condition="Exists('..\My\Assembly\Path')">..\My\Assembly\Path\TheAssembly.dll</HintPath>
    <HintPath Condition="Exists('..\..\My\Assembly\Path')">..\..\My\Assembly\Path\TheAssembly.dll</HintPath>
    <HintPath Condition="Exists('..\..\..\My\Assembly\Path')">..\..\..\My\Assembly\Path\TheAssembly.dll</HintPath>
    <HintPath Condition="Exists('..\..\..\..\My\Assembly\Path')">..\..\..\..\My\Assembly\Path\TheAssembly.dll</HintPath>
    <HintPath Condition="Exists('..\..\..\..\..\My\Assembly\Path')">..\..\..\..\..\My\Assembly\Path\TheAssembly.dll</HintPath>
    <HintPath Condition="Exists('..\..\..\..\..\..\My\Assembly\Path')">..\..\..\..\..\..\My\Assembly\Path\TheAssembly.dll</HintPath>
    <HintPath Condition="Exists('..\..\..\..\..\..\..\My\Assembly\Path')">..\..\..\..\..\..\..\My\Assembly\Path\TheAssembly.dll</HintPath>
    etc...
</Reference>

So the answer to the question would be this:

所以这个问题的答案是这样的:

<Reference Include="assembly">
    <HintPath Condition="Exists('..\..\csharp\bin')">..\..\csharp\bin\assembly.dll</HintPath>
    <HintPath Condition="Exists('..\..\..\..\foo\sdk\csharp\bin')">..\..\..\..\foo\sdk\csharp\bin\assembly.dll</HintPath>
</Reference>

If multiple conditions matches, the last one will be used.

如果多个条件匹配,将使用最后一个。

回答by JReis

I am using this solution without any problem:

我使用这个解决方案没有任何问题:

<Reference Include="log4net">
  <HintPath>
     $(SolutionDir)packages\log4net.2.0.8\lib\net45-full\log4net.dll
  </HintPath>
</Reference>

Got it from here: use hint paths relative answer from LeonidVasilyev

从这里得到它: 使用来自 LeonidVasilyev 的提示路径相对答案