Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Add Unsafe.Unbox (#31133)
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub authored Jul 17, 2018
1 parent 1afc536 commit 64c6d9f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public unsafe static void InitBlockUnaligned(void* startAddress, byte value, uin
public static ref T Subtract<T>(ref T source, int elementOffset) { throw null; }
public unsafe static void* Subtract<T>(void* source, int elementOffset) { throw null; }
public static ref T Subtract<T>(ref T source, System.IntPtr elementOffset) { throw null; }
public static ref T Unbox<T>(object box) where T : struct { throw null; }
public unsafe static void Write<T>(void* destination, T value) { }
public unsafe static void WriteUnaligned<T>(void* destination, T value) { }
public static void WriteUnaligned<T>(ref byte destination, T value) { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,15 @@
ret
} // end of method Unsafe::As

.method public hidebysig static !!T& Unbox<valuetype .ctor ([CORE_ASSEMBLY]System.ValueType) T> (object 'box') cil managed aggressiveinlining
{
.custom instance void System.Runtime.Versioning.NonVersionableAttribute::.ctor() = ( 01 00 00 00 )
.maxstack 1
ldarg.0
unbox !!T
ret
} // end of method Unsafe::Unbox

.method public hidebysig static !!T& Add<T>(!!T& source, int32 elementOffset) cil managed aggressiveinlining
{
.custom instance void System.Runtime.Versioning.NonVersionableAttribute::.ctor() = ( 01 00 00 00 )
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions src/System.Runtime.CompilerServices.Unsafe/tests/UnsafeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,42 @@ public static unsafe void WriteUnaligned_Ptr_Struct()
Assert.Equal(123456789, actual.Int32);
Assert.Equal(3.42, actual.Double);
}

[Fact]
public static void Unbox_Int32()
{
object box = 42;

Assert.True(Unsafe.AreSame(ref Unsafe.Unbox<int>(box), ref Unsafe.Unbox<int>(box)));

Assert.Equal(42, (int)box);
Assert.Equal(42, Unsafe.Unbox<int>(box));

ref int value = ref Unsafe.Unbox<int>(box);
value = 84;
Assert.Equal(84, (int)box);
Assert.Equal(84, Unsafe.Unbox<int>(box));

Assert.Throws<InvalidCastException>(() => Unsafe.Unbox<Byte4>(box));
}

[Fact]
public static void Unbox_CustomValueType()
{
object box = new Int32Double();

Assert.Equal(0, ((Int32Double)box).Double);
Assert.Equal(0, ((Int32Double)box).Int32);

ref Int32Double value = ref Unsafe.Unbox<Int32Double>(box);
value.Double = 42;
value.Int32 = 84;

Assert.Equal(42, ((Int32Double)box).Double);
Assert.Equal(84, ((Int32Double)box).Int32);

Assert.Throws<InvalidCastException>(() => Unsafe.Unbox<bool>(box));
}
}

[StructLayout(LayoutKind.Explicit)]
Expand Down

0 comments on commit 64c6d9f

Please sign in to comment.