是“复制本地"项目引用的传递?

Is quot;Copy Localquot; transitive for project references?(是“复制本地项目引用的传递?)

本文介绍了是“复制本地"项目引用的传递?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

写.建议的欺骗:因为这里的queston建议与链接问题,我宁愿认为它不是是骗子.

Wrt. the proposed dupe: Since this here queston suggests the opposite of the linked question, I'd rather like to think it is not a dupe.

首先,我确实阅读了 复制本地"和项目引用的最佳做法是什么?(也 this) 无论如何我都必须尝试一下,但是获得对此的一般反馈似乎是必要的,因为 <关于这些东西的 href="http://msdn.microsoft.com/en-us/library/vstudio/t1zz5y8c%28v=vs.100%29.aspx" rel="noreferrer">docs 太可怕了我只在 VS2010 上,也许他们在较新的版本中改变了一些东西,很高兴知道.

First, I did read What is the best practice for "Copy Local" and with project references? (also this) and I'll have to try this out anyway, but getting general feedback on this seems necessary as the docs on this stuff are horrible and I'm only on VS2010 and maybe they changed something in newer versions that'll be nice to know.

第二,我只对这个问题的项目参考感兴趣,因为我已经 阅读来自 GAC 的程序集的处理方式不同,而 GAC 与我的问题无关.

Second, I'm only interested in project references for this question as I've read that assemblies from the GAC are handled differently and the GAC is irrelevant for my problem.

第三,在阅读了建议的欺骗之后,但更重要的是这里的 answer@Albireo,区分 file 依赖项似乎也很重要,其中依赖项引用 dll 程序集文件和 project 依赖项(即我要问的),其中依赖项引用了一个项目,并且隐式地引用了该项目的输出文件.

