Differencebetween a Shared Project and a Class Library in Visual Studio 2015

What is the difference between a Shared Project and a Class Library in Visual Studio 2015?

The difference between a shared project and a class library is that the latter is compiled and the unit of reuse is the assembly.

Whereas with the former, the unit of reuse is the source code, and the shared code is incorporated into each assembly that references the shared project.

This can be useful when you want to create separate assemblies that target specific platforms but still have code that should be shared.

See also here:

The shared project reference shows up under the References node in the Solution Explorer, but the code and assets in the shared project are treated as if they were files linked into the main project.


In previous versions of Visual Studio1, you could share source code between projects by Add -> Existing Item and then choosing to Link. But this was kind of clunky and each separate source file had to be selected individually. With the move to supporting multiple disparate platforms (iOS, Android, etc), they decided to make it easier to share source between projects by adding the concept of Shared Projects.


1 This question and my answer (up until now) suggest that Shared Projects was a new feature in Visual Studio 2015. In fact, they made their debut in Visual Studio 2013 Update 2

Trying to better understand Shared Projects and using them in different solutions

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.

Differences between Add Reference... Project vs Shared Project

As a sum up of differences found so far (take that with a grain of salt as I'm not an expert):

Shared Projects vs normal Projects (Class Library, WPF Application, Windows Forms Applications, etc...) added as a reference:

  • Files in Shared Projects can use code from projects where the Shared Project is added as a reference if all of those projects do have the code to be used.

  • Shared Projects do have less suggestions when "Add -> New Item..." as they are more generic.

  • Shared Projects are not compiled to .dll or .exe but added with source code to the project referencing them and therefore can be debugged step by step.

different between class library and portable class library in visual studio

The Portable Class Library project type enables you to write and build managed assemblies that work on more than one Microsoft platform, whereas the "normal" Class Library project type doesn't.

"Microsoft platforms" include .NET Framework, Windows Phone, .NET for Windows Store Apps, Silverlight, Xbox; all in various versions or flavors.

Source

Go through this for converting from one class library to another type. (See also: this relevant SO question.)

Class Library project in Visual Studio 2015

Quite a few options here:

1) Use class library project from Windows subsection:

Sample Image

2) Search for a template online. There is a link that reads: Click here to go online and find templates (at the bottom of the dialog shown in the printscreen).

There you can search for a class library in the Web section:

Sample Image

3) Modify VS installation and check Microsoft Web Developer Tool. More details here.

4) Try reinstalling ASP.NET templates.

Referencing shared project within solution structure

A 'shared project' is effectively a way of sharing the source code between multiple projects without having to build a DLL (like in a class library). You can think of it as literally copy pasting the source code of SharedProject into Class library A and Class library B.

It should be fairly obvious at this point then that Class library A and Class library B do have the same classes defined in SharedProject, however they both define them, rather than both referencing the same classes.

Your initial intuition is correct, the correct way to do it is to replace it with a class library, which is exactly what it's for. You'll then have a SharedProject.dll that both A and B reference, and then C will reference A.dll, B.dll and SharedProject.dll, and it should all be fine (barring version mismatches).

Portable Class Library vs. library project

Portable class libraries are platform independent. They do not use conditional compilation and unmanaged code, they have no UI inside (UI is platform dependent). This is because PCL should work on all specified platforms which was chosen as a target. Also, availability of features depends on selected targets.

So a PCL can be referenced by any project which target is specified in the PCL settings. But libraries of other types can be referenced only by projects which have the same target or by upper subsets of .Net (for example, Silverlight libraries can be used in Windows projects but not vice versa).

More about restrictions and features of PCL can be found on two links bellow:

  1. Share functionality using Portable Class Libraries
  2. Cross-Platform Development with the Portable Class Library

On the first link you can read about what is PCL in general. And on second - info about targets and features.

Hope this helps.

EDIT: See also What is a Portable Class Library?

Class library references on Visual Studio 2015

The new feature of .NET Core and Class Library (Package) is that it targets multiple platform and will compile into multiple assemblies which get automatically packaged into a nuget package.

When your class library targets multiple targets, it will compile to all of them. So if a certain library is only available on full .NET framework but not on .NET Core or other target framework, then you may receive intellisense if your editor is set to .NET 4.5. More information can be found in my other recent answer.

You can switch back and forth with the pull down menu on top left of the coding window, show in the screenshot below.

Sample Image

If you do not want to target a certain framework, you have to remove it's moniker from the project.json file or use preprocessor directives to write platform specific code or libraries/replacements.

.NET Core is heavily modularized and most of only the core modules are referenced in the default project and if you need additional one you need to reference them within the dotnet5.x section.

Basically you have multiple places with "dependencies" in your project.json, a global one where you can add dependencies which are available on all targeted frameworks and one within each "frameworks" section for each of the targets only.

Visual Studio 2015 Class Library (I'm using C#)

It looks like you have created .NET Core class library. You can determine this based on the extension of the project file: *.csproj has been used with classic .NET framework projects, and *.xproj was introduced for .NET Core. Most probably, you don't need a .NET Core class library. So, your problem will go away if you create .NET Framework Class Library project.



Related Topics



Leave a reply



Submit