Solving circular project dependency between C# and

2019-07-15 20:47发布

问题:

We have a solution with a number of projects.

In one particular case, we have two projects: 1) A C# project that does most of the work 2) A C++/CLI project that acts as a go-between to some native C++ code

The C# code calls into the C++ wrapper, all is well.

However, there is some new functionality that we are pulling in. On the managed side of the C++ wrapper (project #2), it requires some static methods in the managed C# code that is in project #1. However, Visual Studio will not let us mutually associate these two projects as it complains of a circular project reference. There is no circular class reference however.

Is there any solution to this problem that does not require a 3rd project as an intermediary?

回答1:

You can have A depend on B. For simplicity lets say A.EXE depends on B.DLL. Then when A initially calls B, it can give it an object of some type or interface that is defined in B and then B can turn around and call back into A at some later point.

In other words, B defines a base class or interface like "I want something that only A can do" but don't implement it. Then let A implements it, passes it to you, and calls it. This gets around the circular dependency without a third project. This applies to any pair of managed projects.