/// <summary> /// Returns a value for the logical NOT operator node. /// </summary> /// <param name="context">Context to evaluate expressions against.</param> /// <param name="evalContext">Current expression evaluation context.</param> /// <returns>Node's value.</returns> protected override object Get(object context, EvaluationContext evalContext) { object operand = GetValue(Operand, context, evalContext); TypeConverter convert = TypeDescriptor.GetConverter(operand.GetType()); if (NumberUtils.IsInteger(operand)) { return(NumberUtils.BitwiseNot(operand)); } else if (convert.CanConvertTo(typeof(int))) { operand = Convert.ToInt32(operand); return(NumberUtils.BitwiseNot(operand)); } else if (operand is Enum) { Type enumType = operand.GetType(); Type integralType = Enum.GetUnderlyingType(enumType); operand = Convert.ChangeType(operand, integralType); object result = NumberUtils.BitwiseNot(operand); return(Enum.ToObject(enumType, result)); } else { return(!Convert.ToBoolean(operand)); } }
/// <summary> /// Returns a value for the logical NOT operator node. /// </summary> /// <param name="context">Context to evaluate expressions against.</param> /// <param name="evalContext">Current expression evaluation context.</param> /// <returns>Node's value.</returns> protected override object Get(object context, EvaluationContext evalContext) { var operand = GetValue(Operand, context, evalContext); if (NumberUtils.IsInteger(operand)) { return(NumberUtils.BitwiseNot(operand)); } if (operand is Enum) { var enumType = operand.GetType(); var integralType = Enum.GetUnderlyingType(enumType); operand = Convert.ChangeType(operand, integralType); var result = NumberUtils.BitwiseNot(operand); return(Enum.ToObject(enumType, result)); } return(!Convert.ToBoolean(operand)); }
public void BitwiseNot() { Assert.AreEqual( ~((Byte)2), NumberUtils.BitwiseNot((Byte)2) ); Assert.AreEqual(~((SByte)2), NumberUtils.BitwiseNot((SByte)2)); Assert.AreEqual(~((Int16)2), NumberUtils.BitwiseNot((Int16)2)); Assert.AreEqual(~((UInt16)2), NumberUtils.BitwiseNot((UInt16)2)); Assert.AreEqual(~((Int32)2), NumberUtils.BitwiseNot((Int32)2)); Assert.AreEqual(~((UInt32)2), NumberUtils.BitwiseNot((UInt32)2)); Assert.AreEqual(~((Int64)2), NumberUtils.BitwiseNot((Int64)2)); Assert.AreEqual(~((UInt64)2), NumberUtils.BitwiseNot((UInt64)2)); Assert.AreEqual( false, NumberUtils.BitwiseNot(true) ); try { NumberUtils.BitwiseNot((double)2.0); Assert.Fail(); } catch(ArgumentException) {} }