diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/InvokeUtils.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/InvokeUtils.cs index 1d4007ac93a68a..88013f0ad58b0b 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/InvokeUtils.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/InvokeUtils.cs @@ -191,16 +191,26 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje break; case EETypeElementType.SByte: - - // Can only be sbyte here - sbyteValue = RuntimeHelpers.FastUnbox(srcObject); + switch (srcElementType) + { + case EETypeElementType.SByte: + sbyteValue = RuntimeHelpers.FastUnbox(srcObject); + break; + default: + goto Failure; + } rawDstValue = &sbyteValue; break; case EETypeElementType.Byte: - - // Can only be byte here - byteValue = RuntimeHelpers.FastUnbox(srcObject); + switch (srcElementType) + { + case EETypeElementType.Byte: + byteValue = RuntimeHelpers.FastUnbox(srcObject); + break; + default: + goto Failure; + } rawDstValue = &byteValue; break;