internal override object BitOr(ArithmeticLiteral v) { return(v.BitOr(this.m_value)); }
internal override object Modulus(ArithmeticLiteral v) { return(v.Modulus(this.m_value)); }
internal override object Modulus(ArithmeticLiteral v) { return v.Modulus((ushort) this.m_value); }
internal override object Subtract(ArithmeticLiteral v) { return v.Subtract((ushort) this.m_value); }
private static object EvaluateBinaryOperation(CodeBinaryOperatorExpression binaryExpr, Type lhsType, object lhsValue, CodeBinaryOperatorType operation, Type rhsType, object rhsValue) { Literal literal; Literal literal2; ArithmeticLiteral literal3; ArithmeticLiteral literal4; RuleEvaluationException exception; switch (operation) { case CodeBinaryOperatorType.Add: literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue); if (literal3 == null) { break; } literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue); if (literal4 == null) { break; } return(literal3.Add(literal4)); case CodeBinaryOperatorType.Subtract: literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue); if (literal3 == null) { break; } literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue); if (literal4 == null) { break; } return(literal3.Subtract(literal4)); case CodeBinaryOperatorType.Multiply: literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue); if (literal3 == null) { break; } literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue); if (literal4 == null) { break; } return(literal3.Multiply(literal4)); case CodeBinaryOperatorType.Divide: literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue); if (literal3 == null) { break; } literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue); if (literal4 == null) { break; } return(literal3.Divide(literal4)); case CodeBinaryOperatorType.Modulus: literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue); if (literal3 == null) { break; } literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue); if (literal4 == null) { break; } return(literal3.Modulus(literal4)); case CodeBinaryOperatorType.IdentityInequality: return(lhsValue != rhsValue); case CodeBinaryOperatorType.IdentityEquality: return(lhsValue == rhsValue); case CodeBinaryOperatorType.ValueEquality: literal = Literal.MakeLiteral(lhsType, lhsValue); if (literal == null) { break; } literal2 = Literal.MakeLiteral(rhsType, rhsValue); if (literal2 == null) { break; } return(literal.Equal(literal2)); case CodeBinaryOperatorType.BitwiseOr: literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue); if (literal3 == null) { break; } literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue); if (literal4 == null) { break; } return(literal3.BitOr(literal4)); case CodeBinaryOperatorType.BitwiseAnd: literal3 = ArithmeticLiteral.MakeLiteral(lhsType, lhsValue); if (literal3 == null) { break; } literal4 = ArithmeticLiteral.MakeLiteral(rhsType, rhsValue); if (literal4 == null) { break; } return(literal3.BitAnd(literal4)); case CodeBinaryOperatorType.LessThan: literal = Literal.MakeLiteral(lhsType, lhsValue); if (literal == null) { break; } literal2 = Literal.MakeLiteral(rhsType, rhsValue); if (literal2 == null) { break; } return(literal.LessThan(literal2)); case CodeBinaryOperatorType.LessThanOrEqual: literal = Literal.MakeLiteral(lhsType, lhsValue); if (literal == null) { break; } literal2 = Literal.MakeLiteral(rhsType, rhsValue); if (literal2 == null) { break; } return(literal.LessThanOrEqual(literal2)); case CodeBinaryOperatorType.GreaterThan: literal = Literal.MakeLiteral(lhsType, lhsValue); if (literal == null) { break; } literal2 = Literal.MakeLiteral(rhsType, rhsValue); if (literal2 == null) { break; } return(literal.GreaterThan(literal2)); case CodeBinaryOperatorType.GreaterThanOrEqual: literal = Literal.MakeLiteral(lhsType, lhsValue); if (literal == null) { break; } literal2 = Literal.MakeLiteral(rhsType, rhsValue); if (literal2 == null) { break; } return(literal.GreaterThanOrEqual(literal2)); default: exception = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.BinaryOpNotSupported, new object[] { operation.ToString() })); exception.Data["ErrorObject"] = binaryExpr; throw exception; } exception = new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.BinaryOpFails, new object[] { operation.ToString(), RuleDecompiler.DecompileType(lhsType), RuleDecompiler.DecompileType(rhsType) })); exception.Data["ErrorObject"] = binaryExpr; throw exception; }
internal override object BitOr(ArithmeticLiteral v) { return v.BitOr((ushort) this.m_value); }
internal override object Subtract(ArithmeticLiteral v) { return v.Subtract(m_value); }
internal override object Add(ArithmeticLiteral v) { return(v.Add()); }
internal override object BitOr(ArithmeticLiteral v) { return v.BitOr(); }
internal virtual object BitOr(ArithmeticLiteral v) { string message = string.Format(CultureInfo.CurrentCulture, Messages.IncompatibleArithmeticTypes, v.TypeName, CodeBinaryOperatorType.BitwiseOr, this.TypeName); throw new RuleEvaluationIncompatibleTypesException(message, v.m_type, CodeBinaryOperatorType.BitwiseOr, this.m_type); }
internal override object Modulus(ArithmeticLiteral v) { return v.Modulus(); }
internal override object Add(ArithmeticLiteral v) { return v.Add(); }
internal override object BitAnd(ArithmeticLiteral v) { return v.BitAnd(m_value); }
internal override object Subtract(ArithmeticLiteral v) { return(v.Subtract(this.m_value)); }
internal override object Multiply(ArithmeticLiteral v) { return v.Multiply(m_value); }
private static bool AdjustValueStandard(Type operandType, object operandValue, Type toType, out object converted) { converted = operandValue; if (operandValue == null) { ValidationError error; if (!toType.IsValueType) { return(true); } if (!ConditionHelper.IsNullableValueType(toType)) { throw new InvalidCastException(string.Format(CultureInfo.CurrentCulture, Messages.CannotCastNullToValueType, new object[] { RuleDecompiler.DecompileType(toType) })); } converted = Activator.CreateInstance(toType); return(RuleValidation.StandardImplicitConversion(operandType, toType, null, out error)); } Type c = operandValue.GetType(); if (c == toType) { return(true); } if (toType.IsAssignableFrom(c)) { return(true); } if (c.IsValueType && toType.IsValueType) { if (c.IsEnum) { c = Enum.GetUnderlyingType(c); operandValue = ArithmeticLiteral.MakeLiteral(c, operandValue).Value; } bool flag = ConditionHelper.IsNullableValueType(toType); Type enumType = flag ? Nullable.GetUnderlyingType(toType) : toType; if (enumType.IsEnum) { object obj2; Type underlyingType = Enum.GetUnderlyingType(enumType); if (AdjustValueStandard(c, operandValue, underlyingType, out obj2)) { converted = Enum.ToObject(enumType, obj2); if (flag) { converted = Activator.CreateInstance(toType, new object[] { converted }); } return(true); } } else if (enumType.IsPrimitive || (enumType == typeof(decimal))) { if (c == typeof(char)) { char ch = (char)operandValue; if (enumType == typeof(float)) { converted = (float)ch; } else if (enumType == typeof(double)) { converted = (double)ch; } else if (enumType == typeof(decimal)) { converted = ch; } else { converted = ((IConvertible)ch).ToType(enumType, CultureInfo.CurrentCulture); } if (flag) { converted = Activator.CreateInstance(toType, new object[] { converted }); } return(true); } if (c == typeof(float)) { float num = (float)operandValue; if (enumType == typeof(char)) { converted = (char)((ushort)num); } else { converted = ((IConvertible)num).ToType(enumType, CultureInfo.CurrentCulture); } if (flag) { converted = Activator.CreateInstance(toType, new object[] { converted }); } return(true); } if (c == typeof(double)) { double num2 = (double)operandValue; if (enumType == typeof(char)) { converted = (char)((ushort)num2); } else { converted = ((IConvertible)num2).ToType(enumType, CultureInfo.CurrentCulture); } if (flag) { converted = Activator.CreateInstance(toType, new object[] { converted }); } return(true); } if (c == typeof(decimal)) { decimal num3 = (decimal)operandValue; if (enumType == typeof(char)) { converted = (char)num3; } else { converted = ((IConvertible)num3).ToType(enumType, CultureInfo.CurrentCulture); } if (flag) { converted = Activator.CreateInstance(toType, new object[] { converted }); } return(true); } IConvertible convertible = operandValue as IConvertible; if (convertible != null) { try { converted = convertible.ToType(enumType, CultureInfo.CurrentCulture); if (flag) { converted = Activator.CreateInstance(toType, new object[] { converted }); } return(true); } catch (InvalidCastException) { return(false); } } } } return(false); }
internal override object Divide(ArithmeticLiteral v) { return v.Divide(m_value); }
internal virtual object Add(ArithmeticLiteral v) { throw new RuleEvaluationIncompatibleTypesException(string.Format(CultureInfo.CurrentCulture, Messages.IncompatibleArithmeticTypes, new object[] { v.TypeName, CodeBinaryOperatorType.Add, this.TypeName }), v.m_type, CodeBinaryOperatorType.Add, this.m_type); }
private static bool AdjustValueStandard(Type operandType, object operandValue, Type toType, out object converted) { // assume it's the same for now converted = operandValue; // check for null if (operandValue == null) { // are we converting to a value type? if (toType.IsValueType) { // is the conversion to nullable? if (!ConditionHelper.IsNullableValueType(toType)) { // value type and null, so no conversion possible string message = string.Format(CultureInfo.CurrentCulture, Messages.CannotCastNullToValueType, RuleDecompiler.DecompileType(toType)); throw new InvalidCastException(message); } // here we have a Nullable<T> // however, we may need to call the implicit conversion operator if the types are not compatible converted = Activator.CreateInstance(toType); return(RuleValidation.StandardImplicitConversion(operandType, toType, null, out ValidationError error)); } // not a value type, so null is valid return(true); } // check simple cases Type currentType = operandValue.GetType(); if (currentType == toType) { return(true); } // now the fun begins // this should handle most class conversions if (toType.IsAssignableFrom(currentType)) { return(true); } // handle the numerics (both implicit and explicit), along with nullable // note that if the value was null, it's already handled, so value cannot be nullable if ((currentType.IsValueType) && (toType.IsValueType)) { if (currentType.IsEnum) { // strip off the enum representation currentType = Enum.GetUnderlyingType(currentType); ArithmeticLiteral literal = ArithmeticLiteral.MakeLiteral(currentType, operandValue); operandValue = literal.Value; } bool resultNullable = ConditionHelper.IsNullableValueType(toType); Type resultType = (resultNullable) ? Nullable.GetUnderlyingType(toType) : toType; if (resultType.IsEnum) { // Enum.ToObject may throw if currentType is not type SByte, // Int16, Int32, Int64, Byte, UInt16, UInt32, or UInt64. // So we adjust currentValue to the underlying type (which may throw if out of range) Type underlyingType = Enum.GetUnderlyingType(resultType); if (AdjustValueStandard(currentType, operandValue, underlyingType, out object adjusted)) { converted = Enum.ToObject(resultType, adjusted); if (resultNullable) { converted = Activator.CreateInstance(toType, converted); } return(true); } } else if ((resultType.IsPrimitive) || (resultType == typeof(decimal))) { // resultType must be a primitive to continue (not a struct) // (enums and generics handled above) if (currentType == typeof(char)) { char c = (char)operandValue; if (resultType == typeof(float)) { converted = (float)c; } else if (resultType == typeof(double)) { converted = (double)c; } else if (resultType == typeof(decimal)) { converted = (decimal)c; } else { converted = ((IConvertible)c).ToType(resultType, CultureInfo.CurrentCulture); } if (resultNullable) { converted = Activator.CreateInstance(toType, converted); } return(true); } else if (currentType == typeof(float)) { float f = (float)operandValue; if (resultType == typeof(char)) { converted = (char)f; } else { converted = ((IConvertible)f).ToType(resultType, CultureInfo.CurrentCulture); } if (resultNullable) { converted = Activator.CreateInstance(toType, converted); } return(true); } else if (currentType == typeof(double)) { double d = (double)operandValue; if (resultType == typeof(char)) { converted = (char)d; } else { converted = ((IConvertible)d).ToType(resultType, CultureInfo.CurrentCulture); } if (resultNullable) { converted = Activator.CreateInstance(toType, converted); } return(true); } else if (currentType == typeof(decimal)) { decimal d = (decimal)operandValue; if (resultType == typeof(char)) { converted = (char)d; } else { converted = ((IConvertible)d).ToType(resultType, CultureInfo.CurrentCulture); } if (resultNullable) { converted = Activator.CreateInstance(toType, converted); } return(true); } else { if (operandValue is IConvertible convert) { try { converted = convert.ToType(resultType, CultureInfo.CurrentCulture); if (resultNullable) { converted = Activator.CreateInstance(toType, converted); } return(true); } catch (InvalidCastException) { // not IConvertable, so can't do it return(false); } } } } } // no luck with standard conversions, so no conversion done return(false); }
internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { ValidationError error; CodeBinaryOperatorExpression newParent = (CodeBinaryOperatorExpression)expression; if (!validation.PushParentExpression(newParent)) { return(null); } if (isWritten) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, new object[] { typeof(CodeBinaryOperatorExpression).ToString() }), 0x17a); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } RuleExpressionInfo info = null; RuleExpressionInfo info2 = null; if (newParent.Left == null) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullBinaryOpLHS, new object[] { newParent.Operator.ToString() }), 0x541); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } else { if (newParent.Left is CodeTypeReferenceExpression) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { newParent.Left.GetType().FullName }), 0x548); error.UserData["ErrorObject"] = newParent.Left; validation.AddError(error); return(null); } info = RuleExpressionWalker.Validate(validation, newParent.Left, false); } if (newParent.Right == null) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.NullBinaryOpRHS, new object[] { newParent.Operator.ToString() }), 0x543); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } else { if (newParent.Right is CodeTypeReferenceExpression) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { newParent.Right.GetType().FullName }), 0x548); error.UserData["ErrorObject"] = newParent.Right; validation.AddError(error); return(null); } info2 = RuleExpressionWalker.Validate(validation, newParent.Right, false); } validation.PopParentExpression(); RuleBinaryExpressionInfo info3 = null; if ((info != null) && (info2 != null)) { Type expressionType = info.ExpressionType; Type rhs = info2.ExpressionType; switch (newParent.Operator) { case CodeBinaryOperatorType.Add: case CodeBinaryOperatorType.Subtract: case CodeBinaryOperatorType.Multiply: case CodeBinaryOperatorType.Divide: case CodeBinaryOperatorType.Modulus: case CodeBinaryOperatorType.BitwiseOr: case CodeBinaryOperatorType.BitwiseAnd: info3 = ArithmeticLiteral.ResultType(newParent.Operator, expressionType, newParent.Left, rhs, newParent.Right, validation, out error); if (info3 == null) { if ((!(expressionType == typeof(ulong)) || !PromotionPossible(rhs, newParent.Right)) && (!(rhs == typeof(ulong)) || !PromotionPossible(expressionType, newParent.Left))) { error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } else { info3 = new RuleBinaryExpressionInfo(expressionType, rhs, typeof(ulong)); } } goto Label_063E; case CodeBinaryOperatorType.IdentityInequality: case CodeBinaryOperatorType.IdentityEquality: info3 = new RuleBinaryExpressionInfo(expressionType, rhs, typeof(bool)); goto Label_063E; case CodeBinaryOperatorType.ValueEquality: info3 = Literal.AllowedComparison(expressionType, newParent.Left, rhs, newParent.Right, newParent.Operator, validation, out error); if (info3 == null) { if ((!(expressionType == typeof(ulong)) || !PromotionPossible(rhs, newParent.Right)) && (!(rhs == typeof(ulong)) || !PromotionPossible(expressionType, newParent.Left))) { error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } else { info3 = new RuleBinaryExpressionInfo(expressionType, rhs, typeof(bool)); } } goto Label_063E; case CodeBinaryOperatorType.BooleanOr: case CodeBinaryOperatorType.BooleanAnd: info3 = new RuleBinaryExpressionInfo(expressionType, rhs, typeof(bool)); if (expressionType != typeof(bool)) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.LogicalOpBadTypeLHS, new object[] { newParent.Operator.ToString(), (expressionType == typeof(NullLiteral)) ? Messages.NullValue : RuleDecompiler.DecompileType(expressionType) }), 0x542); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); info3 = null; } if (rhs != typeof(bool)) { error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.LogicalOpBadTypeRHS, new object[] { newParent.Operator.ToString(), (rhs == typeof(NullLiteral)) ? Messages.NullValue : RuleDecompiler.DecompileType(rhs) }), 0x544); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); info3 = null; } goto Label_063E; case CodeBinaryOperatorType.LessThan: case CodeBinaryOperatorType.LessThanOrEqual: case CodeBinaryOperatorType.GreaterThan: case CodeBinaryOperatorType.GreaterThanOrEqual: info3 = Literal.AllowedComparison(expressionType, newParent.Left, rhs, newParent.Right, newParent.Operator, validation, out error); if (info3 == null) { if ((!(expressionType == typeof(ulong)) || !PromotionPossible(rhs, newParent.Right)) && (!(rhs == typeof(ulong)) || !PromotionPossible(expressionType, newParent.Left))) { error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } else { info3 = new RuleBinaryExpressionInfo(expressionType, rhs, typeof(bool)); } } goto Label_063E; } error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.BinaryOpNotSupported, new object[] { newParent.Operator.ToString() }), 0x548); error.UserData["ErrorObject"] = newParent; validation.Errors.Add(error); } Label_063E: if (info3 != null) { MethodInfo methodInfo = info3.MethodInfo; if (methodInfo == null) { return(info3); } object[] customAttributes = methodInfo.GetCustomAttributes(typeof(RuleAttribute), true); if ((customAttributes == null) || (customAttributes.Length <= 0)) { return(info3); } Stack <MemberInfo> stack = new Stack <MemberInfo>(); stack.Push(methodInfo); bool flag = true; foreach (RuleAttribute attribute in customAttributes) { if (!attribute.Validate(validation, methodInfo, methodInfo.DeclaringType, methodInfo.GetParameters())) { flag = false; } } stack.Pop(); if (!flag) { return(null); } } return(info3); }
internal override object BitAnd(ArithmeticLiteral v) { return(v.BitAnd((ushort)this.m_value)); }
internal override object Divide(ArithmeticLiteral v) { return v.Divide((ushort) this.m_value); }
internal override object BitOr(ArithmeticLiteral v) { return(v.BitOr()); }
internal override object Multiply(ArithmeticLiteral v) { return v.Multiply((ushort) this.m_value); }
internal override object Divide(ArithmeticLiteral v) { return(v.Divide()); }
internal override object Add(ArithmeticLiteral v) { return v.Add(this.m_value); }
internal override object Modulus(ArithmeticLiteral v) { return(v.Modulus()); }
internal override object Divide(ArithmeticLiteral v) { return(v.Divide(this.m_value)); }
internal override object Multiply(ArithmeticLiteral v) { return(v.Multiply()); }
internal override object Multiply(ArithmeticLiteral v) { return(v.Multiply(this.m_value)); }
internal override object Subtract(ArithmeticLiteral v) { return(v.Subtract()); }
internal override object Add(ArithmeticLiteral v) { return(v.Add(this.m_value)); }
public override object Invoke(object obj, BindingFlags invokeAttr, Binder binder, object[] parameters, CultureInfo culture) { object obj2; ArithmeticLiteral literal; ArithmeticLiteral literal2; Literal literal3; Literal literal4; if (this.lhsRootType == null) { this.lhsRootType = Enum.GetUnderlyingType(this.lhsBaseType); } if (this.rhsRootType == null) { this.rhsRootType = Enum.GetUnderlyingType(this.rhsBaseType); } switch (this.op) { case CodeBinaryOperatorType.Add: if ((parameters[0] != null) && (parameters[1] != null)) { literal = ArithmeticLiteral.MakeLiteral(this.lhsRootType, parameters[0]); literal2 = ArithmeticLiteral.MakeLiteral(this.rhsRootType, parameters[1]); obj2 = literal.Add(literal2); obj2 = Executor.AdjustType(obj2.GetType(), obj2, this.resultBaseType); if (this.resultIsNullable) { obj2 = Activator.CreateInstance(this.resultType, new object[] { obj2 }); } return(obj2); } return(null); case CodeBinaryOperatorType.Subtract: if ((parameters[0] != null) && (parameters[1] != null)) { literal = ArithmeticLiteral.MakeLiteral(this.resultRootType, Executor.AdjustType(this.lhsRootType, parameters[0], this.resultRootType)); literal2 = ArithmeticLiteral.MakeLiteral(this.resultRootType, Executor.AdjustType(this.rhsRootType, parameters[1], this.resultRootType)); obj2 = literal.Subtract(literal2); obj2 = Executor.AdjustType(obj2.GetType(), obj2, this.resultBaseType); if (this.resultIsNullable) { obj2 = Activator.CreateInstance(this.resultType, new object[] { obj2 }); } return(obj2); } return(null); case CodeBinaryOperatorType.ValueEquality: literal3 = Literal.MakeLiteral(this.lhsRootType, parameters[0]); literal4 = Literal.MakeLiteral(this.rhsRootType, parameters[1]); return(literal3.Equal(literal4)); case CodeBinaryOperatorType.LessThan: literal3 = Literal.MakeLiteral(this.lhsRootType, parameters[0]); literal4 = Literal.MakeLiteral(this.rhsRootType, parameters[1]); return(literal3.LessThan(literal4)); case CodeBinaryOperatorType.LessThanOrEqual: literal3 = Literal.MakeLiteral(this.lhsRootType, parameters[0]); literal4 = Literal.MakeLiteral(this.rhsRootType, parameters[1]); return(literal3.LessThanOrEqual(literal4)); case CodeBinaryOperatorType.GreaterThan: literal3 = Literal.MakeLiteral(this.lhsRootType, parameters[0]); literal4 = Literal.MakeLiteral(this.rhsRootType, parameters[1]); return(literal3.GreaterThan(literal4)); case CodeBinaryOperatorType.GreaterThanOrEqual: literal3 = Literal.MakeLiteral(this.lhsRootType, parameters[0]); literal4 = Literal.MakeLiteral(this.rhsRootType, parameters[1]); return(literal3.GreaterThanOrEqual(literal4)); } throw new RuleEvaluationException(string.Format(CultureInfo.CurrentCulture, Messages.BinaryOpNotSupported, new object[] { this.op.ToString() })); }