Example #1
0
        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);
        }
Example #2
0
        internal override RuleExpressionInfo Validate(CodeExpression expression, RuleValidation validation, bool isWritten)
        {
            CodeArrayCreateExpression newParent = (CodeArrayCreateExpression)expression;

            if (isWritten)
            {
                ValidationError item = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.CannotWriteToExpression, new object[] { typeof(CodeObjectCreateExpression).ToString() }), 0x17a);
                item.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(item);
                return(null);
            }
            if (newParent.CreateType == null)
            {
                ValidationError error2 = new ValidationError(Messages.NullTypeType, 0x53d);
                error2.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(error2);
                return(null);
            }
            Type lhsType = validation.ResolveType(newParent.CreateType);

            if (lhsType == null)
            {
                return(null);
            }
            if (lhsType.IsArray)
            {
                ValidationError error3 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.ArrayTypeInvalid, new object[] { lhsType.Name }), 0x53d);
                error3.UserData["ErrorObject"] = newParent;
                validation.Errors.Add(error3);
                return(null);
            }
            try
            {
                if (!validation.PushParentExpression(newParent))
                {
                    return(null);
                }
                if (newParent.Size < 0)
                {
                    ValidationError error4 = new ValidationError(Messages.ArraySizeInvalid, 0x53d);
                    error4.UserData["ErrorObject"] = newParent;
                    validation.Errors.Add(error4);
                    return(null);
                }
                if (newParent.SizeExpression != null)
                {
                    RuleExpressionInfo info = RuleExpressionWalker.Validate(validation, newParent.SizeExpression, false);
                    if (info == null)
                    {
                        return(null);
                    }
                    if (((info.ExpressionType != typeof(int)) && (info.ExpressionType != typeof(uint))) && ((info.ExpressionType != typeof(long)) && (info.ExpressionType != typeof(ulong))))
                    {
                        ValidationError error5 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.ArraySizeTypeInvalid, new object[] { info.ExpressionType.Name }), 0x53d);
                        error5.UserData["ErrorObject"] = newParent;
                        validation.Errors.Add(error5);
                        return(null);
                    }
                }
                bool flag = false;
                for (int i = 0; i < newParent.Initializers.Count; i++)
                {
                    CodeExpression expression3 = newParent.Initializers[i];
                    if (expression3 == null)
                    {
                        ValidationError error6 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.MissingInitializer, new object[] { lhsType.Name }), 0x53d);
                        error6.UserData["ErrorObject"] = newParent;
                        validation.Errors.Add(error6);
                        return(null);
                    }
                    RuleExpressionInfo info2 = RuleExpressionWalker.Validate(validation, expression3, false);
                    if (info2 == null)
                    {
                        flag = true;
                    }
                    else
                    {
                        ValidationError error7;
                        if (!RuleValidation.StandardImplicitConversion(info2.ExpressionType, lhsType, expression3, out error7))
                        {
                            if (error7 != null)
                            {
                                error7.UserData["ErrorObject"] = newParent;
                                validation.Errors.Add(error7);
                            }
                            error7 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.InitializerMismatch, new object[] { i, lhsType.Name }), 0x545);
                            error7.UserData["ErrorObject"] = newParent;
                            validation.Errors.Add(error7);
                            return(null);
                        }
                    }
                }
                if (flag)
                {
                    return(null);
                }
                double size = -1.0;
                if (newParent.SizeExpression != null)
                {
                    CodePrimitiveExpression sizeExpression = newParent.SizeExpression as CodePrimitiveExpression;
                    if ((sizeExpression != null) && (sizeExpression.Value != null))
                    {
                        size = (double)Executor.AdjustType(sizeExpression.Value.GetType(), sizeExpression.Value, typeof(double));
                    }
                    if (newParent.Size > 0)
                    {
                        ValidationError error8 = new ValidationError(Messages.ArraySizeBoth, 0x53d);
                        error8.UserData["ErrorObject"] = newParent;
                        validation.Errors.Add(error8);
                        return(null);
                    }
                }
                else if (newParent.Size > 0)
                {
                    size = newParent.Size;
                }
                if ((size >= 0.0) && (newParent.Initializers.Count > size))
                {
                    ValidationError error9 = new ValidationError(string.Format(CultureInfo.CurrentCulture, Messages.InitializerCountMismatch, new object[] { newParent.Initializers.Count, size }), 0x545);
                    error9.UserData["ErrorObject"] = newParent;
                    validation.Errors.Add(error9);
                    return(null);
                }
            }
            finally
            {
                validation.PopParentExpression();
            }
            return(new RuleExpressionInfo(lhsType.MakeArrayType()));
        }
Example #3
0
        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);
        }