@@ -430,6 +430,12 @@ public static IDynamicNumber GetNumber(object lhs, object rhs)
430430 if ( lhs == null || rhs == null )
431431 return null ;
432432
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+
433439 if ( ! TryGetRanking ( lhs . GetType ( ) , out int lhsRanking ) || ! TryGetRanking ( rhs . GetType ( ) , out int rhsRanking ) )
434440 return null ;
435441
@@ -438,26 +444,35 @@ public static IDynamicNumber GetNumber(object lhs, object rhs)
438444 return maxNumber ;
439445 }
440446
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+
441456 [ 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 ) ;
443458
444459 [ 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 ) ;
446461
447462 [ 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 ) ;
449464
450465 [ 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 ) ;
452467
453468 [ 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 ) ;
455470
456471 [ 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 ) ;
458473
459474 [ 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 ) ;
461476
462477 public static bool TryParse ( string strValue , out object result )
463478 {
0 commit comments