繁体   English   中英

转换清单 <Byte[]> 到字节[]

[英]Convert List<Byte[]> to Byte[]

我使用Gtk#,需要将List<Byte[]>转换为Byte[] 我在这里找到了此操作的示例,但这是.NET Framework的示例,当我尝试通过Gtk#进行操作时,出现了一些编译错误:

“类型System.Collections.Generic.List<byte[]>' does not contain a definition for SelectMany' System.Collections.Generic.List<byte[]>' does not contain a definition for ,找不到SelectMany' of type System.Collections.Generic.List'的扩展方法SelectMany' of type (是否缺少using指令或装配体参考?)(CS1061)”。

如何解决此错误,或者我可以使用其他Gtk#的其他转换方式?

确保:

  1. 您正在使用.NET Framework 3.5或更高版本。
  2. 您有对System.Core的引用。
  3. 您在代码文件的顶部具有“ using System.Linq”。

IEnumerable的扩展名是.net框架更高版本的一部分,Gtk#可能没有这些扩展名,或者它们可能是另一个名称。

一个简单的(不确定是否有更简单的方法)方法是计算数组所需的总大小并将其初始化为该大小。 然后遍历该列表,并通过跟踪数组有多远,将每个byte []中的数据放入新的byte []中。

private static Byte[] ConvertList(List<Byte[]> list)
    {
        int totalLength = 0;

        foreach (byte[] b in list)
        {
            totalLength += b.Length;
        }

        byte[] result = new byte[totalLength];

        int currentPosition = 0;

        foreach (byte[] byteArray in list)
        {
            foreach (byte b in byteArray)
            {
                result[currentPosition] = b;
                currentPosition++;
            }
        }

        return result;
    }
private static Byte[] ConvertList(List<Byte[]> list)
    {
        List<Byte> tmpList = new List<byte>();
        foreach (Byte[] byteArray in list)
            foreach (Byte singleByte in byteArray)
                tmpList.Add(singleByte);
        return tmpList.ToArray();
    }

几乎是其他代码示例的简短版本。 经过.Net Framework 2.0测试

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM