将 StreamReader 转换为流

Converting a StreamReader to a Stream

protobuff-net 中的 Deserialize 方法接受一个 Stream 作为参数。在我过去使用 FileStream 并传递它并且它有效之前,但现在我必须使用 StreamReader 因为我正在为 windows store/phone 移植应用程序。我收到以下错误:

 error CS1503: Argument 1: cannot convert from 'System.IO.StreamReader' to 'System.IO.Stream'

这也是一个 unity3d 项目,我遇到的另一个错误是:

error CS4028: 'await' requires that the type 'Windows.Foundation.IAsyncOperation<Windows.Storage.StorageFolder>' have a suitable GetAwaiter method. Are you missing a using directive for 'System'?

这很奇怪,因为我将代码包装在 #if NETFX_CORE #endif 以下是我正在使用的包括:

#if NETFX_CORE

using System.IO;
using System.Threading.Tasks;
using Windows.Storage;
#endif

逻辑上你不能 - StreamReader 用于 text 数据,而 protobuf 数据是 binary 数据。目前尚不清楚您从哪里获得 StreamReader,但您应该寻找专为二进制数据设计的 API。

实际上,您可能能够使用StreamReader.BaseStream获取底层流,但您不应该创建StreamReader来无论如何都从非文本数据中读取...并构建 reader 可能会尝试从流中读取文本数据并在找不到时抱怨...