English 中文(简体)
.NET Core - Portable Class Library
  • 时间:2025-02-05

.NET Core - Portable Class Library


Previous Page Next Page  

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.

PCL

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.

Toopng

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.

Remove

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.

StringLibrary

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.

retarget

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.

Properties

Cpck on the Target .NET Platform Standard.

Target

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.

class pbrary

Let us now add a new class; for this, you need to right-cpck on project in Solution Explorer and select Add → Class...

add new 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…

References

In the Reference Manager dialog box, select StringLibrary which is our portable class pbrary project, and then cpck OK.

Library project

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.

Run Apppcation

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