[英]How to correctly resolve DLL references through class libraries without adding the reference to the calling project
TL:DR How do I reference an assembly only in a class library rather than both the library and the calling project? TL:DR如何仅在类库中而不是在库和调用项目中都引用程序集?
I am building a utility library in C# in order to promote code reuse. 我正在C#中构建实用程序库,以促进代码重用。 In this instance, I am wanting to do something things with a TFS server and need to reference several assemblies from the TFS side of things: 在这种情况下,我想使用TFS服务器做一些事情,并且需要从TFS方面引用几个程序集:
I include these references in the class library called, say, Utility. 我将这些引用包括在名为Utility的类库中。 I then proceed to wrap objects in those assemblies in helper objects. 然后,我继续将那些程序集中的对象包装在辅助对象中。 There are no errors, intellisense works correctly, and so forth. 没有错误,智能感知正常,依此类推。
When I want to use that class library in another project inside the same solution, say, TestCLI, I add a reference to the Utility project by selecting the project from the solution references tab. 当我想在同一解决方案中的另一个项目(例如TestCLI)中使用该类库时,我可以通过从解决方案引用选项卡中选择项目来添加对Utility项目的引用。 I can then use the Utility classes without issue, until I go to build. 然后,我可以毫无问题地使用Utility类,直到进行构建为止。
When I build the solution, it throws an error such as: 当我构建解决方案时,它会引发如下错误:
The type 'Microsoft.TeamFoundation.VersionControl.Client.BranchObject' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.TeamFoundation.VersionControl.Client, Version=12.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
In the past, I have worked around this issue by adding the same references to the calling project (TestCLI) in addition to the class library (Utility). 过去,我通过在类库(Utility)之外添加了对调用项目(TestCLI)的相同引用来解决此问题。 I feel that this is defeating one of the purposes of having a class library and that I've missed a step in order to not have to worry about library references in my calling project. 我觉得这违背了拥有类库的目的之一,并且我已经错过了一个步骤,以便不必担心调用项目中的库引用。
Is there some way to resolve these dependencies without including the references in both the class library and the calling project? 有什么方法可以解决这些依赖关系,而不必在类库和调用项目中都包含引用? Am I structuring my solutions incorrectly? 我的解决方案结构不正确吗? Am I thinking about class libraries in the incorrect manner? 我是否在以错误的方式考虑类库?
The references are required because you are exposing objects from the other libraries, and then to use these classes the final program needs the references. 引用是必需的,因为要从其他库中公开对象,然后使用这些类,最终程序需要引用。
To avoid this you must hide the external objects, through a wrapper, a copy of the class or anything else, it depends primarily on what and why you are exposing those objects. 为避免这种情况,您必须通过包装,类的副本或其他任何东西隐藏外部对象,这主要取决于您公开这些对象的方式和原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.