Skip to content

Commit 223436b

Browse files
committed
Complete TryUnBox
1 parent 527021c commit 223436b

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs

+16-1
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,22 @@ private unsafe void InternalSetValue(object? value, nint flattenedIndex)
693693
// value class or primitive type
694694

695695
ref byte offsetDataRef = ref Unsafe.Add(ref arrayDataRef, flattenedIndex * pMethodTable->ComponentSize);
696-
if (!RuntimeHelpers.TryUnboxInto(ref offsetDataRef, pElementMethodTable, value))
696+
if (CastHelpers.IsInstanceOfAny(pElementMethodTable, value) != null)
697+
{
698+
if (pElementMethodTable->IsNullable)
699+
{
700+
RuntimeHelpers.Unbox_Nullable(ref offsetDataRef, pElementMethodTable, value);
701+
}
702+
else if (pElementMethodTable->ContainsGCPointers)
703+
{
704+
Buffer.BulkMoveWithWriteBarrier(ref offsetDataRef, ref value.GetRawData(), pElementMethodTable->GetNumInstanceFieldBytes());
705+
}
706+
else
707+
{
708+
SpanHelpers.Memmove(ref offsetDataRef, ref value.GetRawData(), pElementMethodTable->GetNumInstanceFieldBytes());
709+
}
710+
}
711+
else
697712
{
698713
// Allow enum -> primitive conversion, disallow primitive -> enum conversion
699714
MethodTable* thSrc = RuntimeHelpers.GetMethodTable(value);

src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs

-3
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,6 @@ internal static unsafe bool ObjectHasComponentSize(object obj)
323323
[MethodImpl(MethodImplOptions.InternalCall)]
324324
internal static extern unsafe void Unbox_Nullable(ref byte destination, MethodTable* toTypeHnd, object? obj);
325325

326-
[MethodImpl(MethodImplOptions.InternalCall)]
327-
internal static extern unsafe bool TryUnboxInto(ref byte destination, MethodTable* toTypeHnd, object obj);
328-
329326
// Given an object reference, returns its MethodTable*.
330327
//
331328
// WARNING: The caller has to ensure that MethodTable* does not get unloaded. The most robust way

src/coreclr/vm/ecalllist.h

-1
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,6 @@ FCFuncEnd()
353353
#endif // FEATURE_COMINTEROP
354354

355355
FCFuncStart(gCastHelpers)
356-
FCFuncElement("CanCastTo_NoCacheLookup", ::IsInstanceOfAny_NoCacheLookup)
357356
FCFuncElement("IsInstanceOfAny_NoCacheLookup", ::IsInstanceOfAny_NoCacheLookup)
358357
FCFuncElement("ChkCastAny_NoCacheLookup", ::ChkCastAny_NoCacheLookup)
359358
FCFuncElement("Unbox_Helper", ::Unbox_Helper)

0 commit comments

Comments
 (0)