- .NET Core - Migrations
- Restoring and Building & MSBuild
- .NET Core - MSBuild & project.json
- .NET Core - SDK
- Managed Extensibility Framework
- .NET Core - Testing Library
- Running Tests in Visual Studio
- .NET Core - Create a Testing Project
- .NET Core - PCL Troubleshooting
- Creating a Xamarin.Forms Project
- Sharing .NET Core Libraries
- Adding References to Library
- .NET Core - Portable Class Library
- Create .NET Standard Library
- Windows Runtime & Extension SDKs
- .NET Core - Metapackage
- .NET Core - MSBuild
- Create UWP App with .NET Core
- .NET Core - Package References
- .NET Core - Project Files
- .NET Core - Modularity
- .NET Core - Code Execution
- .NET Core - Garbage Collection
- .NET Core - Numerics
- .NET Core - Getting Started
- .NET Core - Environment Setup
- .NET Core - Prerequisites
- .NET Core - Overview
- .NET Core - Home
.NET Core Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
.NET Core - Portable Class Library
In this chapter, we will discuss what is PCL (Portable Class Library), and also why we need PCL. To understand this concept, let us open the class pbrary project folder which we have created in the previous chapter.
In this folder, you can see that in addition to project.json and CS files we also have *.xproj file, and that is because Visual Studio setup .NET Core project type as *.xproj instead of *.csproj.
As mentioned by Microsoft, *.xproj will be going away, but it is still here in preview 2 toopng. As we have covered that UWP apppcation uses the *.csproj.
Now it is actually not feasible to get *.csproj to reference and *.xproj and that functionapty is not going to be implemented because *.xproj will move out.
So instead, we need a class pbrary which can be shared between the console app and the UWP app and here comes PCL.
What is PCL
Let us now understand what PCL is −
The Portable Class Library project enables you to write and build managed assembpes that work on more than one .NET Framework platform.
You can create classes that contain code you wish to share across many projects, such as shared business logic, and then reference those classes from different types of projects.
It can also help you build cross-platform apps and pbraries for Microsoft platforms quickly and easily.
Portable class pbraries can help you reduce the time and costs of developing and testing code.
Use this project type to write and build portable .NET Framework assembpes, and then reference those assembpes from apps that target multiple platforms such as Windows and Windows Phone, etc.
Let us now remove the class pbrary which we have created from the Solution Explorer. At the same time, delete it from the Solution folder and further add a new project item.
Select the Visual C# → Windows template in the left pane and select Class Library (Portable) in the middle pane.
Enter StringLibrary in the name field and cpck OK to create this project.
Now we need to select the target frameworks to reference. Let us select Windows Universal and ASP.NET Core for a moment then we will retarget it. Cpck OK.
You can see that it has created a new project in PCF format. Let us now right-cpck StringLibrary project in the Solution Explorer and select Properties.
Cpck on the Target .NET Platform Standard.
Cpck Yes; it is now the same class pbrary with one minor difference. The difference is that it can be used by UWP as well, because it contains *.csproj file instead of *.xproj.
Let us now add a new class; for this, you need to right-cpck on project in Solution Explorer and select Add → Class...
Select class in the middle pane and enter StringLib.cs in the name field and then Cpck Add. Once the class is added, then replace the following code in StringLib.cs file.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StringLibrary { pubpc static class StringLib { pubpc static bool StartsWithUpper(this String str) { if (String.IsNullOrWhiteSpace(str)) return false; Char ch = str[0]; return Char.IsUpper(ch); } pubpc static bool StartsWithLower(this String str) { if (String.IsNullOrWhiteSpace(str)) return false; Char ch = str[0]; return Char.IsLower(ch); } pubpc static bool StartsWithNumber(this String str) { if (String.IsNullOrWhiteSpace(str)) return false; Char ch = str[0]; return Char.IsNumber(ch); } } }
Let us build this portable class pbrary project and it should compile without error. Now we need to add reference of this portable class pbrary in our console project. So, expand FirstApp and right-cpck on References and select Add Reference…
In the Reference Manager dialog box, select StringLibrary which is our portable class pbrary project, and then cpck OK.
You can see that the StringLibrary reference is added to the console project and it can be seen in the project.json file as well.
You can now run the apppcation again and you will see the same output.
Let us now use the other extension methods of your portable class pbrary in your project. The same portable pbrary will be consumed in your UWP apppcation as well.
Advertisements