【问题标题】:Using the same assembly references in csproj with different folder structures在具有不同文件夹结构的 csproj 中使用相同的程序集引用
【发布时间】:2014-08-29 08:41:39
【问题描述】:

我有一个 C# 项目 MyProject.csproj,它位于具有以下文件夹结构并引用 Dependency.dll 的解决方案中:

    • Dependency.dll
  • 项目
    • 我的项目
      • MyProject.csproj

因此,在 MyProject.csproj 中对 Dependency.dll 的引用具有类似这样的 HintPath:

..\..\Libs\Dependency.dll

现在我想在不同项目结构中的不同解决方案中使用 MyProject 作为源,无需修改。这是因为 MyProject 位于它自己的源代码控制存储库中,我在不同的解决方案中使用它作为 Mercurial 子存​​储库/Git 子模块。 (问题可能会在源代码控制级别解决...)这样的不同解决方案如下所示:

    • Dependency.dll
  • 我的项目
    • MyProject.csproj

请注意,MyProject 文件夹现在与 Libs 文件夹处于同一级别。因此,原来的 HintPath 现在是无效的(因为它应该是 ..\Libs\Dependency.dll)并且我得到了构建错误。

有没有办法解决这个问题,但在不同的解决方案中保持相同的 csproj?

我发现以下可能的解决方案很棒,但需要修改 csproj。这在我的情况下是最有可能的,但有时有一些外部组件我无法请求这样的修改,所以如果可能的话,我会寻找一些解决方案级别的覆盖。

谢谢。

【问题讨论】:

    标签: c# git mercurial csproj assembly-loading


    【解决方案1】:

    目前,我使用this blog post 中概述的技术解决了这个问题。

    <ItemGroup>
      <LibReferenceSearchPathFiles Include="..\..\Libs\**\*.dll">
          <InProject>false</InProject>
      </LibReferenceSearchPathFiles>
    </ItemGroup>
    <Target Name="BeforeResolveReferences">
      <RemoveDuplicates Inputs="@(LibReferenceSearchPathFiles->'%(RootDir)%(Directory)')">
        <Output TaskParameter="Filtered" ItemName="LibReferenceSearchPath" />
      </RemoveDuplicates>
      <CreateProperty Value="@(LibReferenceSearchPath);$(AssemblySearchPaths)">
        <Output TaskParameter="Value" PropertyName="AssemblySearchPaths" />
      </CreateProperty>
    </Target>
    

    这可以加载来自 Libs 子文件夹的 dll。如果所有 dll 都位于 Libs 文件夹的根目录中,则可以从 Include 值中删除第一个通配符。

    【讨论】:

    • 答案中的链接已失效“HTTP 错误 503。服务不可用。”.
    猜你喜欢
    • 2012-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多