I am trying to define a property that returns a pointer to a generic type argument like so:
public class MemWrapper where T: struct
{
readonly I
Generics and pointers don't work well together, but this is actually a perfect fit for "ref return":
public class MemWrapper<T> where T : struct
{
readonly IntPtr pointerToUnmanagedHeapMem;
// ... do some memory management also ...
public unsafe ref T Ptr
{
get { return ref Unsafe.AsRef<T>(pointerToUnmanagedHeapMem.ToPointer()); }
}
}
Alternative Ptr
syntax:
public unsafe ref T Ptr => ref Unsafe.AsRef<T>(pointerToUnmanagedHeapMem.ToPointer());
Note that this requires recent versions of Unsafe
; here I'm using:
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="4.4.0" />
Note that you now don't need unsafe
in the consuming code - just the one property that touches pointerToUnmanagedHeapMem
.
Consuming code:
var wrapper = ... // some MemWrapper<T>
ref Foo foo = ref wrapper.Ptr;
Console.WriteLine(foo.SomeProperty); // not foo->SomeProperty
SomeInnerMethod(ref foo); // pass down to other ref Foo methods
no unmanaged pointers; the code is now perfectly "safe" outside of .Ptr
.
Note: if you need to talk about multiple consecutive items: Span<T>
/Memory<T>
are your friends.