简体   繁体   English

.dll参考。 项目参考

[英].dll reference Vs. project reference

I have a project (project B) that has a reference to another project (project C), and uses some methods from this project. 我有一个项目(项目B),它引用了另一个项目(项目C),并使用了该项目中的某些方法。 Now, I have a third project (project A) which needs to use only project B methods (and these methods use project A methods) - Project A uses project C but not directly. 现在,我有第三个项目(项目A),该项目仅需要使用项目B的方法(这些方法使用项目A的方法)-项目A使用项目C,但不直接使用项目C。

  1. Do I need to add a reference to project C or just to project B? 我需要添加对项目C的引用还是仅对项目B的引用?
  2. If I'll add a .dll reference, can I just add reference to project B? 如果要添加.dll引用,是否可以仅添加对项目B的引用?
  1. Do I need to add a reference to project C or just to project B? 我需要添加对项目C的引用还是仅对项目B的引用?

Just B is enough. 仅B就足够了。 If you are adding project reference in Visual Studio, adding project reference of B is enough. 如果要在Visual Studio中添加项目引用,则添加B项目引用就足够了。 At compile time, dlls of both project (B and C) will be copied to executable path of A 在编译时,两个项目(B和C)的dll将被复制到A可执行路径

  1. If I'll add a .dll reference, can I just add reference to project B? 如果要添加.dll引用,是否可以仅添加对项目B的引用?

In that case you will only be required to add reference to the project B dll, but project C 's dll should be in the executable folder as well, since project B depends on it. 在那种情况下,您只需要添加对项目B dll的引用,但是项目C的dll也应该位于可执行文件夹中,因为项目B依赖于它。

In both cases you don't have to reference project C explicitly, since there will be no direct calls to project C from Project A. But remember in both cases you will end up with the Project C dll in your Project A executable. 在这两种情况下,您都不必显式引用项目C,因为不会从Project A直接调用项目C。但是请记住,在两种情况下,您最终都将在Project A可执行文件中使用Project C dll。

You may see: Managing Project References - MSDN 您可能会看到: 管理项目引用-MSDN

Seeing @David's comment , you can't have circular reference in your projects. 看到@David的评论 ,您的项目中没有循环引用。 Project A using methods of Project B and Project B using methods of Project A, you will not be able to compile this code. 使用项目B的方法的项目A和使用项目A的方法的项目B,您将无法编译此代码。 You need to redesign your code and have hierarchy in your projects like A -> B -> C 您需要重新设计代码,并在项目中具有层次结构,例如A-> B-> C

See: Why are circular references in Visual Studio a bad practice? 请参阅: 为什么Visual Studio中的循环引用是一种不好的做法?

You will most likely need both, since B depends on C . 由于B取决于C ,因此您很可能同时需要两者。 If there is a method signature exposing a type of C , you definitely will need the reference to make it compile. 如果存在公开C类型的方法签名,则肯定需要引用使其编译。 Else, you would probably need it at runtime. 否则,您可能会在运行时需要它。

If possible, always add it as reference, since IntelliSense will notice the project reference and will notice method changes before you compile. 如果可能,请始终将其添加为参考,因为IntelliSense会注意到项目参考,并且会在编译之前注意到方法的更改。

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

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