using System; using System.Collections.Generic; using System.Runtime.InteropServices; using System.Text; namespace System { public static class BufferExtensions { public static ArraySegment GetArray(this Memory memory) { return ((ReadOnlyMemory)memory).GetArray(); } public static ArraySegment GetArray(this ReadOnlyMemory memory) { if (!MemoryMarshal.TryGetArray(memory, out var result)) { throw new InvalidOperationException("Buffer backed by array was expected"); } return result; } } }