5

我正在尝试从GZipStream Class构建一个示例。使用命令gmcs gzip.cs,我收到错误消息。gzip.cs 与 msdn 的来源相同。

看来我需要在编译时添加引用。少了什么东西?

gzip.cs(57,32): error CS1061: Type `System.IO.FileStream' does not contain a definition for `CopyTo' and no extension method `CopyTo' of type `System.IO.FileStream' could be found (are you missing a using directive or an assembly reference?)
/Library/Frameworks/Mono.framework/Versions/2.10.1/lib/mono/2.0/mscorlib.dll (Location of the symbol related to previous error)
gzip.cs(86,40): error CS1061: Type `System.IO.Compression.GZipStream' does not contain a definition for `CopyTo' and no extension method `CopyTo' of type `System.IO.Compression.GZipStream' could be found (are you missing a using directive or an assembly reference?)
/Library/Frameworks/Mono.framework/Versions/2.10.1/lib/mono/gac/System/2.0.0.0__b77a5c561934e089/System.dll (Location of the symbol related to previous error)
Compilation failed: 2 error(s), 0 warnings

解决了

为了使用 .NET 4 函数,我应该使用“dmcs”,而不是“gmcs”。

4

1 回答 1

7

Stream.CopyTo仅在 .NET 4 中出现 - 它可能还没有出现在 Mono 中(或者您可能需要更新的版本)。

不过,编写类似的扩展方法很容易:

public static class StreamExtensions
{
    public static void CopyTo(this Stream input, Stream output)
    {
        byte[] buffer = new byte[16 * 1024];
        int bytesRead;
        while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write(buffer, 0, bytesRead);
        }
    }
}
于 2011-05-11T15:52:44.780 回答