Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the portable class library actually solve?

Tags:

I was wondering, what does the PCL actually solve? If all it does is limit me to what types are cross-platform, then why didn't Microsoft just make this as a feature in a standard .NET library through the IDE?

Basically, I can easily compile a .NET library containing some POCO objects and reference that DLL in my Silverlight, WPF, and Windows Store app without needing to recompile or having any issues. Is there any hard examples of code that works in the PCL that would not work in a standard .NET library?


Oh, and I know that obviously there are some things that would work in the standard .NET library, I'm not concerned about that... I guess my question is this:

Is there any code that would compile in a Portable Class Library, that would not function correclty if that exact same code was in a .NET Library?

like image 469
michael Avatar asked May 02 '13 12:05

michael


People also ask

What is portable class library?

The Portable Class Library project enables you to write and build managed assemblies 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.

What is the use of class library?

In object-oriented programming , a class library is a collection of prewritten class es or coded templates, any of which can be specified and used by a programmer when developing an application program.

What is PCL project?

PCL projects target specific profiles that support a known set of BCL classes/features. However, the down side to PCL is that they often require extra architectural effort to separate profile specific code into their own libraries.

What is the difference between the portable class library and shared projects?

The difference between a shared project and a class library is that the latter is compiled and the unit of reuse is the assembly. Whereas with the former, the unit of reuse is the source code, and the shared code is incorporated into each assembly that references the shared project.


2 Answers

Two things:

First, if your intention is to create a library that does work on multiple platforms, you don't want to find out at runtime that you accidentally used an API that wasn't available on all platforms (via a TypeLoadException or MissingMethodException or something). So Portable Class Libraries will only give you intellisense for APIs that are supported on all the platforms you're targeting, and you will get build errors for anything outside that set.

Second, if you create a .NET Framework library that only uses APIs that are available on all platforms, the DLL created still will not work on other platforms (ie Windows Phone and Silverlight) without compiling it as a library for those platforms. It sounds like you expect that it would, and that is a reasonable expectation but has not been true in the past. Portable Class Libraries are the way we are making that work (and in fact it is the case that if you create a class library for Windows Store apps and only use APIs available for the .NET Framework and WP8, the resulting binary would work on both of those platforms unchanged).

like image 183
Daniel Plaisted Avatar answered Nov 02 '22 21:11

Daniel Plaisted


I wouldn't worry too much about the code itself, but just try the following and see why Portable Class Libraries are useful.

  1. Create a standard .NET 4.5 Class Library (it can be empty/contain no classes).
  2. Create a Silverlight 5 application.
  3. Attempt to reference the .NET 4.5 Class Library from your Silverlight 5 application.

You'll get the following error message:

You can't add a reference to Demo.Utils.dll as it was not built against the Silverlight runtime. Silverlight projects will only work with Silverlight assemblies.

That, in itself, is just a taste as to why Portable Class Libraries are so wonderful. The tough part is trying to adapt code to be platform agnostic, which sounds easy, but isn't as easy as you think. Trust me, when you first try to read a file from a folder and realize that the PCL doesn't allow FileInfo you'll be frustrated, but the guidance from Microsoft is to abstract the platform dependencies away and inject a class that implements an interface that actually takes care of it. In fact, here is a nice article on that.


Another useful article to look over goes over a bit of the inner workings of the way PCLs are set up. This article will help you understand why PCLs are able to target multiple platforms. https://www.simple-talk.com/blogs/2013/04/19/inside-portable-class-libraries/

like image 22
myermian Avatar answered Nov 02 '22 21:11

myermian