Thế Long
Thế Long

Reputation: 546

C# Using references/class library in a shared project

I have just learned and created a shared project in visual studio 2017. I have noticed that the shared project did not have the "Reference" to refer to other resources (other projects, class library, …). I even take a look at the .shproj file and saw that it only Import the class I have created inside the shared project.

My problem is that if I want to create an add-in app, I need use the class library to call the necessary API that is exposed by the origin software. How can I reference/add other project/ class library (or resources in general) to my shared project? Or is that even possible at all?

Part of my problem is also described here reference to a shared project from other shared project

But I need a more general solution. Thank you all for your help.

Upvotes: 4

Views: 5270

Answers (2)

Corey
Corey

Reputation: 16564

Long story short: shared projects don't, and realistically can't work that way.

In Visual Studio a shared project is just a container for files - source code, resources, etc - that you can add into other projects. This can be useful in some cases when you want to have the same code (and so on) in multiple projects without putting that code into a library.

Shared projects do not have references, do not have NuGet packages or anything, just the files that they contain. They don't even have the configuration data required to compile any source files they contain, and the compiler won't do much validation of the contents if the shared project isn't included into a full project of some sort.

And since the shared project doesn't have any way of specifying references or packages then you will need to add those references and packages to every project that links to the shared project. The compiler will tell you pretty quick if you miss one.

While it would be nice to have references in shared projects, it turns out to be much less simple than you might think. The same shared project can be included in projects that target different frameworks, platforms and architectures. Let's say you're building some code that will run on iOS, Mono, Windows .NET Framework and .NET Core, with specific code for each target and some shared code. If you try to add a NuGet dependency to the shared project it's going to blow up in your face on at least one of those. Same with most of the references. Add all the references you need for .NET Core and suddenly the other projects don't compile.

Upvotes: 7

Stephen Kennedy
Stephen Kennedy

Reputation: 21548

You need to add the reference in the project which consumes the shared project.

As an example, let's say you have "Project A" which references "Shared Project B", and you need to use Newtonsoft.Json in "Shared Project B". Since you can't add a reference to the shared project, you install the Newtonsoft Nuget package to Project A and your code in "Shared Project B" will automagically compile.

Upvotes: 4

Related Questions