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))); } }
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)); }