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!



Related Articles

Comments

  • clarisonic pro with the cadaver nail-brush is sterling an eye to me

    Posted by iouwanzi on 06/05/2013 11:48pm

    [url=http://www.miaclarisonicaustralia.org/]clarisonic australia[/url] Pour construire la nouvelle galerie frais, toute la peau du visage de ghd pas cher plus l’exemple ultime de conception et de style et de même modèle, Katy Perry a réinventé son contrôle à l’aide de décadence et de complexité, de tout l’aspect de fois. Juste pour le moment, Katy est immortalisée tout simplement parce que le blanc vif au sol dans l’intention habilité par la photographie Steve LaChapelle. Comme probablement vos fans de photographie préférée, tous le double d’équilibrage glamour et kitsch, une formation photographie numérique d’aujourd’hui, tout le monde présent passionnant et l’air chaud qui ont été similaires dans les modes impliquant Vanessa Paradis avec Depp Ashton, tout aussi toxicomanes substantiels impliquant 20-30 années à venir ! J’aime vraiment au sujet de n’importe qui ? [url=http://www.miaclarisonicaustralia.org/]clarisonic mia 2[/url] Notre coiffeur étranger encouragés plus tôt ghd Gold styler à mon avis, ainsi que maintenant nos U. s. salon de coiffure en outre leur préféré dans mon expérience – car il est ce que vos applications amant dans le salon, ainsi qu’à la maison (la dame s’avère en outre faire un copain, donc mon partenaire et j’ai confiance dans la jeune fille!). Votre nom de marque est en fait le meilleur et même fera un cheveux sentiment fantastique.Réveillé par le glamour unique dans les années folles, cutting edge styler généralement la méthode pour obtenir l’attraction en ajoutant une touche de paillettes. Tous l’écarlate s’engage dans seulement deux cas Craft Deco design et le style que vous pourriez choisir. [url=http://www.miaclarisonicaustralia.org/clarisonic-mia]clarisonic mia[/url] Pour construire la nouvelle galerie frais, toute la peau du visage de ghd pas cher plus l’exemple ultime de conception et de style et de même modèle, Katy Perry a réinventé son contrôle à l’aide de décadence et de complexité, de tout l’aspect de fois. Juste pour le moment, Katy est immortalisée tout simplement parce que le blanc vif au sol dans l’intention habilité par la photographie Steve LaChapelle. Comme probablement vos fans de photographie préférée, tous le double d’équilibrage glamour et kitsch, une formation photographie numérique d’aujourd’hui, tout le monde présent passionnant et l’air chaud qui ont été similaires dans les modes impliquant Vanessa Paradis avec Depp Ashton, tout aussi toxicomanes substantiels impliquant 20-30 années à venir ! J’aime vraiment au sujet de n’importe qui ?

    Reply
  • what about portability to Linux/iOS/Android?..

    Posted by Yuri G on 04/11/2013 11:36am

    There is much more expected from Microsoft to fill the gap in mobile and cyber development...

    Reply
  • Nice article

    Posted by Phani on 08/20/2012 06:29am

    Its very nice article and I like this feature very much.

    Reply
  • Mr

    Posted by Phani on 08/17/2012 07:03am

    Its a cool feature to be used and good thing it can be used from VS 2010. Its a nice idea to build a DLL to support multiple platoforms. Thank you for this article.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

  • It's not unusual for a company to use a variety of formal and informal file-sharing methods. Many methods are fraught with significant operational, financial, and legal risks in addition to other potentially negative business consequences. Strategic managed file transfer (MFT) software and services help connect the business "dots" between the various ad hoc and more systematic ways that files are moved. Read this white paper to learn how the right MFT solution, can bring order, structure, and, importantly, …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds