DotNext by .NET Foundation and Contributors

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

 BoxedValue<T>

public abstract 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 abstract 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(); } [NullableContext(2)] [return: 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: Nullable(new byte[] { 1, 0 })] public static BoxedValue<T> Box(T value) { return Unsafe.As<BoxedValue<T>>((object)value); } [return: 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([Nullable(new byte[] { 1, 0 })] BoxedValue<T> boxedValue) { return boxedValue.Value; } public static explicit operator BoxedValue<T>(T value) { return Box(value); } public static implicit operator ValueType([Nullable(new byte[] { 2, 0 })] BoxedValue<T> boxedValue) { return Unsafe.As<ValueType>((object)boxedValue); } [return: Nullable(new byte[] { 1, 0 })] public BoxedValue<T> Copy() { return Unsafe.As<BoxedValue<T>>(MemberwiseClone()); } public static explicit operator BoxedValue<T>([In] [IsReadOnly] ref T? value) { return TryBox(ref value); } [NullableContext(2)] public abstract override bool Equals(object obj); public abstract override int GetHashCode(); [NullableContext(1)] public abstract override string ToString(); } }