简体   繁体   English

Visual Studio C#.dll与项目引用

[英]Visual Studio C# .dll vs. project referencing

I have two C# projects in my Visual Studio solution. 我的Visual Studio解决方案中有两个C#项目。 Project B needs to reference code in Project A. Both projects need to be compiled to dll's and then used in an application. 项目B需要引用项目A中的代码。两个项目都需要编译为dll,然后在应用程序中使用。

In Project BI can set a reference to project A and that allows things to compile. 在Project BI中,可以设置对项目A的引用,并且可以进行编译。 But when I actually use the resulting dll's in my application it throws a missing assembly reference error. 但是,当我在应用程序中实际使用生成的dll时,它将引发缺少的程序集引用错误。

My current solution is to tell Project B to reference the bin/debug/ProjectA.dll. 我当前的解决方案是告诉Project B引用bin / debug / ProjectA.dll。 Everything works when running the application with this configuration. 使用此配置运行应用程序时,一切正常。 But this solution has a number of problems. 但是这种解决方案有许多问题。 For instance, any code written in Project A that B relies on wont be visible to B until A has been rebuilt. 例如,在项目A中编写的任何B依赖的代码都不会对B可见,直到重建A。 And building the solution relies on A being built first. 构建解决方案依赖于首先构建的A。

Is there a way that I can add a reference to Project A but have the resulting dll built so it looks for the ProjectA.dll reference and not the project itself? 有没有一种方法可以添加对Project A的引用,但要构建生成的dll,以便它查找ProjectA.dll引用而不是项目本身? I expect there is, but my google searching has resulted in no answers. 我希望有,但是我的Google搜索没有结果。

For instance, any code written in Project A that B relies on wont be visible to B until A has been rebuilt. 例如,在项目A中编写的任何B依赖的代码都不会对B可见,直到重建A。 And building the solution relies on A being built first. 构建解决方案依赖于首先构建的A。

This is not a problem, this is, in my view, a benefit. 这不是问题,我认为这是一种好处。 As you are guaranteed to not be able access functionality which is not there. 保证您将无法访问不存在的功能。 In case of a new functionality, one may say, it's not a big deal: I can not find it, got an exception/error so I know something went wrong. 有人可能会说,如果有一项新功能,那没什么大不了的:我找不到它,遇到了异常/错误,所以我知道出了点问题。 But what about updated code ? 但是更新的代码呢? In this way you are guaranteed that B will use only most updated version of A . 这样,可以确保B将仅使用A最新版本。

Is there a way that I can add a reference to ProjectA but have the resulting dll built so it looks for the ProjectA.dll reference and not the project itself? 有没有一种方法可以添加对ProjectA的引用,但要构建生成的dll,以便它查找ProjectA.dll引用而不是项目本身?

You may simply implement post-build event on your project A which copies it and all dependent DLLs in some predefined central location, where B.dll will be copied too. 您可以简单地在项目A上实现构建后事件,该事件将项目及其所有相关DLL复制到某个预定义的中央位置,在B.dll位置还将复制B.dll If your B.dll depends on A.dll , OS, following default behavior, will search for A.dll in the same folder where B.dll is, for first, before checking %PATH% variable content. 如果您的B.dll依赖于A.dll ,则操作系统将按照默认行为在检查%PATH%变量内容之前首先在与B.dll相同的文件夹中搜索A.dll

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM