Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a ProjectReference to a project that is not in the same solution

While doing some refactoring of our projects and solution files, i have separated some .sln files to contain less projects.

Occasionally i need to reference some projects from outside the scope of the current .sln file.

For example, my initial state was this:

SOLUTION A

  • PROJ A
  • PROJ B

After refactoring it would look like this:

SOLUTION A_NEW

  • PROJ A

SOLUTION B_NEW

  • PROJ B

My question is -- Is it possible to add a ProjectReference node to a project that is not defined in the same VS solution? (in my case, having PROJ A have a project reference to PROJ B).

Also, if it is possible, is that recommended?

I know that this is not possible from the VS IDE, only by editing the .csproj file manually.

like image 371
lysergic-acid Avatar asked Jul 22 '12 16:07

lysergic-acid


People also ask

How do you add references to a project?

You can also right-click the project node and select Add > Project Reference. If you see a References node in Solution Explorer, you can use the right-click context menu to choose Add Reference. Or, right-click the project node and select Add > Reference. For more information, see How to: Add or remove references.


3 Answers

You can't do this. A project reference includes an identifying GUID for the referenced project, which is maintained in the solution file in order to track solution build options and dependencies. If you try to reference a project that is not in the solution, Visual Studio will complain.

You can add a file reference to the assembly produced by a project that's not in the solution, of course.

UPDATE: Since this got downvoted, I'll refine my answer.

Though it's technically possible to craft a project file that references another project outside the same solution, Visual Studio won't help you to do it easily. One very good reason why it's a bad idea to do this (that I've observed) is that whatever Solution Configuration and Platform you're building (the referencing project) will be ignored if MSBuild decides to build the referenced project - the default Configuration and Platform specified in that referenced project file will be used instead. Thus you may end up with a mixture of binary types in different folders.

like image 193
lesscode Avatar answered Sep 27 '22 01:09

lesscode


Temporarily add the project to the solution, add a reference to it, unload the project that now has a reference added to it, remove the referenced project, reload the project with the reference.

If you don't unload the project then the reference will be automatically removed by Visual Studio when the referenced project is removed.

As you can probably tell, Visual Studios not designed to do this and you'd be better defining a build order for the solutions and use assembly references instead.

like image 31
GrahamD Avatar answered Sep 26 '22 01:09

GrahamD


You can definitely add a project to a solution A that is in solution B. There is not any problem with that. From my experience, it's not something that I usually have done or do, but sometimes need to. This can be especially true on large projects where you need different nodes of your architecture to reuse same code base.

Hope this helps.

like image 36
Tigran Avatar answered Sep 24 '22 01:09

Tigran