Third, after reading the suggested dupe, but more so the nice answer here by @Albireo, it would also appear that it is important to differentiate file dependencies, where the dependency references a dll assembly file and project dependencies (i.e. what I'm asking about), where the dependency references a project and implicitly the output file of that project.

  • 2 个 C# 可执行项目
  • n C# dll 程序集项目
  • 这 2 个可执行文件具有不同的输出目录,因为它们将单独部署,因此它们在开发人员计算机上也是独立的
  • 这 2 个可执行文件依赖于某些 DLL 程序集(它们可能相互依赖)
  • 共有三个输出目录:
    • /x1 用于可执行 1 项目
    • /x2 用于可执行 2 项目
    • /lib 用于所有 dll 程序集
    • 2 C# executable projects
    • n C# dll assembly projects
    • The 2 executables have different output directories as they will be deployed separately and that way they're also separate on the developer machine
    • The 2 executables have dependencies on some of the DLL assemblies (which may depend on each other)
    • There are three output directories:
      • /x1 for executable 1 project
      • /x2 for executable 2 project
      • /lib for all the dll assemblies

      DLL 程序集allCopy Local设置为 false 以供其项目引用,因为它们都构建到相同的输出目录.

      The DLL assemblies all have Copy Localset to false for their project references, as they all build to the same output directory.

      这 2 个可执行项目已将它们引用的所有 DLL 程序集项目引用的 Copy Local 设置为 true直接将DLL复制到/x1 /x2中.

      The 2 executable projects have set Copy Local to true for all the DLL assembly project references they reference directly, so that the DLLs will be copied into /x1 /x2 respectively.

      问题现在是 wrt.到可执行项目直接引用但通过引用的程序集传递的DLL:Will程序集,当复制本地"在第一个程序集上设置为 true 时,通过另一个程序集传递引用,复制到可执行文件的输出文件夹?

      The question now is wrt. to DLLs that are not directly referenced by an executable project, but only transitively through a referenced assembly: Will assemblies, that are only referenced transitively through another assembly, be copied into the output folder of the executable, when "Copy Local" is set to true on the first assembly?

      示例:

      • x1.csproj(例如输出 = x1/one.exe)
        • 参考:dlA.csproj(例如 Output = lib/a.dll)与 Copy Local = *true*
        • (没有直接引用 b.dll)
        • x1.csproj (e.g.Output = x1/one.exe)
          • Reference: dlA.csproj ( e.g. Output = lib/a.dll) with Copy Local = *true*
          • (no direct reference on b.dll)
          • 参考:dlB.csproj(例如 Output = lib/b.dll)与 Copy Local = **false**
          • (没有直接引用 c.dll)
          • Reference: dlB.csproj ( e.g. Output = lib/b.dll) with Copy Local = **false**
          • (no direct reference on c.dll)
          • (没有进一步的相关参考资料)

          因此,我们有 one.exe -> 的逻辑依赖.a.dll ->b.dll ->c.dll,其中只有a.dll 明显被复制到one.exe 的输出目录.其他两个 dll 是否也会被复制到输出目录中? 这是否记录在某个地方?

          Thus, we have a logical dependency of one.exe -> a.dll -> b.dll -> c.dll, where only a.dll with obviously be copied to the output directory of one.exe. Will the other two dlls also be copied to the output directory? Is this documented somewhere?

          而且,是的,我试过了.而且,是的,它似乎可以工作,但我还没有足够努力地戳它,无论如何,我可能错过了更多的东西.(还有任何官方文档的问题.)

          And, yes, I tried it. And, yes, it seems to work, but I haven't poked it hard enough yet and anyway there maybe something more to it that I may have missed. (And also there's the question wrt. any official docs.)

          推荐答案

          似乎区分文件依赖项也很重要,其中依赖项引用 dll 程序集文件和项目依赖项(即我要问的),其中依赖项引用项目并隐含地输出该项目的文件项目.

          it would also appear that it is important to differentiate file dependencies, where the dependency references a dll assembly file and project dependencies (i.e. what I'm asking about), where the dependency references a project and implicitly the output file of that project.

          不是真的,不.

          MSBuild 并不真正关心引用是指向解决方案中的另一个项目还是指向 DLL.

          MSBuild doesn't really care if the reference points to another project in the solution or to a DLL.

          如果 ProjectA 依赖 ProjectB 来构建 ProjectA ProjectB 必须已经构建(并且最多日期),然后 MSBuild 将提取其 DLL(而不是其 C# 代码)并将其链接到 ProjectA.

          If ProjectA depends on ProjectB to build ProjectA ProjectB must be already built (and up-to-date), MSBuild will then pull its DLL (not its C# code) and link it to ProjectA.

          添加项目引用而不是 DLL 是语法糖".为了您的方便:这样 MSBuild 知道它必须选择引用项目的输出,无论输出是什么.

          Adding a project reference instead of a DLL is "syntactic sugar" for your convenience: this way MSBuild knows it must pick the output of the referenced project, whatever the output is.

          否则,您将不得不手动预构建依赖项,找到它的 DLL 并将其链接到项目,每当您切换构建配置、移动或重命名时都重复该过程.不太实用.

          Otherwise, you'll have to manually pre-build the dependency, find its DLL and link it to the project, repeating the process whenever you switch build configuration, move or rename things. Not really practical.

          另外两个dll也会复制到输出目录吗?

          Will the other two dlls also be copied to the output directory?

          如果直接从引用程序集的项目中使用依赖项中的任何类型的元素,则将复制该引用.

          If any kind of element from a dependency is used directly from the project where the assembly is referenced, that reference will be copied.

          一个例子可能是这个解决方案布局:

          An example could be this solution layout:

          • 我的解决方案
          • MySolution.ConsoleApplication
          • MySolution.FirstDependency
          • MySolution.SecondDependency
          • MySolution.ThirdDependency
          • MySolution.FourthDependency

          有了这个依赖链:

          • MySolution.ConsoleApplication
          • MySolution.FirstDependency
            • MySolution.SecondDependency
              • MySolution.ThirdDependency
              • MySolution.FourthDependency

              如果您构建此解决方案,您会注意到在 MySolution.ConsoleApplication 输出目录中将有 MySolution.FirstDependencyMySolution.SecondDependency<的 DLL/code> 和 MySolution.ThirdDependency 但没有 MySolution.FourthDependency 的 DLL.

              If you build this solution you'll notice that in MySolution.ConsoleApplication output directory there will be the DLLs for MySolution.FirstDependency, MySolution.SecondDependency and MySolution.ThirdDependency but no DLL for MySolution.FourthDependency.

              为什么会这样?当 MSBuild 构建 MySolution.SecondDependency 时,它注意到向 MySolution.FourthDependency 声明了一个依赖项,但由于它无法从MySolution.FourthDependencyMySolution.SecondDependency 代码中它决定执行一些优化";并从输出中省略 MySolution.FourthDependency 程序集.

              Why is it so? When MSBuild builds MySolution.SecondDependency it notices that there's a dependency declared to MySolution.FourthDependency, but since it can't find any usage of any kind of element from MySolution.FourthDependency in MySolution.SecondDependency code it decides to perform some "optimization" and omits MySolution.FourthDependency assembly from the output.

              过去,当我通过 NuGet AutoMapper 添加到深度依赖"时,同样的问题困扰着我:添加 AutoMapper 会添加两个程序集引用,AutoMapperAutoMapper.Net4,当第二个程序集需要对 .NET Framework 4 引入的新集合对象执行某种操作时,第二个程序集由第一个程序集通过反射加载.由于第二个程序集是通过反射加载的,MSBuild 认为它未使用并且没有懒得到处抄.

              This same issue bit me in the past when I added through NuGet AutoMapper to a "deep dependency": adding AutoMapper adds two assembly references, AutoMapper and AutoMapper.Net4, where the second assembly is loaded by the first through reflection when it needs to perform certain kind of action on the new collection objects introduced by the .NET Framework 4. Since the second assembly is loaded through reflection MSBuild thinks it's unused and doesn't bother to copy it around.

              所以,是的,只要您直接使用它们,它们就会被复制,而不是通过反射.

              So, yes, they will be copied as long as you're using them directly and not through reflection.

              这是否记录在某处?

              这种行为似乎是一种特征".在 MSBuild 中,当我遇到此问题时,我设法找到了 Microsoft 一些人的博客文章,但目前我无法再次找到它.

              This behavior seems to be a "feature" of MSBuild, I managed to find a blog post by some folks from Microsoft back when I experienced this issue, but I can't find it again at the moment.

              这篇关于是“复制本地"项目引用的传递?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:是“复制本地"项目引用的传递?

基础教程推荐