What is the advantage of using portable class libr

2019-01-14 23:02发布

问题:

Is anybody explain to me what is the advantage of using portable class libraries instead of using "Add as Link"?

Thanks

回答1:

Disadvantages of linked files:

  • Add as link can be hard to maintain, especially as you scale to multiple projects and many source files. Tooling (such as Project Linker for Visual Studio 2010, or holding ALT while dragging in Visual Studio 2012) can make this easier.
  • Refactoring tools don't work with linked files. For example, if you rename a class or method in a linked file, refactoring tools won't update references to other linked copies of that API.
  • When editing code in a linked file, intellisense may show you APIs that are not available on all the platforms the file is linked into.
  • Visual Studio will give you a message box saying "This document is opened by another project" when you try to open a linked file already opened by another project.
  • You end up with a separate DLL for each platform. If you are creating a reusable library you would like to share with others, it might be easier to distribute if there was just one version of it, not a separate one for each platform.

Disadvantages of Portable Class Libraries:

  • You are limited to APIs that are available on all the platforms you are targeting. You can't use conditional compilation (#if statements) to get around differences between the platforms
  • It can be hard to figure out what APIs are supported on a given combination of platforms. Here is a spreadsheet which can help with this: Portable Class Library API List

For some guidance on how to take advantage of Portable Class Libraries, see the following:

  • How to Make Portable Class Libraries Work for You
  • Create Cross-platform Apps with Portable Class Libraries (BUILD session)

While I'm partial to Portable Class Libraries (as a member of the PCL team), linked files are also an entirely valid way of sharing code and if you don't run into or don't mind the drawbacks, then go ahead and use linked source files. I mostly use Portable Class Libraries, but I still use linked source files when PCLs don't fit.