【问题标题】:Operations on ReadOnlySequence<byte>ReadOnlySequence<byte> 上的操作
【发布时间】:2019-12-27 09:01:56
【问题描述】:

我正在对 System.IO.Pipelines 进行一些试验,但无法找到转换或使用返回的 ReadOnlySequence 对象的最佳方法。

我能够用它做任何事情的唯一方法是将它转换为一个数组,然后执行普通的字节函数,例如:

        byte[] byteArray = new byte[]{ 0x01, 0x02, 0x03, 0x50, 0x99 };
        ReadOnlySequence<byte> buffer = new ReadOnlySequence<byte>(byteArray);

对 Buffer 进行操作:

        var theArray = buffer.ToArray();
        // perform normal byte array operations

如果不转换为数组,我不知道该怎么做:

        foreach(byte theByte in buffer)
            // do something with the byte

测试缓冲区中的特定字节

        if(buffer[0] == 0x11)
            ...

将字节缓冲区复制到结构

        Marshal.Copy(

是通过将 ReadOnlySequence 转回数组来访问其内容的唯一方法吗?

【问题讨论】:

标签: c# system.io.pipelines


【解决方案1】:

您可以像这样在ReadOnlySequence 上使用foreach

foreach (var item in buffer)
{
    if (item.Span[0] == 0x01)
    {
        Console.WriteLine("hello");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 1970-01-01
    • 2013-03-01
    相关资源
    最近更新 更多