From c2640876c3907cabdd4d856dbf6c1ded4c2e4ba2 Mon Sep 17 00:00:00 2001 From: dahall Date: Tue, 10 Nov 2020 21:35:58 -0700 Subject: [PATCH] Added IntPtr.AsRef extension method --- Core/Extensions/InteropExtensions.cs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Core/Extensions/InteropExtensions.cs b/Core/Extensions/InteropExtensions.cs index c9f85516..b9b03611 100644 --- a/Core/Extensions/InteropExtensions.cs +++ b/Core/Extensions/InteropExtensions.cs @@ -42,6 +42,15 @@ namespace Vanara.Extensions return new ReadOnlySpan((ptr + prefixBytes).ToPointer(), length); } + /// Gets a reference to a structure based on this allocated memory. + /// The type of items in the . + /// A pointer to the starting address of a specified number of elements in memory. + /// Bytes to skip before starting the span. + /// If known, the total number of bytes allocated to the native memory in . + /// A referenced structure. + public static ref T AsRef(this IntPtr ptr, int prefixBytes = 0, SizeT allocatedBytes = default) => + ref MemoryMarshal.GetReference(AsSpan(ptr, 1, prefixBytes, allocatedBytes)); + /// Returns the pointer as a . /// The type of items in the . /// A pointer to the starting address of a specified number of elements in memory.