DotNext by .NET Foundation and Contributors

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

 BoxedValue<T>

public sealed class BoxedValue<T> where T : struct
Represents a typed representation of the boxed value type.
using System; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; namespace DotNext.Runtime { public sealed class BoxedValue<T> where T : struct { [DebuggerBrowsable(DebuggerBrowsableState.Never)] public ref T Value { get { return ref Unsafe.Unbox<T>((object)this); } } [ExcludeFromCodeCoverage] private BoxedValue() { throw new NotImplementedException(); } [System.Runtime.CompilerServices.NullableContext(2)] [return: System.Runtime.CompilerServices.Nullable(new byte[] { 2, 0 })] [return: NotNullIfNotNull("boxedValue")] public static BoxedValue<T> GetTypedReference(object boxedValue) { if (boxedValue == null) return null; if (!(boxedValue is T)) throw new ArgumentException(ExceptionMessages.BoxedValueTypeExpected<T>(), "boxedValue"); return Unsafe.As<BoxedValue<T>>(boxedValue); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] public static BoxedValue<T> Box(T value) { return Unsafe.As<BoxedValue<T>>((object)value); } [return: System.Runtime.CompilerServices.Nullable(new byte[] { 2, 0 })] [return: NotNullIfNotNull("value")] public static BoxedValue<T> TryBox([In] [IsReadOnly] ref T? value) { return Unsafe.As<BoxedValue<T>>((object)value); } public static implicit operator T([System.Runtime.CompilerServices.Nullable(new byte[] { 1, 0 })] BoxedValue<T> boxedValue) { return boxedValue.Value; } public static explicit operator BoxedValue<T>(T value) { return Box(value); } public static explicit operator BoxedValue<T>([In] [IsReadOnly] ref T? value) { return TryBox(ref value); } } }