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

末鹿安然 提交于 2019-12-23 07:12:39

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/25460581/portable-class-library-does-not-support-system-io-why

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!