Before reading this article please go through my previous article of the series:
This is part two of the series Visual Studio 2015 Features. In part 1, you learned about Custom Layout feature, like how can you manage different types of layout for different types of the system.
Today we are going to learn a very extensive and nice feature of Visual Studio 2015, Shared Project. As we all know, till know we can only share the library, assembly, services, etc. but there is not any option with previous Visual Studio to share the project itself.
But Visual Studio 2015 provides this feature and here you can create different kinds of project which is “Shared Project” and can share it with any type of project.
Shared Project is very useful when you are going to create some common functionality or shared logic which can be used with others application. It can be accessed by any type of application like Console Application, Windows Application, Phone application, etc.
Create Shared Project
To create a shared project, open Visual Studio 2015 and go to File menu and choose New and then choose Project. It will open a new Project dialog window, here from the Visual C# node, you can choose Shared Project from the right pane.
Specify the name of the project “MySharedProject" and click OK.
If you are using the Visual Studio 2015 with Windows 7 and it is your first time to setup Shared Project then you will get the following error,
To resolve this error you need to make some changes in the CodeSharing file of the Visual Studio 2015.
Go to the following location C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\CodeSharing and select the following file.
Open it with notepad and change the following line of code.
Find this code,
- <Import Project="$(MSBuildExtensionsPath32)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" Condition="Exists('$(MSBuildExtensionsPath32)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets')" />
-
- <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" Condition="!Exists('$(MSBuildExtensionsPath32)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets')" />
And change above line with the following line of code,
- <Import Project="$(MSBuildExtensionsPath32)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" Condition="false" />
- <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" Condition="true" />
Now everything is fine, you can try to create the shared project again and you will see, it has created; You will find your created project as in the following,
So, after adding the Shared Project, here I am going to add a entity class “Employee” which would be accessed in the different project.
- using System;
- using System.Collections.Generic;
- using System.Text;
-
- namespace MySharedProject
- {
- public class Employee
- {
- public int EmployeeId
- {
- get;
- set;
- }
-
- public string EmployeeName
- {
- get;
- set;
- }
- public string Address
- {
- get;
- set;
- }
- public int Salary
- {
- get;
- set;
- }
- }
- }
Use the Shared Project
Now, it is time to access this shared project with other application. So, go and create a Console Application “MyApp”.
To access Shared Project with this application, you just need to give the reference of the Shared Project.
So, right click on
References and choose
Add Reference.
It will open
Reference Manager for you where you can manage your references for the project. So, go to the
Shared Projects option and choose
Solution, you will see all the shared project related to this solution is available in right pane. Select your shared project and click
OK.
You can see in the following image, the Shared Project has been added with your
MyApp project. You can use the component of the Shared Project with your main project.
So, it is time to access the Shared Project’s class “
Employee” with “
MyApp” application. When you type
Employee, it will be not showing because we need to add the namespace for accessing the shared project.
So, we have used successfully the entity class of the Shared Project in Console App. So, make some change in the code and pass the value to the fields of the Employee class and run it.
Press F5, to run the project and you will find the following output for this.
So, today we learned about the Shared Project feature of Visual Studio 2015. I believe this is a very nice feature and we can implement it with our project for common functionality.
Thanks for reading this article, hope you enjoyed it.