Portable Class Library even better in .NET 4.5

Visual Studio 2012 makes Cross-Platform development even easier. It comes with a feature called Portable Class Library (PCL). This feature was available in Visual Studio 2010 as well, but it required an additional install as against being out-of-the-box for 2012. It’s also worth noting that PCL is available only for Pro and above versions of 2012. So it’s not available with the Express edition of Visual Studio 2012.

Let’s get started. In Visual Studio 2012 you can see a template called Portable Class Library.


Once you create a project with this, it pops up the window:


Note that even if you uncheck any of these, say .NET Frameworks 4.5, it still keeps the library compatible with the .net core. So I’m not sure what the checkboxes actually mean.

Once the project has been created, you can see your selections in the project properties.


I have added a file StringFormatter with the GetMessedUpCase method to my library.

   1:  using System.Text;
   3:  namespace PortableClassLibraryDemo
   4:  {
   5:      public class StringFormatter
   6:      {
   7:          public string GetMessedUpCase(string input)
   8:          {
   9:              StringBuilder messedUpCaseBuilder = new StringBuilder();
  11:              int i = 0;
  12:              foreach (char letter in input)
  13:              {
  14:                  if (i++ % 2 == 0)
  15:                  {
  16:                      messedUpCaseBuilder.Append(letter.ToString().ToLower());
  17:                  }
  18:                  else
  19:                  {
  20:                      messedUpCaseBuilder.Append(letter.ToString().ToUpper());
  21:                  }
  22:              }
  24:              return messedUpCaseBuilder.ToString();
  25:          }
  26:      }
  27:  }

As you can see, this is, for all programming purposes, your regular class library project. There’s nothing new / different about this project.

Now I add a good ol’ Console application and add the PortableClassLibraryDemo as a reference. I call the GetMessedUpCase method just as I would with any other method from any referenced library.

   1:  static void Main(string[] args)
   2:  {
   3:      string test = "THIS IS MESSED UP CASE";
   5:      StringFormatter stringFormatter = new StringFormatter();
   7:      Console.WriteLine(stringFormatter.GetMessedUpCase(test));
   8:  }

The output is as shown below.


Now I can create a Windows 8 Metro Style C#/XAML App and add the PortableClassLibraryDemo  assembly as a reference to it.

My XAML is pretty simple, for the sake of the demo.

   1:  <StackPanel Margin="30, 40, 0, 0" Width="300" HorizontalAlignment="Left">
   2:      <TextBox x:Name="inputTextBox" />
   3:      <Button x:Name="Convert" Content="Get messed-up case" Click="Convert_Click_1"/>
   4:      <TextBlock x:Name="outputBlock" FontSize="20"/>
   5:  </StackPanel>

The code behind for the button click is also pretty straight forward.

   1:  private void Convert_Click_1(object sender, RoutedEventArgs e)
   2:  {
   3:      // using PortableClassLibraryDemo; added at the top
   4:      StringFormatter stringFormatter = new StringFormatter();
   5:      outputBlock.Text = stringFormatter.GetMessedUpCase(inputTextBox.Text);
   6:  }

When I execute the app, the output again looks as expected.


Cool huh?

Now let’s go a little deeper to see what’s actually happening. When I open the PortableClassLibraryDemo.dll in ILDASM or Reflector, what I see is the TargetFrameworkAttribute attribute added to the assembly.

.custom instance void [mscorlib]System.Runtime.Versioning.TargetFrameworkAttribute::.ctor(string) = { string('.NETPortable,Version=v4.0,Profile=Profile4') FrameworkDisplayName=string('.NET Portable Subset') }

So what does the string that is passed to the attribute mean? Stephen breaks it down very clearly on his blog site (scroll down to the table below). The Version 4 and Profile 4, maps to the below frameworks:

.NETCore,Version=v4.5,Profile=* (.NET for Metro style apps)
.NETFramework,Version=v4.5,Profile=* (.NET Framework)
Silverlight,Version=v4.0 (Silverlight)
Silverlight,Version=v4.0,Profile=WindowsPhone* (Windows Phone)

(in short, all the things we selected in the ‘Add Portable Class Library’ window).

Why would you need this project template? You can create a shared view model that you can port to multiple frameworks. You can also have use this for common business logic layer.

I know I’m going to use it for a couple of my projects!!


Comments have been disabled for this content.