使用Mono与.NET GZipStream类

5

我正在尝试使用GZipStream类中的示例代码。在执行命令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

问题已解决

我应该使用 "dmcs" 而不是 "gmcs" 以便使用 .NET 4 功能。

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);
        }
    }
}

我正在使用最新的Mono 2.10.2版本。 - prosseek
@prosseek:那么很可能他们还没有将其包含进来。 - Jon Skeet
1
我发现我没有使用支持.NET 4.0的dmcs。它可以使用dmcs进行编译。感谢您的帮助。 - prosseek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接