C# 主项目和子项目引用

发布于 2024-09-12 01:35:47 字数 255 浏览 1 评论 0原文

使用 Visual Studio 2008:

解决方案内有一个主项目 (Project1),同一解决方案内还有其他项目(这些项目是 Excel 2007 工作簿模板)。这些其他项目需要引用 Project1 中的类和对象,并且 Project1 需要能够访问 Workbook 项目。

我现在所拥有的是将其他项目设置为依赖于 Project1,并且将引用添加到 Project1 的其他项目中。

这是完成我想要完成的任务的正确方法还是还有其他方法?

Using Visual Studio 2008:

Within the Solution there is a main project (Project1), within the same solution there are other projects (these projects are Excel 2007 Workbook templates). These other projects need to reference classes and objects within Project1, and Project1 needs to be able to access the Workbook projects.

What I currently have now is the other projects are set to be dependent upon Project1 and a reference is added to the other projects for Project1.

Is this the correct way to do what I am trying to accomplish or is there another way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

居里长安 2024-09-19 01:35:47

您不能让两个项目相互依赖(这是完全合乎逻辑的;应该先构建哪个项目?)。您应该将 Project1 和 Workbook 项目都需要的部分移动到一个单独的项目中,该项目可以被 Project1 和 Workbook 项目引用。


更新
有几种不同的方法可以处理这种情况,并且在不了解您的应用程序的详细信息的情况下,我无法给出任何具体的解决方案。但一种方法是使用依赖注入。您可以提取此功能的接口,而不是让 Workbook 项目对 Project1 中的代码具有硬连线依赖关系。该接口放置在另一个项目中,Project1 和 Workbook 项目都可以引用该接口。

然后我假设 Project1 将在 Workbook 项目中创建类型的实例。这意味着您应该更改这些类型的构造函数,使其具有此接口类型的参数。 Project1 将包含接口的实现,该接口在创建工作簿项目类型时将传递给它们。

这样,工作簿项目代码就可以使用 Project1 中的代码,而无需引用它。

You cannot have two projects depend on each other (this is entirely logical; which project should be built first?). You should move the parts in Project1 that is needed both by Project1 and the Workbook projects into a separate project, that can be referenced both by Project1 and the Workbook projects.


Update
There are several different ways of handling this situation, and without knowing the details of your application I can't give any specific solution. But one way is to use dependency injection. Instead of having the Workbook project have a hard-wired dependency on the code in Project1, you can extract an interface for this functionality. This interface is placed in another project that can be referenced both by Project1 and the Workbook project.

Then I assume that Project1 will create instances of types in the Workbook project. This means that you should alter the constructors of these types to have a parameter if this interface type. Project1 will contain an implementation of the interface that it will pass to the Workbook project types when creating them.

That way the Workbook project code can use code from Project1 without having a reference to it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文