Monday, January 16, 2017

Target Multiple Platforms and Framework Versions with Visual Studio Shared Projects

Visual Studio 2015 included a useful new feature known as a Shared Project. Apparently borrowed from Xamarin Studio, the Shared Project is a unique project type in that does not represent any particular build target or platform; rather, it defines only a layout of files and folders which may be shared with other projects. While perhaps not immediately apparent in its usefulness, this new feature mitigates a long-time pain for maintainers of libraries which target multiple framework versions and/or platforms.

shared project type
Shared Project Type

Prior to Visual Studio 2015, if a file needed to be shared between multiple projects within a solution, developers could use Add As Link when adding the file to a project. This method adds a file reference to the project which may be located in any arbitrary path. The file is not copied, and the original file path is used during compilation. In this way, it is possible to define multiple projects, each with their own build configuration, which individually reference a common set of code files.

add as link
Add As Link

Add As Link 
is an effective way to share code, but because it must be maintained per-project it can be painfully tedious. In the case of a large solution with many files and many build targets, this prospect can be daunting.

Shared Projects to the Rescue

add shared project reference
Add Shared Project Reference

Rather than maintaining links to every code file in each project, developers can define a single Shared Project with the file structure as they desire, and add it to each compiled project as a special type of reference. Compiled projects can define their own assembly references and/or NuGet packages and any other specific build configurations without worrying about the structure of the code in the referenced Shared Project. No more links.

shared project references
Configuring Target Projects

This trick of course works for unit test projects, too. Create another Shared Project containing your tests and reference that as well as the relevant project (not the Shared Project) for each framework or platform target.

shared projects for tests
Configuring Unit Test Projects

Happy coding.


Shalini said...

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

Digital Marketing Company in Chennai

tessa tessa said...

This is a great post. I like this topic.This site has lots of advantage. It helps me in many ways.Thanks for posting this again.

SEO Company in India

sathya said...

Great post! I am actually getting ready to across this information, is very helpful my friend. Also great blog here with all of the valuable information you have Keep up the good work you are doing here.Well, got a good knowledge.

SEO Company in India