@@ -146,6 +146,8 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
146
146
147
147
Unsafe . SkipInit ( out dstObject ) ;
148
148
149
+ ref byte rawSrcValue = ref RuntimeHelpers . GetRawData ( srcObject ) ;
150
+
149
151
// This is the table of all supported widening conversions:
150
152
//
151
153
// Boolean (W = BOOL)
@@ -176,13 +178,13 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
176
178
switch ( srcElementType )
177
179
{
178
180
case EETypeElementType . Char :
179
- charValue = RuntimeHelpers . FastUnbox < char > ( srcObject ) ;
181
+ charValue = Unsafe . As < byte , char > ( ref rawSrcValue ) ;
180
182
break ;
181
183
case EETypeElementType . Byte :
182
- charValue = ( char ) RuntimeHelpers . FastUnbox < byte > ( srcObject ) ;
184
+ charValue = ( char ) rawSrcValue ;
183
185
break ;
184
186
case EETypeElementType . UInt16 :
185
- charValue = ( char ) RuntimeHelpers . FastUnbox < ushort > ( srcObject ) ;
187
+ charValue = ( char ) Unsafe . As < byte , ushort > ( ref rawSrcValue ) ;
186
188
break ;
187
189
default :
188
190
goto Failure ;
@@ -194,7 +196,7 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
194
196
switch ( srcElementType )
195
197
{
196
198
case EETypeElementType . SByte :
197
- sbyteValue = RuntimeHelpers . FastUnbox < sbyte > ( srcObject ) ;
199
+ sbyteValue = Unsafe . As < byte , sbyte > ( ref rawSrcValue ) ;
198
200
break ;
199
201
default :
200
202
goto Failure ;
@@ -206,7 +208,7 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
206
208
switch ( srcElementType )
207
209
{
208
210
case EETypeElementType . Byte :
209
- byteValue = RuntimeHelpers . FastUnbox < byte > ( srcObject ) ;
211
+ byteValue = rawSrcValue ;
210
212
break ;
211
213
default :
212
214
goto Failure ;
@@ -218,13 +220,13 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
218
220
switch ( srcElementType )
219
221
{
220
222
case EETypeElementType . SByte :
221
- shortValue = ( short ) RuntimeHelpers . FastUnbox < sbyte > ( srcObject ) ;
223
+ shortValue = ( short ) Unsafe . As < byte , sbyte > ( ref rawSrcValue ) ;
222
224
break ;
223
225
case EETypeElementType . Byte :
224
- shortValue = ( short ) RuntimeHelpers . FastUnbox < byte > ( srcObject ) ;
226
+ shortValue = ( short ) rawSrcValue ;
225
227
break ;
226
228
case EETypeElementType . Int16 :
227
- shortValue = RuntimeHelpers . FastUnbox < short > ( srcObject ) ;
229
+ shortValue = Unsafe . As < byte , short > ( ref rawSrcValue ) ;
228
230
break ;
229
231
default :
230
232
goto Failure ;
@@ -236,13 +238,13 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
236
238
switch ( srcElementType )
237
239
{
238
240
case EETypeElementType . Char :
239
- ushortValue = ( ushort ) RuntimeHelpers . FastUnbox < char > ( srcObject ) ;
241
+ ushortValue = ( ushort ) Unsafe . As < byte , char > ( ref rawSrcValue ) ;
240
242
break ;
241
243
case EETypeElementType . Byte :
242
- ushortValue = ( ushort ) RuntimeHelpers . FastUnbox < byte > ( srcObject ) ;
244
+ ushortValue = ( ushort ) rawSrcValue ;
243
245
break ;
244
246
case EETypeElementType . UInt16 :
245
- ushortValue = RuntimeHelpers . FastUnbox < ushort > ( srcObject ) ;
247
+ ushortValue = Unsafe . As < byte , ushort > ( ref rawSrcValue ) ;
246
248
break ;
247
249
default :
248
250
goto Failure ;
@@ -254,22 +256,22 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
254
256
switch ( srcElementType )
255
257
{
256
258
case EETypeElementType . Char :
257
- intValue = ( int ) RuntimeHelpers . FastUnbox < char > ( srcObject ) ;
259
+ intValue = ( int ) Unsafe . As < byte , char > ( ref rawSrcValue ) ;
258
260
break ;
259
261
case EETypeElementType . SByte :
260
- intValue = ( int ) RuntimeHelpers . FastUnbox < sbyte > ( srcObject ) ;
262
+ intValue = ( int ) Unsafe . As < byte , sbyte > ( ref rawSrcValue ) ;
261
263
break ;
262
264
case EETypeElementType . Byte :
263
- intValue = ( int ) RuntimeHelpers . FastUnbox < byte > ( srcObject ) ;
265
+ intValue = ( int ) rawSrcValue ;
264
266
break ;
265
267
case EETypeElementType . Int16 :
266
- intValue = ( int ) RuntimeHelpers . FastUnbox < short > ( srcObject ) ;
268
+ intValue = ( int ) Unsafe . As < byte , short > ( ref rawSrcValue ) ;
267
269
break ;
268
270
case EETypeElementType . UInt16 :
269
- intValue = ( int ) RuntimeHelpers . FastUnbox < ushort > ( srcObject ) ;
271
+ intValue = ( int ) Unsafe . As < byte , ushort > ( ref rawSrcValue ) ;
270
272
break ;
271
273
case EETypeElementType . Int32 :
272
- intValue = RuntimeHelpers . FastUnbox < int > ( srcObject ) ;
274
+ intValue = Unsafe . As < byte , int > ( ref rawSrcValue ) ;
273
275
break ;
274
276
default :
275
277
goto Failure ;
@@ -281,16 +283,16 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
281
283
switch ( srcElementType )
282
284
{
283
285
case EETypeElementType . Char :
284
- uintValue = ( uint ) RuntimeHelpers . FastUnbox < char > ( srcObject ) ;
286
+ uintValue = ( uint ) Unsafe . As < byte , char > ( ref rawSrcValue ) ;
285
287
break ;
286
288
case EETypeElementType . Byte :
287
- uintValue = ( uint ) RuntimeHelpers . FastUnbox < byte > ( srcObject ) ;
289
+ uintValue = ( uint ) rawSrcValue ;
288
290
break ;
289
291
case EETypeElementType . UInt16 :
290
- uintValue = ( uint ) RuntimeHelpers . FastUnbox < ushort > ( srcObject ) ;
292
+ uintValue = ( uint ) Unsafe . As < byte , ushort > ( ref rawSrcValue ) ;
291
293
break ;
292
294
case EETypeElementType . UInt32 :
293
- uintValue = RuntimeHelpers . FastUnbox < uint > ( srcObject ) ;
295
+ uintValue = Unsafe . As < byte , uint > ( ref rawSrcValue ) ;
294
296
break ;
295
297
default :
296
298
goto Failure ;
@@ -302,28 +304,28 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
302
304
switch ( srcElementType )
303
305
{
304
306
case EETypeElementType . Char :
305
- longValue = ( long ) RuntimeHelpers . FastUnbox < char > ( srcObject ) ;
307
+ longValue = ( long ) Unsafe . As < byte , char > ( ref rawSrcValue ) ;
306
308
break ;
307
309
case EETypeElementType . SByte :
308
- longValue = ( long ) RuntimeHelpers . FastUnbox < sbyte > ( srcObject ) ;
310
+ longValue = ( long ) Unsafe . As < byte , sbyte > ( ref rawSrcValue ) ;
309
311
break ;
310
312
case EETypeElementType . Byte :
311
- longValue = ( long ) RuntimeHelpers . FastUnbox < byte > ( srcObject ) ;
313
+ longValue = ( long ) rawSrcValue ;
312
314
break ;
313
315
case EETypeElementType . Int16 :
314
- longValue = ( long ) RuntimeHelpers . FastUnbox < short > ( srcObject ) ;
316
+ longValue = ( long ) Unsafe . As < byte , short > ( ref rawSrcValue ) ;
315
317
break ;
316
318
case EETypeElementType . UInt16 :
317
- longValue = ( long ) RuntimeHelpers . FastUnbox < ushort > ( srcObject ) ;
319
+ longValue = ( long ) Unsafe . As < byte , ushort > ( ref rawSrcValue ) ;
318
320
break ;
319
321
case EETypeElementType . Int32 :
320
- longValue = ( long ) RuntimeHelpers . FastUnbox < int > ( srcObject ) ;
322
+ longValue = ( long ) Unsafe . As < byte , int > ( ref rawSrcValue ) ;
321
323
break ;
322
324
case EETypeElementType . UInt32 :
323
- longValue = ( long ) RuntimeHelpers . FastUnbox < uint > ( srcObject ) ;
325
+ longValue = ( long ) Unsafe . As < byte , uint > ( ref rawSrcValue ) ;
324
326
break ;
325
327
case EETypeElementType . Int64 :
326
- longValue = RuntimeHelpers . FastUnbox < long > ( srcObject ) ;
328
+ longValue = Unsafe . As < byte , long > ( ref rawSrcValue ) ;
327
329
break ;
328
330
default :
329
331
goto Failure ;
@@ -335,19 +337,19 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
335
337
switch ( srcElementType )
336
338
{
337
339
case EETypeElementType . Char :
338
- ulongValue = ( ulong ) RuntimeHelpers . FastUnbox < char > ( srcObject ) ;
340
+ ulongValue = ( ulong ) Unsafe . As < byte , char > ( ref rawSrcValue ) ;
339
341
break ;
340
342
case EETypeElementType . Byte :
341
- ulongValue = ( ulong ) RuntimeHelpers . FastUnbox < byte > ( srcObject ) ;
343
+ ulongValue = ( ulong ) rawSrcValue ;
342
344
break ;
343
345
case EETypeElementType . UInt16 :
344
- ulongValue = ( ulong ) RuntimeHelpers . FastUnbox < ushort > ( srcObject ) ;
346
+ ulongValue = ( ulong ) Unsafe . As < byte , ushort > ( ref rawSrcValue ) ;
345
347
break ;
346
348
case EETypeElementType . UInt32 :
347
- ulongValue = ( ulong ) RuntimeHelpers . FastUnbox < uint > ( srcObject ) ;
349
+ ulongValue = ( ulong ) Unsafe . As < byte , uint > ( ref rawSrcValue ) ;
348
350
break ;
349
351
case EETypeElementType . UInt64 :
350
- ulongValue = RuntimeHelpers . FastUnbox < ulong > ( srcObject ) ;
352
+ ulongValue = Unsafe . As < byte , ulong > ( ref rawSrcValue ) ;
351
353
break ;
352
354
default :
353
355
goto Failure ;
@@ -359,34 +361,34 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
359
361
switch ( srcElementType )
360
362
{
361
363
case EETypeElementType . Char :
362
- floatValue = ( float ) RuntimeHelpers . FastUnbox < char > ( srcObject ) ;
364
+ floatValue = ( float ) Unsafe . As < byte , char > ( ref rawSrcValue ) ;
363
365
break ;
364
366
case EETypeElementType . SByte :
365
- floatValue = ( float ) RuntimeHelpers . FastUnbox < sbyte > ( srcObject ) ;
367
+ floatValue = ( float ) Unsafe . As < byte , sbyte > ( ref rawSrcValue ) ;
366
368
break ;
367
369
case EETypeElementType . Byte :
368
- floatValue = ( float ) RuntimeHelpers . FastUnbox < byte > ( srcObject ) ;
370
+ floatValue = ( float ) rawSrcValue ;
369
371
break ;
370
372
case EETypeElementType . Int16 :
371
- floatValue = ( float ) RuntimeHelpers . FastUnbox < short > ( srcObject ) ;
373
+ floatValue = ( float ) Unsafe . As < byte , short > ( ref rawSrcValue ) ;
372
374
break ;
373
375
case EETypeElementType . UInt16 :
374
- floatValue = ( float ) RuntimeHelpers . FastUnbox < ushort > ( srcObject ) ;
376
+ floatValue = ( float ) Unsafe . As < byte , ushort > ( ref rawSrcValue ) ;
375
377
break ;
376
378
case EETypeElementType . Int32 :
377
- floatValue = ( float ) RuntimeHelpers . FastUnbox < int > ( srcObject ) ;
379
+ floatValue = ( float ) Unsafe . As < byte , int > ( ref rawSrcValue ) ;
378
380
break ;
379
381
case EETypeElementType . UInt32 :
380
- floatValue = ( float ) RuntimeHelpers . FastUnbox < uint > ( srcObject ) ;
382
+ floatValue = ( float ) Unsafe . As < byte , uint > ( ref rawSrcValue ) ;
381
383
break ;
382
384
case EETypeElementType . Int64 :
383
- floatValue = ( float ) RuntimeHelpers . FastUnbox < long > ( srcObject ) ;
385
+ floatValue = ( float ) Unsafe . As < byte , long > ( ref rawSrcValue ) ;
384
386
break ;
385
387
case EETypeElementType . UInt64 :
386
- floatValue = ( float ) RuntimeHelpers . FastUnbox < ulong > ( srcObject ) ;
388
+ floatValue = ( float ) Unsafe . As < byte , ulong > ( ref rawSrcValue ) ;
387
389
break ;
388
390
case EETypeElementType . Single :
389
- floatValue = RuntimeHelpers . FastUnbox < float > ( srcObject ) ;
391
+ floatValue = Unsafe . As < byte , float > ( ref rawSrcValue ) ;
390
392
break ;
391
393
default :
392
394
goto Failure ;
@@ -398,37 +400,37 @@ private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(obje
398
400
switch ( srcElementType )
399
401
{
400
402
case EETypeElementType . Char :
401
- doubleValue = ( double ) RuntimeHelpers . FastUnbox < char > ( srcObject ) ;
403
+ doubleValue = ( double ) Unsafe . As < byte , char > ( ref rawSrcValue ) ;
402
404
break ;
403
405
case EETypeElementType . SByte :
404
- doubleValue = ( double ) RuntimeHelpers . FastUnbox < sbyte > ( srcObject ) ;
406
+ doubleValue = ( double ) Unsafe . As < byte , sbyte > ( ref rawSrcValue ) ;
405
407
break ;
406
408
case EETypeElementType . Byte :
407
- doubleValue = ( double ) RuntimeHelpers . FastUnbox < byte > ( srcObject ) ;
409
+ doubleValue = ( double ) rawSrcValue ;
408
410
break ;
409
411
case EETypeElementType . Int16 :
410
- doubleValue = ( double ) RuntimeHelpers . FastUnbox < short > ( srcObject ) ;
412
+ doubleValue = ( double ) Unsafe . As < byte , short > ( ref rawSrcValue ) ;
411
413
break ;
412
414
case EETypeElementType . UInt16 :
413
- doubleValue = ( double ) RuntimeHelpers . FastUnbox < ushort > ( srcObject ) ;
415
+ doubleValue = ( double ) Unsafe . As < byte , ushort > ( ref rawSrcValue ) ;
414
416
break ;
415
417
case EETypeElementType . Int32 :
416
- doubleValue = ( double ) RuntimeHelpers . FastUnbox < int > ( srcObject ) ;
418
+ doubleValue = ( double ) Unsafe . As < byte , int > ( ref rawSrcValue ) ;
417
419
break ;
418
420
case EETypeElementType . UInt32 :
419
- doubleValue = ( double ) RuntimeHelpers . FastUnbox < uint > ( srcObject ) ;
421
+ doubleValue = ( double ) Unsafe . As < byte , uint > ( ref rawSrcValue ) ;
420
422
break ;
421
423
case EETypeElementType . Int64 :
422
- doubleValue = ( double ) RuntimeHelpers . FastUnbox < long > ( srcObject ) ;
424
+ doubleValue = ( double ) Unsafe . As < byte , long > ( ref rawSrcValue ) ;
423
425
break ;
424
426
case EETypeElementType . UInt64 :
425
- doubleValue = ( double ) RuntimeHelpers . FastUnbox < ulong > ( srcObject ) ;
427
+ doubleValue = ( double ) Unsafe . As < byte , ulong > ( ref rawSrcValue ) ;
426
428
break ;
427
429
case EETypeElementType . Single :
428
- doubleValue = ( double ) RuntimeHelpers . FastUnbox < float > ( srcObject ) ;
430
+ doubleValue = ( double ) Unsafe . As < byte , float > ( ref rawSrcValue ) ;
429
431
break ;
430
432
case EETypeElementType . Double :
431
- doubleValue = RuntimeHelpers . FastUnbox < double > ( srcObject ) ;
433
+ doubleValue = Unsafe . As < byte , double > ( ref rawSrcValue ) ;
432
434
break ;
433
435
default :
434
436
goto Failure ;
0 commit comments