Search code examples
c#visual-studioreferenceproject-reference

Reference in two projects


I've two projects say P1 and P2.

P1 has a reference of P2.

so I can access P2's methods from P1. But what if I want to access P1's methods from P2 then how can I access them?

I know I can't add P1's reference in P2?

If it is possible? If yes, then how?


Solution

  • As others pointed out circular references are the problem. It can't compile P2 before it has compiled P1, but if P1 depends on P2 it can't compile P1 it until P2 has compiled... Got the problem?

    Now the solutions:

    • The easy way out: Create a shared library where you put in your shared code of P1 and P2. This shared project can be referenced by both P1 and P2.

    • The better solution: Create an interface which you define in a shared library. Base your 'references' of P2 in P1 on the shared interface, not on the actual implementation. In that way you have a better testable solution and it is easier to replace parts of your code.