@@ -430,6 +430,12 @@ public static IDynamicNumber GetNumber(object lhs, object rhs)
430
430
if ( lhs == null || rhs == null )
431
431
return null ;
432
432
433
+ if ( lhs is string lhsString && ! TryParse ( lhsString , out lhs ) )
434
+ return null ;
435
+
436
+ if ( rhs is string rhsString && ! TryParse ( rhsString , out rhs ) )
437
+ return null ;
438
+
433
439
if ( ! TryGetRanking ( lhs . GetType ( ) , out int lhsRanking ) || ! TryGetRanking ( rhs . GetType ( ) , out int rhsRanking ) )
434
440
return null ;
435
441
@@ -438,26 +444,35 @@ public static IDynamicNumber GetNumber(object lhs, object rhs)
438
444
return maxNumber ;
439
445
}
440
446
447
+ public static IDynamicNumber AssertNumbers ( string name , object lhs , object rhs )
448
+ {
449
+ var number = GetNumber ( lhs , rhs ) ;
450
+ if ( number == null )
451
+ throw new ArgumentException ( $ "Invalid numbers passed to { name } : { lhs ? . GetType ( ) . Name ?? "null" } , { rhs ? . GetType ( ) . Name ?? "null" } ") ;
452
+
453
+ return number ;
454
+ }
455
+
441
456
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
442
- public static object Add ( object lhs , object rhs ) => GetNumber ( lhs , rhs ) . add ( lhs , rhs ) ;
457
+ public static object Add ( object lhs , object rhs ) => AssertNumbers ( nameof ( Add ) , lhs , rhs ) . add ( lhs , rhs ) ;
443
458
444
459
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
445
- public static object Sub ( object lhs , object rhs ) => GetNumber ( lhs , rhs ) . sub ( lhs , rhs ) ;
460
+ public static object Sub ( object lhs , object rhs ) => AssertNumbers ( nameof ( Subtract ) , lhs , rhs ) . sub ( lhs , rhs ) ;
446
461
447
462
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
448
- public static object Subtract ( object lhs , object rhs ) => GetNumber ( lhs , rhs ) . sub ( lhs , rhs ) ;
463
+ public static object Subtract ( object lhs , object rhs ) => AssertNumbers ( nameof ( Subtract ) , lhs , rhs ) . sub ( lhs , rhs ) ;
449
464
450
465
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
451
- public static object Mul ( object lhs , object rhs ) => GetNumber ( lhs , rhs ) . mul ( lhs , rhs ) ;
466
+ public static object Mul ( object lhs , object rhs ) => AssertNumbers ( nameof ( Multiply ) , lhs , rhs ) . mul ( lhs , rhs ) ;
452
467
453
468
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
454
- public static object Multiply ( object lhs , object rhs ) => GetNumber ( lhs , rhs ) . mul ( lhs , rhs ) ;
469
+ public static object Multiply ( object lhs , object rhs ) => AssertNumbers ( nameof ( Multiply ) , lhs , rhs ) . mul ( lhs , rhs ) ;
455
470
456
471
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
457
- public static object Div ( object lhs , object rhs ) => GetNumber ( lhs , rhs ) . div ( lhs , rhs ) ;
472
+ public static object Div ( object lhs , object rhs ) => AssertNumbers ( nameof ( Divide ) , lhs , rhs ) . div ( lhs , rhs ) ;
458
473
459
474
[ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
460
- public static object Divide ( object lhs , object rhs ) => GetNumber ( lhs , rhs ) . div ( lhs , rhs ) ;
475
+ public static object Divide ( object lhs , object rhs ) => AssertNumbers ( nameof ( Divide ) , lhs , rhs ) . div ( lhs , rhs ) ;
461
476
462
477
public static bool TryParse ( string strValue , out object result )
463
478
{
0 commit comments