Introduction to Portable Class Library in .NET Framework 4.5

Portable Class Library, otherwise called PCL, comes with .NET framework 4.5 RC and a separate project template is available on Visual Studio 2012 RC. The portable class library can also be installed on top of Visual Studio 2010. It can be downloaded from here.

Portable Class Library

If you are developer working on applications targeting multiple platforms you might have to create duplicates of underlying class libraries targeting the specific platforms, even if they contain the same functionality or logics or algorithms. In .NET framework 4.5 RC, the Portable Class Library has been introduced to solve exactly that problem. By choosing the Portable Class Library template in Visual Studio 2012 RC you can create a single class library to hold the common business logic and share it across applications targeting different platforms like .Net Framework, Windows Phone, Metro style apps, Silverlight and Xbox. Fig 1.0 shows the PCL project template in Visual Studio 2012 RC.

PCL project template in Visual Studio 2012 RC
Fig 1.0: PCL project template in Visual Studio 2012 RC

Selecting the Portable Class Library template will prompt the developer to select different platforms and versions that the library should be supported on. Fig 1.1 shows the platform selection window.

Platform selection window
Fig 1.1: Platform selection window

How it Works?

How ca a single portable class library can be shared across multiple platforms? How will it be compatible? Here is the answer; when you create a Portable Class Library then Visual Studio will take care of importing only the supported set of base class libraries that are compatible with the selected platform versions. Even in the referenced base class libraries, only the supported methods or types will be exposed through intellisense. This set of libraries is wrapped under the project references in the solution explorer as .NET Portable Subset.

.NET Portable Subset
Fig 2.0: .NET Portable Subset

You can use the Visual Studio object explorer to dig through the list of assemblies and the class that they expose.

Visual Studio object explorer
Fig 2.1: Visual Studio object explorer

Even when you explicitly try to perform an Add reference, only the class libraries supporting the selected platform versions will be available.

Only the class libraries supporting the selected platform versions will be available
Fig 2.2: Only the class libraries supporting the selected platform versions will be available

Limitations and Prerequisites

Some limitations or prerequisites of Potable Class Library are:

1. Only a subset of the base class libraries supporting the selected platform versions will be included in the project reference.

2. Below is the list of platforms and versions that support Portable Class Library assemblies.

a. .Net Framework 4.0 or later

b.Silverlight 4 or later

c. Windows Phone 7 or later

d. Xbox 360

3. While deploying an application referencing a Portable Class Library, the target machine or device should have the specific platform version installed, as mentioned in the PCL.

Developing a Sample PCL and Consuming it

In this section we will create a sample Portable Class Library and consume it in a Windows forms application as well as in a Silverlight application.

In Visual Studio 2010, when I tried to add the reference of a normal class library in a Silverlight client application it displayed an error as shown in Fig 3.0.

Visual Studio 2010 error
Fig 3.0: Visual Studio 2010 error

Now in Visual Studio 2012 RC create a Portable Class Library named PortabilityDemoLibrary, add a Windows forms application and a Silverlight application to the same solution. Add a class and interface to the PCL. Below is the code.

namespace PortabilityDemoLibrary
{
    public interface IValidate
    {
        //Note that generics are allowed in PCL as all the selected platform versions support it.
        bool Validate(List<string> strList);
    }
}
namespace PortabilityDemoLibrary
{
    public class Validator : IValidate
    {
 
        #region IValidate Members
 
        public bool Validate(List<string> strList)
        {
            //Do some validation on the given string collection.
 
            return true;
        }
 
        #endregion
    }
}

Add the reference of the PCL to the Windows forms application and Silverlight application. Make use of the Validate function from the PCL in both applications. (Note that the adding reference to the Silverlight application goes through fine now).

Window Forms Application Call:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
           
 
            IValidate validator = new Validator();
            validator.Validate(DataFiles.GetStringDataCollection());
        }
    }
}

Silverlight Application Call:

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
 
            IValidate validator = new Validator();
            validator.Validate(DataFiles.GetStringDataCollection());
        }
    }
}

Another notable thing is that you can always go and update the platform support for a PCL project at any point in time. Based on the selected changes, Visual Studio will take care of refreshing the PCL project, like adding or removing the supported libraries in the Portable subset as necessary . I hope this article gave a good overview about the Portable Class Library in .Net framework 4.5 RC.

Happy reading!

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read