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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • A modern mobile IT strategy is no longer an option, it is an absolute business necessity. Today's most productive employees are not tied to a desk, an office, or a location. They are mobile. And your company's IT strategy has to be ready to support them with easy, reliable, 24/7 access to the business information they need, from anywhere in the world, across a broad range of communication devices. Here's how some of the nation's most progressive corporations are meeting the many needs of their mobile workers …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds