Reputation: 15282
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?
Upvotes: 22
Views: 11855
Reputation: 28596
I have created a simple youtube video on What is portable class library , you can see it from here http://www.youtube.com/watch?v=DcBfjdDHlxo
But let me answer in more detail here.The whole point of creating a class library project is reusability. Now we want this reusability not only within a .NET application, not across .NET applications but across different types of .NET applications. Now different types of .NET application means WPF, Windows, Silver light, Windows phone etc.
Now each one of these application types are different have bit different .NET framework.
For example in silver light application if you try to reference a simple “Class project” you would end with a below error. That’s where portable class libraries are useful. By creating a portable class we can reference it in any kind of .NET project types.
Upvotes: 3
Reputation: 16744
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).
Upvotes: 22
Reputation: 32505
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/
Upvotes: 5
Reputation: 2858
What does the portable class library actually solve?
Eventually it will solve a LOT! (Like Java but in this century)
Is there any code that would compile in a Portable Class Library, that would not function correctly if that exact same code was in a .NET Library?
By Principle I think Not , isn't the other way round ?
But I think, will all due respect, you might actually be missing the point
Think about copy/paste code , and Project cloning , linking code , conditional compiler directives ,( even T4 templates?) and the final result is reference DLL Hell
and unneeded complexity, boilerplate , verbosity ( Not very elegant right ?)
Aren't they workarounds to binary incompatibility ?
Its very early But I think its an awesome direction, mostly when you see Xamarin and Mono are working in supporting pcl (they already do partially) ,I think its the only thing it could save as all from Html and Javascript frenzy or Code Generators. (long live TypeSafe!)
PCL could also free up the c# army from the Windows boxes
(but all this is just my opinion)
Upvotes: 2
Reputation: 15971
There are some API differences in the Portable Class Library that concerns .NET Framework 4, when you are developing in Visual Studio 2012. Primarily, this concerns a few properties and methods in the System.Net
and System.Xml
namespaces. For detailed information, see this MSDN page.
Upvotes: 1