private QueryScalarValue ExecuteBinaryOperation(QueryScalarValue firstValue, QueryScalarValue secondValue, Func <object, object, Type, object> evaluationMethod) { var t1 = (QueryClrPrimitiveType)firstValue.Type; var t2 = (QueryClrPrimitiveType)secondValue.Type; object v1 = firstValue.Value; object v2 = secondValue.Value; QueryClrPrimitiveType commonType = t1; try { commonType = (QueryClrPrimitiveType)this.GetCommonType(t1, t2); if (v1 == null || v2 == null) { return(commonType.CreateErrorValue(new QueryError("Null values are not supported for the operation."))); } var result = evaluationMethod(v1, v2, commonType.ClrType); return(commonType.CreateValue(result)); } catch (Exception ex) { return(commonType.CreateErrorValue(new QueryError(ex.Message))); } }
/// <summary> /// Compares the given clr value value to the given query scalar value, and throws a DataComparisonException if they dont match /// </summary> /// <param name="expected">expected CLR value</param> /// <param name="actual">actual query primitive value to compare</param> /// <param name="assert">The assertion handler to use</param> public void Compare(object expected, QueryScalarValue actual, AssertionHandler assert) { if (actual.IsDynamicPropertyValue()) { expected = this.converter.SerializePrimitive(expected); actual = new QueryClrPrimitiveType(typeof(string), actual.Type.EvaluationStrategy).CreateValue(this.converter.SerializePrimitive(actual.Value)); } this.UnderlyingComparer.Compare(expected, actual, assert); }
/// <summary> /// Visits a <see cref="QueryClrPrimitiveType"/>. /// </summary> /// <param name="type">Query type being visited.</param> /// <returns>The result of visiting this query type.</returns> public ComparisonResult Visit(QueryClrPrimitiveType type) { return(this.parent.ComparePrimitive((QueryScalarValue)this.expectedValue, this.actualValue, this.path, this.shouldThrow)); }
private QueryScalarValue EvaluateCast(object value, QueryClrPrimitiveType targetType) { object result = null; if (value == null) { return(targetType.NullValue); } if (targetType.ClrType == typeof(bool)) { result = Convert.ToBoolean(value, CultureInfo.InvariantCulture); } #if !SILVERLIGHT if (targetType.ClrType == typeof(byte[])) { // TODO: figure out what's the intended behavior here ////result = Convert.ToBase64String(value as byte[], Base64FormattingOptions.None); result = value as byte[]; } #endif if (targetType.ClrType == typeof(byte)) { result = Convert.ToByte(value, CultureInfo.InvariantCulture); } if (targetType.ClrType == typeof(sbyte)) { result = Convert.ToSByte(value, CultureInfo.InvariantCulture); } if (targetType.ClrType == typeof(char)) { result = Convert.ToChar(value, CultureInfo.InvariantCulture); } if (targetType.ClrType == typeof(decimal)) { result = Convert.ToDecimal(value, CultureInfo.InvariantCulture); } if (targetType.ClrType == typeof(double)) { result = Convert.ToDouble(value, CultureInfo.InvariantCulture); } if (targetType.ClrType == typeof(float)) { result = Convert.ToSingle(value, CultureInfo.InvariantCulture); } if (targetType.ClrType == typeof(int)) { result = Convert.ToInt32(value, CultureInfo.InvariantCulture); } if (targetType.ClrType == typeof(uint)) { result = Convert.ToUInt32(value, CultureInfo.InvariantCulture); } if (targetType.ClrType == typeof(long)) { result = Convert.ToInt64(value, CultureInfo.InvariantCulture); } if (targetType.ClrType == typeof(ulong)) { result = Convert.ToUInt64(value, CultureInfo.InvariantCulture); } if (targetType.ClrType == typeof(short)) { result = Convert.ToInt16(value, CultureInfo.InvariantCulture); } if (targetType.ClrType == typeof(ushort)) { result = Convert.ToUInt16(value, CultureInfo.InvariantCulture); } if (targetType.ClrType == typeof(string)) { result = Convert.ToString(value, CultureInfo.InvariantCulture); } if (targetType.ClrType == typeof(DateTime)) { result = Convert.ToDateTime(value, CultureInfo.InvariantCulture); } if (targetType.ClrType == typeof(Guid)) { Type valueType = null; if (value != null) { valueType = value.GetType(); } if (valueType == typeof(Guid) || valueType == typeof(Guid?)) { result = (Guid)value; } else if (value != null) { throw new InvalidCastException(string.Format(CultureInfo.InvariantCulture, "Cannot cast value of type '{0}' to guid", value.GetType())); } } return(targetType.CreateValue(result)); }
/// <summary> /// Creates a QueryValue of a given type. /// </summary> /// <param name="type">Type of the QueryValue that will be generated.</param> /// <returns>QueryValue representing the provided object.</returns> public QueryValue Visit(QueryClrPrimitiveType type) { return(this.VisitScalar(type)); }