Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Portable Class Library does not support System.IO, Why?

I created a portable class library to be used in my Monodroid project. But the problem is that I need System.IO library but unfortunately I couldn't add it.

I even tried to add it by Add Reference option but it was in vain.

Why this happened ? How shall I do this ?

like image 269
Seyed Hamed Shams Avatar asked Aug 23 '14 09:08

Seyed Hamed Shams


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 PCL in Visual Studio?

Portable Class Libraries (PCLs) are considered deprecated in the latest versions of Visual Studio. While you can still open, edit, and compile PCLs, for new projects it is recommended to use .


1 Answers

You can't use System.IO because it isn't a portable class library. System.IO makes calls which are specific to the OS it runs on (Windows), while the portable class library is ment to be cross-platform.

The solution to what you're looking for can be found here:

What should you do when you’re trying to write a portable library but you need some functionality that isn’t supported? You can’t call the API directly, and you can’t reference a library that does, because portable libraries can’t reference non-portable libraries. The solution is to create an abstraction in your portable library that provides the functionality you need, and to implement that abstraction for each platform your portable library targets. For example, if you need to save and load text files, you might use an interface like this:

public interface IFileStorage 
{
    Task SaveFileAsync(string filename, string contents);
    Task<String> LoadFileAsync(string filename); 
} 

It’s a good idea to include only the functionality you need in the abstraction. In this example, the interface doesn’t abstract general file system concepts such as streams, folders, or enumerating files. This makes the abstraction more portable and easier to implement. The methods return Tasks so that the implementation for Windows Store apps can call the WinRT file IO APIs, which are async.

Creating an abstraction allows portable libraries to call into non-portable code, and this pattern is applicable almost any time you need to access non-portable functionality from a portable library. Of course, you need some way for the portable code to get a reference to an implementation of the abstraction. How you do that can depend on whether you are writing a cross platform app or a general purpose reusable library.

like image 129
Yuval Itzchakov Avatar answered Sep 20 '22 15:09

Yuval Itzchakov