首页 > 解决方案 > 如何从 ReadOnlySpan 获取字节 []不调用 .ToArray?

问题描述

让我们假设类型为 的遗留 API public byte[] ProcessAndCreateNewArray(byte[] readonlyParam);。如果我有格式输入,var inputReadOnlyParam = new ReadOnlySpan<byte>那么不做任何其他事情,API 需要像这样调用

// Could the creation of new array by calling .ToArray be avoided?
ReadOnlySpan<byte> res = (ReadOnlySpan<byte>)ProcessAndCreateNewArray(inputReadOnlyParam.ToArray())

但是有什么方法可以避免.ToArray调用并传入 span 已经拥有的缓冲区?我试图看MemoryMarshal,但找不到任何东西。这可能吗?

标签: c#.net-core

解决方案


推荐阅读