Early Preview

This is currently very much a preview. Please feel free to try things out, but don't be upset if anything is not yet working. Feedback is welcome over on our GitHub Dicussions page.

class System.​Runtime.​InteropServices.​SafeBuffer

Assembly: System.Runtime

Inheritance: object → ConstrainedExecution.​CriticalFinalizerObjectSafeHandleMicrosoft.​Win32.​SafeHandles.​SafeHandleZeroOrMinusOneIsInvalid → SafeBuffer

Implemented Interfaces

Provides a controlled memory buffer that can be used for reading and writing. Attempts to access memory outside the controlled buffer (underruns and overruns) raise exceptions.

Fields and Constants

protected nint
handle
Specifies the handle to be wrapped.

Properties

public ulong
ByteLength
Gets the size of the buffer, in bytes.
public bool
IsInvalid
Gets a value that indicates whether the handle is invalid.
public bool
IsClosed
Gets a value indicating whether the handle is closed.

Methods

public void
AcquirePointer​(Byte*& pointer)
public void
Initialize​(uint numElements, uint sizeOfEachElement)
Specifies the allocation size of the memory buffer by using the specified number of elements and element size. You must call this method before you use the <see cref="T:System.Runtime.InteropServices.SafeBuffer" /> instance.
numElements The number of elements in the buffer.
sizeOfEachElement The size of each element in the buffer.
public void
Initialize​(ulong numBytes)
Defines the allocation size of the memory region in bytes. You must call this method before you use the <see cref="T:System.Runtime.InteropServices.SafeBuffer" /> instance.
numBytes The number of bytes in the buffer.
public void
Initialize​(uint numElements)
Defines the allocation size of the memory region by specifying the number of value types. You must call this method before you use the <see cref="T:System.Runtime.InteropServices.SafeBuffer" /> instance.
numElements The number of elements of the value type to allocate memory for.
public void
ReadArray​(ulong byteOffset, T[] array, int index, int count)
Reads the specified number of value types from memory starting at the offset, and writes them into an array starting at the index.
byteOffset The location from which to start reading.
array The output array to write to.
index The location in the output array to begin writing to.
count The number of value types to read from the input array and to write to the output array.
public void
ReadSpan​(ulong byteOffset, Span<​T> buffer)
Reads value types from memory starting at the offset, and writes them into a span. The number of value types that will be read is determined by the length of the span.
byteOffset The location from which to start reading.
buffer The output span to write to.
public T
Read​(ulong byteOffset)
Reads a value type from memory at the specified offset.
Returns The value type that was read from memory.
byteOffset The location from which to read the value type. You may have to consider alignment issues.
public void
ReleasePointer​()
Releases a pointer that was obtained by the <see cref="M:System.Runtime.InteropServices.SafeBuffer.AcquirePointer(System.Byte*@)" /> method.
public void
WriteArray​(ulong byteOffset, T[] array, int index, int count)
Writes the specified number of value types to a memory location by reading bytes starting from the specified location in the input array.
byteOffset The location in memory to write to.
array The input array.
index The offset in the array to start reading from.
count The number of value types to write.
public void
WriteSpan​(ulong byteOffset, ReadOnlySpan<​T> data)
Writes the value types from a read-only span to a memory location.
byteOffset The location in memory to write to.
data The input span.
public void
Write​(ulong byteOffset, T value)
Writes a value type to memory at the given location.
byteOffset The location at which to start writing. You may have to consider alignment issues.
value The value to write.
public void
Close​()
Inherited from SafeHandle
Marks the handle for releasing and freeing resources.
public void
DangerousAddRef​(Boolean& success)
Inherited from SafeHandle
public nint
DangerousGetHandle​()
Inherited from SafeHandle
Returns the value of the <see cref="F:System.Runtime.InteropServices.SafeHandle.handle" /> field.
Returns An <see cref="T:System.IntPtr" /> representing the value of the <see cref="F:System.Runtime.InteropServices.SafeHandle.handle" /> field. If the handle has been marked invalid with <see cref="M:System.Runtime.InteropServices.SafeHandle.SetHandleAsInvalid" /> , this method still returns the original handle value, which can be a stale value.
public void
DangerousRelease​()
Inherited from SafeHandle
Manually decrements the reference counter on a <see cref="T:System.Runtime.InteropServices.SafeHandle" /> instance.
public void
Dispose​()
Inherited from SafeHandle
Releases all resources used by the <see cref="T:System.Runtime.InteropServices.SafeHandle" /> class.
protected void
Dispose​(bool disposing)
Inherited from SafeHandle
Releases the unmanaged resources used by the <see cref="T:System.Runtime.InteropServices.SafeHandle" /> class specifying whether to perform a normal dispose operation.
disposing <see langword="true" /> for a normal dispose operation; <see langword="false" /> to finalize the handle.
protected void
Finalize​()
Inherited from SafeHandle
Frees all resources associated with the handle.
protected bool
ReleaseHandle​()
Inherited from SafeHandle
When overridden in a derived class, executes the code required to free the handle.
Returns <see langword="true" /> if the handle is released successfully; otherwise, in the event of a catastrophic failure, <see langword="false" /> . In this case, it generates a releaseHandleFailed Managed Debugging Assistant.
protected void
SetHandle​(nint handle)
Inherited from SafeHandle
Sets the handle to the specified pre-existing handle.
handle The pre-existing handle to use.
public void
SetHandleAsInvalid​()
Inherited from SafeHandle
Marks a handle as no longer used.
public bool
Equals​(object obj)
Inherited from object
Determines whether the specified object is equal to the current object.
Returns <see langword="true" /> if the specified object is equal to the current object; otherwise, <see langword="false" /> .
obj The object to compare with the current object.
public int
GetHashCode​()
Inherited from object
Serves as the default hash function.
Returns A hash code for the current object.
public Type
GetType​()
Inherited from object
Gets the <see cref="T:System.Type" /> of the current instance.
Returns The exact runtime type of the current instance.
protected object
MemberwiseClone​()
Inherited from object
Creates a shallow copy of the current <see cref="T:System.Object" /> .
Returns A shallow copy of the current <see cref="T:System.Object" /> .
public string
ToString​()
Inherited from object
Returns a string that represents the current object.
Returns A string that represents the current object.