DotNext by .NET Foundation and Contributors

<PackageReference Include="DotNext" Version="4.12.4" />

 Reference

public static class Reference
Provides factory methods for creating references.
using System; using System.Runtime.CompilerServices; namespace DotNext.Runtime { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public static class Reference { [System.Runtime.CompilerServices.NullableContext(2)] [CLSCompliant(false)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public unsafe static Reference<TValue> Create<TValue>([System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] IntPtr getter) { if (getter == (IntPtr)(void*)null) throw new ArgumentNullException("getter"); return new Reference<TValue>(getter); } [CLSCompliant(false)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public unsafe static Reference<TValue> Create<TOwner, [System.Runtime.CompilerServices.Nullable(2)] TValue>(TOwner owner, [System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1, 1 })] IntPtr getter) where TOwner : class { ArgumentNullException.ThrowIfNull((object)owner, "owner"); if (getter == (IntPtr)(void*)null) throw new ArgumentNullException("getter"); Array array = owner as Array; if (array != null && array.Rank == 1) throw new ArgumentException(ExceptionMessages.ObjectMustNotBeArray, "owner"); return Reference<TValue>.Create(owner, getter); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public unsafe static Reference<TValue> Field<[System.Runtime.CompilerServices.Nullable(2)] TValue>(StrongBox<TValue> box) { return Create<StrongBox<TValue>, TValue>(box, (IntPtr)(void*)); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public static Reference<TValue> ArrayElement<[System.Runtime.CompilerServices.Nullable(2)] TValue>(TValue[] array, [System.Runtime.CompilerServices.NativeInteger] IntPtr index) { ArgumentNullException.ThrowIfNull((object)array, "array"); if ((ulong)(long)index >= (ulong)(long)Intrinsics.GetLength(array)) throw new ArgumentOutOfRangeException("index"); return new Reference<TValue>(array, index); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 0, 1 })] public unsafe static Reference<TValue> Allocate<[System.Runtime.CompilerServices.Nullable(2)] TValue>(TValue value) { return Reference<TValue>.Create(new StrongBox<TValue>(value), (IntPtr)(void*)); } [System.Runtime.CompilerServices.NullableContext(0)] public unsafe static Reference<TValue> Unbox<TValue>([System.Runtime.CompilerServices.Nullable(1)] object boxed) where TValue : struct { if (!(boxed is TValue)) throw new ArgumentException(ExceptionMessages.BoxedValueTypeExpected<TValue>(), "boxed"); return Reference<TValue>.Create(boxed, (IntPtr)(void*)); } [System.Runtime.CompilerServices.NullableContext(0)] [CLSCompliant(false)] public unsafe static Reference<TValue> FromPointer<[System.Runtime.CompilerServices.IsUnmanaged] TValue>(TValue* ptr) where TValue : struct { if (ptr == null) throw new ArgumentNullException("ptr"); return new Reference<TValue>((void*)ptr); } private static ref TValue GetValueRef<TValue>(StrongBox<TValue> box) { return ref box.Value; } } }