CSE.Exps.ArithExp.Negate C# (CSharp) Method

Negate() static private method

Applies numeric negation (i.e. unary minus) to numeric values
static private Negate ( CseObject obj ) : CseObject
obj CseObject The CseObject with the value to negate
return CseObject
		internal static CseObject Negate(CseObject obj) {
			CseObject result = (CseObject)obj.Clone();

			dynamic value = obj.Value;
			double numValue;

			if (value is string)
				throw new CseLogicException(CseLogicExceptionType.CANT_NEGATE_NON_NUM, value.ToString());
			else if (!double.TryParse(value.ToString(), out numValue)) {
				MethodInfo mi = value.GetType().GetMethod(OpOverloadNames.UMINUS);
				if (null != mi) {
					result.Value = value.GetType().InvokeMember(OpOverloadNames.UMINUS, OpOverloadNames.Flags, null, CsEval.evalEnvironment, new object[] { value });
					return result;
				}
				else
					throw new CseLogicException(CseLogicExceptionType.CANT_NEGATE_NON_NUM, value.ToString());
			}

			numValue *= -1;
			result.Value = numValue;

			try {
				result = CastExp.Parse(CsEval.evalEnvironment, value.GetType().Name, result);
			}
			catch {
				if (value.ToString().ToLower().Contains("e"))
					result = LiteralExp.ParseEType(numValue.ToString());
				else if (value.ToString().Contains("."))
					result = LiteralExp.ParseFloatType(numValue.ToString(), null);
				else
					throw new CseLogicException(CseLogicExceptionType.ERROR_NEGATING_VALUE_OF_TYPE, value.ToString(), value.GetType().Name);
			}

			return result;
		}
	}

Usage Example

Example #1
0
 ///
 /// <summary>
 ///		Negates the value property
 /// </summary>
 ///
 /// <param name="obj">CseObject to change</param>
 ///
 /// <returns>The CseObject with its value property negated</returns>
 ///
 public static CseObject operator -(CseObject obj)
 {
     return(ArithExp.Negate(obj));
 }