无法在Windows 10上创建新的ConcurrentQueue<byte[]>?

3
我正在尝试在Windows 10应用程序中使用HashLib库,但它抛出了一个未处理的异常(System.MethodAccessException):

'HashLib.Hash.TransformStream(System.IO.Stream, Int64)'方法尝试访问'System.Collections.Concurrent.ConcurrentQueue`1..ctor()'方法失败。

没有更多信息。抛出异常的确切行是在HashLib的源文件Hash.cs的第380行。
System.Collections.Concurrent.ConcurrentQueue<byte[]> queue = new System.Collections.Concurrent.ConcurrentQueue<byte[]>();

我找不到关于这个问题的线索在MSDN上。我只看到它甚至支持便携式类库,所以我认为它应该在普通的Windows 10应用中也能工作。完全相同的代码已经在WPF应用程序和Windows 8.1应用程序中成功使用和测试,没有任何问题。
1个回答

0
解决方法是将流转换为byte[],这样可以解决问题。
    public static string MakeHashForFile(Stream fileStream)
    {
        //HashResult hashResult = hashImplementation.ComputeStream(fileStream);
        byte[] bytes = GetBytesFromStream(fileStream);

        HashResult hashResult = hashImplementation.ComputeBytes(bytes);

        return hashResult.ToString().Replace("-", String.Empty).ToLowerInvariant();
    }

    private static byte[] GetBytesFromStream(Stream stream)
    {
        byte[] result;
        using (MemoryStream reader = new MemoryStream())
        {
            stream.CopyTo(reader);
            result = reader.ToArray();
        }
        return result;
    }

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