解决另一个项目重命名的程序集依赖项

Resolving another project's renamed assembly dependency

我有一个解决方案,我需要为第三方客户打造一个组件。这导致 class 结构如下:

Project1
  References
    MyBrandedAssembly.dll (namespace: MyAssembly)
Project2
  References
    Project1

当 MsBuild 构建 Project1 时,它可以将 MyAssembly 命名空间解析为 MyBrandedAssembly.dll:

Primary reference "MyAssembly".
  Resolved file path is "SolutionPath\Binaries\MyBrandedAssembly.dll".
  Reference found at search path location "{HintPathFromItem}".

但是构建Project2时,无法解析二阶引用:

Dependency "MyAssembly".
  Could not resolve this reference. Could not locate the assembly "MyAssembly". 
   Check to make sure the assembly exists on disk. If this reference is required by your  
   code, you may get compilation errors.
    For SearchPath "SolutionPath\Project1\bin\Debug".
    Considered "SolutionPath\Project1\bin\Debug\MyAssembly", but it didn't exist.

为什么不呢?我怎样才能强迫它?

您可以将预构建事件添加到项目 csproj 文件,以将 dll 从项目 1 输出 bin 文件夹复制到项目 2 bin 文件夹。这将允许程序集解析器找到该程序集。

将此添加到您的 project2.csproj

<Target Name="BeforeBuild">
    <Delete Files="$(ProjectDir)bin$(Configuration)\MyBrandedAssembly.dll" />
    <Copy SourceFiles="$(ProjectDir)..\<Project1>\bin$(Configuration)\MyBrandedAssembly.dll" DestinationFolder="$(ProjectDir)bin$(Configuration)\" />
</Target>