1

我在一个解决方案中有两个 c# 项目。

第一个项目是一个winforms有多个类的项目,名为QuantumGUI. 第二个项目是一个class library有多个类的项目,名为QuantumDAL.

我的目标是从 QuantumDAL 中的一个类访问和设置 QuantumGUI 类或 Form.cs 中的变量。我尝试在我的 QuantumDAL 项目中添加对 QuantumGUI 的引用,但收到以下错误:“A Reference to ‘QuantumGUI’ could not be added. Adding the project as a reference would cause a circular dependency”.
我收到了类似的错误消息,尝试添加项目依赖项。当您考虑它时,错误消息是有道理的。我尝试过其他的,我认为可能的方法,但结果是空的。我相信必须有一个聪明的方法来完成这件事。
如果我以错误的方式处理这个问题,有没有办法让两个项目中的代码都可以访问一个“全局”类?感谢您花时间看这个。

4

1 回答 1

0

There are two problems with what you're trying to do:

First, as the IDE is warning you, you're about to create a circular dependency. This means that the compiler would need to build project A before it can build project B, but it would need to build project B before it can build project A. Neither project can go "first".

The second problem is that your WinForms project is most likely an executable, and you cannot add references to *.exe files via the IDE. You can add those references via the command-line, but the fact that Visual Studio is trying to stop you from doing it should be a red flag that it's a really bad idea.

The correct way to do what you want is to refactor the common classes into a third class library that you reference from both other projects. If needed, you can wire up events (in particular, look at the INotifyPropertyChanged interface and its event) that notify interested observer objects when things change.

于 2012-12-24T18:00:58.510 回答