11using System . Diagnostics ;
22using System . Diagnostics . CodeAnalysis ;
33using System . Runtime . CompilerServices ;
4- using static InlineIL . IL ;
5- using static InlineIL . IL . Emit ;
6- using static InlineIL . MethodRef ;
7- using static InlineIL . TypeRef ;
84
95namespace DotNext . Runtime ;
106
@@ -29,6 +25,13 @@ namespace DotNext.Runtime;
2925 where T : struct
3026{
3127 private T value ;
28+
29+ static BoxedValue ( )
30+ {
31+ // AOT: instantiate the class to make instance members visible to AOT
32+ var boxed = new BoxedValue < T > ( ) ;
33+ GC . KeepAlive ( boxed ) ;
34+ }
3235
3336 [ ExcludeFromCodeCoverage ]
3437 private BoxedValue ( )
@@ -63,22 +66,15 @@ private BoxedValue()
6366 /// </summary>
6467 /// <param name="value">The value to be boxed.</param>
6568 /// <returns>A boxed representation of the value.</returns>
66- public static BoxedValue < T > Box ( T value )
67- {
68- Push ( value ) ;
69- Box < T > ( ) ;
70- Dup ( ) ;
71- Call ( Constructor ( Type < BoxedValue < T > > ( ) ) ) ;
72- return Return < BoxedValue < T > > ( ) ;
73- }
69+ public static BoxedValue < T > Box ( T value ) => Unsafe . As < BoxedValue < T > > ( value ) ;
7470
7571 /// <summary>
7672 /// Boxes nullable value type to an object.
7773 /// </summary>
7874 /// <param name="value">The value to be boxed.</param>
7975 /// <returns>A boxed representation of the value.</returns>
8076 [ return : NotNullIfNotNull ( nameof ( value ) ) ]
81- public static BoxedValue < T > ? TryBox ( in T ? value ) => value . HasValue ? Box ( value . GetValueOrDefault ( ) ) : null ;
77+ public static BoxedValue < T > ? TryBox ( in T ? value ) => Unsafe . As < BoxedValue < T > ? > ( value ) ;
8278
8379 /// <summary>
8480 /// Unboxes the value.
0 commit comments