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?
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.
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.
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.
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.
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).
I wouldn't worry too much about the code itself, but just try the following and see why Portable Class Libraries are useful.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With