3

Even though Shared Projects have been around since Visual Studio 2015 (maybe as early as VS 2013 update2), I've only recently learned about them. Today I spent time trying to learn how to use them following a tutorial I found Shared Project: An Impressive Feature of Visual Studio 2015 Preview. However, the one thing the author did in that tutorial, which won't work for us, is he created the Shared Project and 3 other projects, all within the same solution. Of course, you can do that, but in practice we're likely to want to create a Shared Project in some solution, and then as time goes by, include that Shared Project in other solutions.

So what I did is instead of putting the Windows Forms application into the same solution as the author of that C# Corner post did, I created a new solution with a Windows Forms project in it, then I tried to add the Shared Project from the first solution. First, I tried adding the .sln file. That failed miserably. Then I tried adding the .shproj file to the second solution. That failed miserably as well.

Next I shared here on SO for ways of addressing this. I found 2 posts: Adding references in a shared (.shproj) project and How do I add a reference to a Shared Code project (.shproj) from another project. The second one gave me an idea. I decided I would simply add the Shared Project, from the first solution, to the second solution by clicking on the second solution within Solution Explorer, then doing a "Add Existing Project". That worked.

But I wonder, is that the way you're supposed to use Shared Projects? If so, it seems to me as though I could just as well created a simple class library in the first solution and then added that class library project to the second solution. Is there something about Shared Projects that make them inherently better to use, if you add the Shared Project to a different solution, instead of just adding a regular class library project to a solution?

Rod
  • 4,107
  • 12
  • 57
  • 81

2 Answers2

4

A class library compiles into its own DLL and your original project references that DLL, whereas a project using a Shared Project will compile into a single assembly. One scenario I could think of with shared projects is that you can have single code base but has platform specific code sections marked by directives.

alans
  • 1,022
  • 9
  • 17
  • I've got a follow-up question. I opened File Explorer and looked at the folder for the Windows Forms app that I created, following the tutorial I linked to above, but having created it in its own solution. Yesterday I had brought in the Shared Project, referenced it in the .cs file, built it and it ran fine. I now see that the files from the Shared Project are NOT in the solution folder directory for the Windows Forms app. So, I'm now taking Shared Projects to mean that they're located in only 1 place on the developer's machine, but referred by other projects. Correct? – Rod Mar 28 '19 at 15:24
  • 1
    @Rod Yes, it doesn't bring in the actual folder into the current project folder. It will point to the original location. I was disappointed when I came to the same realization. Fortunately, we eventually figured a way to "share" a project through git submodules. It would actually import everything, including unit test projects, but... we learned to deal with it. – alans Mar 28 '19 at 15:36
  • Ah, thank you @alans! Now I get the whole point of Shared Projects! Basically its a way of having common code in only one place, which I see the advantage to. I'm familiar with git's submodules; sounds like something I should look into. – Rod Mar 28 '19 at 15:59
3

There is a good video on this subject even though it's being explained in the context of xamarin they do a good job i think. https://www.youtube.com/watch?v=G5ov0gLZWgQ

Personally I I would always go with PCL (portable class lib) rather than SAP (shared project). I use shared code projects as documentation container in my projects. The project green icon stands out really well. I keep everything there from markeddown doc files to stored procedures and etc.

unpantofar
  • 104
  • 5