简体   繁体   English

自动复制相关 DLL,无需二进制引用

[英]Automatically copy related DLLs without binary reference

Visual Studio 2015, C# Console project + C# Library projects: Visual Studio 2015,C# 控制台项目 + C# 库项目:

Is there a way to achieve automatically copying related project binary output to an output dir of a particular "master" project without using Project reference (and Copy local = True)?有没有办法在不使用项目引用(和 Copy local = True)的情况下自动将相关项目二进制输出复制到特定“主”项目的输出目录?

I want to be able to maintain the exe-s and dll-s loosely coupled without the actual binary reference, but can rely on the related dll-s are always copied to the right output dir (eg Debug/Release).我希望能够在没有实际二进制引用的情况下保持 exe-s 和 dll-s 松散耦合,但可以依赖相关的 dll-s 始终复制到正确的输出目录(例如调试/发布)。

Example:例子:

  • Master project: Console project named "Console"主项目:名为“Console”的控制台项目
  • Related project: Library project named "Library"相关项目:名为“Library”的图书馆项目
  • There is no need (and no will) to create binary reference from Console to Library, but we know we will need the Library DLL in the current dir while startup resolving Dependency Injection container components (in the EXE app).不需要(也不会)创建从控制台到库的二进制引用,但我们知道在启动解析依赖注入容器组件(在 EXE 应用程序中)时,我们将需要当前目录中的库 DLL。
  • We want to automate the building process on the local machine.我们希望在本地机器上自动化构建过程。
  • We want to easily add this kind of "reference" from another "master" projects (eg WPF GUI project), so the approach "Project dependencies + Build Events" isn't flexible enough.我们希望从另一个“主”项目(例如 WPF GUI 项目)轻松添加这种“引用”,因此“项目依赖项 + 构建事件”方法不够灵活。

I have only verified this solution on VS2017, but based on the date of the referenced MSDn article, it should work with VS2015 : use ReferenceOutputAssembly is your csproj.我只在 VS2017 上验证了这个解决方案,但根据引用的 MSDn 文章的日期,它应该适用于 VS2015:使用ReferenceOutputAssembly是你的 csproj。 Sadly, this is a property that can only be added via manual edit of the csproj.遗憾的是,这是一个只能通过手动编辑 csproj 添加的属性。

<ProjectReference Include="..\..\Calfitec\PglLibPAC\PglOdpm\PglOdpm.csproj">
  <Project>{261de855-9e8f-45de-b57d-fd4c7deb5f1b}</Project>
  <Name>PglOdpm</Name>
  <ReferenceOutputAssembly>false</ReferenceOutputAssembly>
</ProjectReference>

See also: How to have a Project Reference without referencing the actual binary另请参阅: 如何在不引用实际二进制文件的情况下拥有项目参考

This is the intended use of the feature:这是该功能的预期用途:

Sometimes you want a project reference from project B to project A to indicate a build-time dependency, but you don't actually want assembly B to reference assembly A (maybe because it's a runtime-only dependency or loaded using reflection).有时您希望从项目 B 到项目 A 的项目引用来指示构建时依赖项,但您实际上并不希望程序集 B 引用程序集 A(可能因为它是仅运行时依赖项或使用反射加载)。

TL; TL; DR;博士; The fact that Copy local even appears tells me that Console has a direct Add Reference dependency which kinda defeats the purpose of DI. Copy local甚至出现的事实告诉我,Console 有一个直接的 Add Reference 依赖项,这有点违背了 DI 的目的。 Just let Visual Studio do it's thing via Copy Local=true .只需让 Visual Studio 通过Copy Local=true来做这件事。

If:如果:

  1. Console has a Add Reference to Library控制台有一个对添加引用
  2. Console expects Library to appear in the same folder as Console at runtime控制台期望在运行时出现在与控制台相同的文件夹中
  3. You don't have a large number of projects in your solution您的解决方案中没有大量项目

...then you should just let Visual Studio do it's thing via Copy Local = true because Console has a direct dependency that must be solved and Dependency Injection (DI) doesn't even come into the equation. ...那么你应该让 Visual Studio 通过Copy Local = true来做这件事,因为 Console 有一个必须解决的直接依赖关系,并且依赖注入(DI)甚至没有进入等式。 If you have a large number of projects, it can be shown to speed builds considerably if the copy happens once at the end but you didn't indicate this is your scenario.如果您有大量项目,如果复制最后发生一次,则可以显示它大大加快了构建速度,但您没有指出这是您的场景。

If however:但是,如果:

  1. The aim is to use dependency injection目的是使用依赖注入
  2. Console does not have a Add Reference to Library控制台没有添加引用到库
  3. classes in Library are to be created dynamically via DI中的类将通过 DI 动态创建
  4. Library is to be optionally deployed into a different folder than Console, perhaps a folder known as Plugins or DI将选择性地部署到与 Console 不同的文件夹中,可能是一个称为 Plugins 或 DI 的文件夹

...then it's easier to have a Post Build step in one of your projects to copy dependant DLLs once to a single target folder. ...然后在您的项目之一中进行构建后步骤将依赖的 DLL 复制一次到单个目标文件夹会更容易。 This is especially the case when developing plugins/DI for a large solution.在为大型解决方案开发插件/DI 时尤其如此。

See < project >.参见<项目>。 Properties.Build Events.Post-build event command line Properties.Build Events.Post-build 事件命令行

DI?迪?

It sounds like you are wanting to use DI, but the appearing of Copy Local=true tells me that your program has a direct relationship to the library anyway.听起来您想使用 DI,但是Copy Local=true的出现告诉我您的程序无论如何都与库有直接关系。 What's the point of DI? DI的意义何在? Your's is already strongly-coupled.你的已经是强耦合了。

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

相关问题 在我的应用程序中使用sdk的dll而无需复制和注册 - Using dlls of sdk inside my app without copy and register 在运行时引用.dll? - Reference .dlls on runtime? 复制C ++ dll以生成任何通过扩展引用它们的C#项目的输出 - Copy C++ dlls to build output of any C# project that reference them by extension 引用 C:\\Programs 文件中的 dll,无需复制 - Reference dll in C:\Programs files without copy it 我可以拆分.net DLL而不必重新编译引用原始DLL的其他DLL吗? - Can I split a .net DLL without having to recompile other DLLs that reference the original one? .NET Framework类如何引用本机Windows DLL而不会变得特定于位? - How do .NET Framework classes reference native Windows DLLs without becoming bitness-specific? 在32位和64位处理器上开发,我应该如何构建我的项目以自动引用正确的dll? - Developing on both 32 bit and 64 bit processors, how should I structure my project to automatically reference the right dlls? 如何在不参考实际二进制文件的情况下获得项目参考 - How to have a Project Reference without referencing the actual binary 单二进制文件在子目录中找不到DLL - Mono binary can't find DLLs in subdirectory InstallUtil 是否会自动为您注册 .dll? - Does InstallUtil automatically register .dlls for you?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM