internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten) { CodeCastExpression expression2 = (CodeCastExpression)expression; if (isWritten) { ValidationError item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, new object[] { typeof(CodeCastExpression).ToString() }), 0x17a); item.UserData["ErrorObject"] = expression2; validation.Errors.Add(item); return(null); } if (expression2.Expression == null) { ValidationError error2 = new ValidationError(Messages.NullCastExpr, 0x53d); error2.UserData["ErrorObject"] = expression2; validation.Errors.Add(error2); return(null); } if (expression2.Expression is CodeTypeReferenceExpression) { ValidationError error = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CodeExpressionNotHandled, new object[] { expression2.Expression.GetType().FullName }), 0x548); error.UserData["ErrorObject"] = expression2.Expression; validation.AddError(error); return(null); } if (expression2.TargetType == null) { ValidationError error4 = new ValidationError(Messages.NullCastType, 0x53d); error4.UserData["ErrorObject"] = expression2; validation.Errors.Add(error4); return(null); } RuleExpressionInfo info = RuleExpressionWalker.Validate(validation, expression2.Expression, false); if (info == null) { return(null); } Type expressionType = info.ExpressionType; Type type = validation.ResolveType(expression2.TargetType); if (type == null) { return(null); } if (expressionType == typeof(NullLiteral)) { if (ConditionHelper.IsNonNullableValueType(type)) { ValidationError error5 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CastOfNullInvalid, new object[] { RuleDecompiler.DecompileType(type) }), 0x53d); error5.UserData["ErrorObject"] = expression2; validation.Errors.Add(error5); return(null); } } else { Type type3 = expressionType; if (ConditionHelper.IsNullableValueType(type3)) { type3 = type3.GetGenericArguments()[0]; } Type type4 = type; if (ConditionHelper.IsNullableValueType(type4)) { type4 = type4.GetGenericArguments()[0]; } bool flag = false; if (type3.IsValueType && type4.IsValueType) { if (type3.IsEnum) { flag = type4.IsEnum || IsNumeric(type4); } else if (type4.IsEnum) { flag = IsNumeric(type3); } else if (type3 == typeof(char)) { flag = IsNumeric(type4); } else if (type4 == typeof(char)) { flag = IsNumeric(type3); } else if (type3.IsPrimitive && type4.IsPrimitive) { try { Convert.ChangeType(Activator.CreateInstance(type3), type4, CultureInfo.CurrentCulture); flag = true; } catch (Exception) { flag = false; } } } if (!flag) { ValidationError error6; flag = RuleValidation.ExplicitConversionSpecified(expressionType, type, out error6); if (error6 != null) { error6.UserData["ErrorObject"] = expression2; validation.Errors.Add(error6); return(null); } } if (!flag) { ValidationError error7 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CastIncompatibleTypes, new object[] { RuleDecompiler.DecompileType(expressionType), RuleDecompiler.DecompileType(type) }), 0x53d); error7.UserData["ErrorObject"] = expression2; validation.Errors.Add(error7); return(null); } } return(new RuleExpressionInfo(type)); }
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); }
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); }
public EnumOperationMethodInfo(Type lhs, CodeBinaryOperatorType operation, Type rhs, bool isZero) { this.op = operation; this.expectedParameters = new ParameterInfo[] { new SimpleParameterInfo(lhs), new SimpleParameterInfo(rhs) }; bool flag = ConditionHelper.IsNullableValueType(lhs); bool flag2 = ConditionHelper.IsNullableValueType(rhs); this.lhsBaseType = flag ? Nullable.GetUnderlyingType(lhs) : lhs; this.rhsBaseType = flag2 ? Nullable.GetUnderlyingType(rhs) : rhs; if (this.lhsBaseType.IsEnum) { this.lhsRootType = EnumHelper.GetUnderlyingType(this.lhsBaseType); } else { this.lhsRootType = this.lhsBaseType; } if (this.rhsBaseType.IsEnum) { this.rhsRootType = EnumHelper.GetUnderlyingType(this.rhsBaseType); } else { this.rhsRootType = this.rhsBaseType; } switch (this.op) { case CodeBinaryOperatorType.Add: if (!this.lhsBaseType.IsEnum || !rhs.IsEnum) { if (this.lhsBaseType.IsEnum) { this.resultBaseType = this.lhsBaseType; } else { this.resultBaseType = this.rhsBaseType; } break; } this.resultBaseType = this.lhsRootType; break; case CodeBinaryOperatorType.Subtract: if (!this.rhsBaseType.IsEnum || !this.lhsBaseType.IsEnum) { if (this.lhsBaseType.IsEnum) { this.resultRootType = this.lhsRootType; if (isZero && (this.rhsBaseType != this.lhsRootType)) { this.resultBaseType = this.lhsRootType; } else { this.resultBaseType = this.lhsBaseType; } } else { this.resultRootType = this.rhsRootType; if (isZero) { this.resultBaseType = this.rhsRootType; } else { this.resultBaseType = this.rhsBaseType; } } } else { this.resultRootType = this.rhsRootType; this.resultBaseType = this.rhsRootType; } this.resultIsNullable = flag || flag2; this.resultType = this.resultIsNullable ? typeof(Nullable <>).MakeGenericType(new Type[] { this.resultBaseType }) : this.resultBaseType; return; case CodeBinaryOperatorType.ValueEquality: case CodeBinaryOperatorType.LessThan: case CodeBinaryOperatorType.LessThanOrEqual: case CodeBinaryOperatorType.GreaterThan: case CodeBinaryOperatorType.GreaterThanOrEqual: this.resultType = typeof(bool); return; case CodeBinaryOperatorType.BitwiseOr: case CodeBinaryOperatorType.BitwiseAnd: case CodeBinaryOperatorType.BooleanOr: case CodeBinaryOperatorType.BooleanAnd: return; default: return; } this.resultIsNullable = flag || flag2; this.resultType = this.resultIsNullable ? typeof(Nullable <>).MakeGenericType(new Type[] { this.resultBaseType }) : this.resultBaseType; }