public static object PerformUnaryOp(UnaryOperator op, object left, object right) { object operand = op.Side == UnaryOperator.OperandSide.Left ? left : right; IEvaluator tempv = operand as IEvaluator; if (tempv != null) { operand = tempv.Evaluate(); } try { return(op.ExecuteOperator(operand)); } catch (InvalidCastException) when(operand is IOperator) { throw new ArgumentException("Unary operand cannot be " + operand.GetType().Name); } catch (Exception ex) when(ex is InvalidCastException || ex is FormatException || ex is ArgumentException || ex is OverflowException) { throw new ArgumentException("Unary operator '" + op.OperatorString + "' not defined."); } }