简体   繁体   English

如何通过类库正确解析DLL引用而不将引用添加到调用项目中

[英]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方面引用几个程序集:

  • Microsoft.TeamFoundation.Client Microsoft.TeamFoundation.Client
  • Microsoft.VersionControl.Client Microsoft.VersionControl.Client
  • Microsoft.WorkItemTracking.Client Microsoft.WorkItemTracking.Client

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.

相关问题 如何在不调用DLL作为C#引用的情况下从一个项目调用另一个项目的类? - How to call a class from a project to another project without call the DLL as reference in C#? 添加引用Project vs DLL - Adding reference Project vs DLL 共享DLL而不添加参考 - Sharing dll without adding reference 通过反射调用动态加载的DLL方法,但目标引用了一个DLL - Calling a dynamically loaded DLL method through reflection, but the target references a DLL 我可以创建动态 web 服务客户端引用而不将其引用添加到项目的服务引用中吗? - Can I create dynamic web service client reference without adding its reference to service references of the project? C#在另一个项目中添加对类的引用而不引用该项目 - C# Adding a reference to a class in another project without referencing the project 添加dll参考与项目参考的影响 - Impact of adding dll reference vs project reference 如何在不添加对类库的引用的情况下访问另一个类库中的视图模型和项目模型 - How to access view models and models of project in another class library without adding reference to the class library 通过本地项目引用导入NuGet引用 - Importing NuGet references through a local project reference 向VS 2008项目添加DLL引用 - Adding a DLL reference to a VS 2008 project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM