private static object AddDecimal(IConvertible Left, IConvertible Right)
 {
     decimal num = Left.ToDecimal(null);
     decimal num2 = Right.ToDecimal(null);
     try
     {
         return decimal.Add(num, num2);
     }
     catch (OverflowException)
     {
         return (Convert.ToDouble(num) + Convert.ToDouble(num2));
     }
 }
 private static object AddDecimal(IConvertible conv1, IConvertible conv2)
 {
     decimal num;
     if (conv1 != null)
     {
         num = conv1.ToDecimal(null);
     }
     decimal num2 = conv2.ToDecimal(null);
     try
     {
         return decimal.Add(num, num2);
     }
     catch (OverflowException)
     {
         return (Convert.ToDouble(num) + Convert.ToDouble(num2));
     }
 }
        internal static uint ToUint32(object value, IConvertible ic)
        {
            switch (GetTypeCode(value, ic))
            {
                case TypeCode.Empty:
                    return 0;

                case TypeCode.Object:
                case TypeCode.DateTime:
                {
                    object obj2 = ToPrimitive(value, PreferredType.Number, ref ic);
                    if (obj2 == value)
                    {
                        return 0;
                    }
                    return ToUint32(obj2, ic);
                }
                case TypeCode.DBNull:
                    return 0;

                case TypeCode.Boolean:
                    if (ic.ToBoolean(null))
                    {
                        return 1;
                    }
                    return 0;

                case TypeCode.Char:
                    return ic.ToChar(null);

                case TypeCode.SByte:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                    return (uint) ic.ToInt64(null);

                case TypeCode.Byte:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                    return ic.ToUInt32(null);

                case TypeCode.UInt64:
                    return (uint) ic.ToUInt64(null);

                case TypeCode.Single:
                case TypeCode.Double:
                    return (uint) Runtime.DoubleToInt64(ic.ToDouble(null));

                case TypeCode.Decimal:
                    return (uint) Runtime.UncheckedDecimalToInt64(ic.ToDecimal(null));

                case TypeCode.String:
                    return (uint) Runtime.DoubleToInt64(ToNumber(ic.ToString(null)));
            }
            return 0;
        }
Example #4
0
        public static object ChangeType(object value, Type conversionType, IFormatProvider provider)
        {
            if (conversionType == null)
            {
                throw new ArgumentNullException("conversionType");
            }
            if (value == null)
            {
                if (conversionType.IsValueType)
                {
                    throw new InvalidCastException("InvalidCast_CannotCastNullToValueType");
                }
                return(null);
            }
            IConvertible convertible1 = value as IConvertible;

            if (convertible1 == null)
            {
                if (value.GetType() != conversionType)
                {
                    throw new InvalidCastException("InvalidCast_IConvertible");
                }
                return(value);
            }

            if (typeof(System.Enum).IsAssignableFrom(conversionType) == true)
            {
                return(convertible1.ToInt32(provider));
            }

            #region - bool -
            if (conversionType == typeof(bool))
            {
                return(convertible1.ToBoolean(provider));
            }
            #endregion

            #region - char -
            if (conversionType == typeof(char))
            {
                return(convertible1.ToChar(provider));
            }
            #endregion

            #region - sbyte -
            if (conversionType == typeof(sbyte))
            {
                return(convertible1.ToSByte(provider));
            }
            #endregion

            #region - byte -
            if (conversionType == typeof(byte))
            {
                return(convertible1.ToByte(provider));
            }
            #endregion

            #region - short -
            if (conversionType == typeof(short))
            {
                return(convertible1.ToInt16(provider));
            }
            #endregion

            #region - ushort -
            if (conversionType == typeof(ushort))
            {
                return(convertible1.ToUInt16(provider));
            }
            #endregion

            #region - int -
            if (conversionType == typeof(int))
            {
                return(convertible1.ToInt32(provider));
            }
            #endregion

            #region - uint -
            if (conversionType == typeof(uint))
            {
                return(convertible1.ToUInt32(provider));
            }
            #endregion

            #region - long -
            if (conversionType == typeof(long))
            {
                return(convertible1.ToInt64(provider));
            }
            #endregion

            #region - ulong -
            if (conversionType == typeof(ulong))
            {
                return(convertible1.ToUInt64(provider));
            }
            #endregion

            #region - float -
            if (conversionType == typeof(float))
            {
                return(convertible1.ToSingle(provider));
            }
            #endregion

            #region - double -
            if (conversionType == typeof(double))
            {
                return(convertible1.ToDouble(provider));
            }
            #endregion

            #region - decimal -
            if (conversionType == typeof(decimal))
            {
                return(convertible1.ToDecimal(provider));
            }
            #endregion

            #region - DateTime -
            if (conversionType == typeof(DateTime))
            {
                try
                {
                    return(convertible1.ToDateTime(provider));
                }
                catch (Exception ex)
                {
                    throw new System.FormatException(ex.Message + "\n" + value.ToString());
                }
            }
            #endregion

            if (conversionType == typeof(Nullable <DateTime>))
            {
                var _value = value as Nullable <DateTime>;
                if (_value.HasValue)
                {
                    return(_value.Value);
                }
                return(_value);
            }

            #region - string -
            if (conversionType == typeof(string))
            {
                return(convertible1.ToString(provider));
            }
            #endregion

            #region - object -
            if (conversionType == typeof(object))
            {
                return(value);
            }
            #endregion

            #region = Uri =
            if (conversionType == typeof(Uri))
            {
                string s = value.ToString();
                return(new Uri(s));
            }
            #endregion



            return(convertible1.ToType(conversionType, provider));
        }
Example #5
0
        private static int CompareConvertible(IConvertible comparisonValue, IConvertible predicateValue)
        {
//            try
//            {
            TypeCode comparisonType = comparisonValue.GetTypeCode();

            switch (predicateValue.GetTypeCode())
            {
            case TypeCode.DBNull:
                return(comparisonType == TypeCode.DBNull ? 0 : 1);

            case TypeCode.Boolean:
                if (comparisonType != TypeCode.Boolean)
                {
                    return(1);
                }
                return(predicateValue.ToBoolean(CultureInfo.CurrentCulture)
                       .CompareTo(comparisonValue.ToBoolean(CultureInfo.CurrentCulture)));

            case TypeCode.Char:
                return(predicateValue.ToChar(CultureInfo.CurrentCulture)
                       .CompareTo(comparisonValue.ToChar(CultureInfo.CurrentCulture)));

            case TypeCode.SByte:
                return(predicateValue.ToSByte(CultureInfo.CurrentCulture)
                       .CompareTo(comparisonValue.ToSByte(CultureInfo.CurrentCulture)));

            case TypeCode.Byte:
                return(predicateValue.ToByte(CultureInfo.CurrentCulture)
                       .CompareTo(comparisonValue.ToByte(CultureInfo.CurrentCulture)));

            case TypeCode.Int16:
                return(predicateValue.ToInt16(NumberFormatInfo.CurrentInfo)
                       .CompareTo(comparisonValue.ToInt16(NumberFormatInfo.CurrentInfo)));

            case TypeCode.UInt16:
                return(predicateValue.ToUInt16(NumberFormatInfo.CurrentInfo)
                       .CompareTo(comparisonValue.ToUInt16(NumberFormatInfo.CurrentInfo)));

            case TypeCode.Int32:
                return(predicateValue.ToInt32(NumberFormatInfo.CurrentInfo)
                       .CompareTo(comparisonValue.ToInt32(NumberFormatInfo.CurrentInfo)));

            case TypeCode.UInt32:
                return(predicateValue.ToUInt32(NumberFormatInfo.CurrentInfo)
                       .CompareTo(comparisonValue.ToUInt32(NumberFormatInfo.CurrentInfo)));

            case TypeCode.Int64:
                return(predicateValue.ToInt64(NumberFormatInfo.CurrentInfo)
                       .CompareTo(comparisonValue.ToInt64(NumberFormatInfo.CurrentInfo)));

            case TypeCode.UInt64:
                return(predicateValue.ToUInt64(NumberFormatInfo.CurrentInfo)
                       .CompareTo(comparisonValue.ToUInt64(NumberFormatInfo.CurrentInfo)));

            case TypeCode.Single:
                return(predicateValue.ToSingle(NumberFormatInfo.CurrentInfo)
                       .CompareTo(comparisonValue.ToSingle(NumberFormatInfo.CurrentInfo)));

            case TypeCode.Double:
                return(predicateValue.ToDouble(NumberFormatInfo.CurrentInfo)
                       .CompareTo(comparisonValue.ToDouble(NumberFormatInfo.CurrentInfo)));

            case TypeCode.Decimal:
                return(predicateValue.ToDecimal(NumberFormatInfo.CurrentInfo)
                       .CompareTo(comparisonValue.ToDecimal(NumberFormatInfo.CurrentInfo)));

            case TypeCode.DateTime:
                return(predicateValue.ToDateTime(DateTimeFormatInfo.CurrentInfo)
                       .CompareTo(comparisonValue.ToDateTime(DateTimeFormatInfo.CurrentInfo)));

            default:
                return(string.CompareOrdinal(predicateValue.ToString(CultureInfo.CurrentCulture), comparisonValue.ToString(CultureInfo.CurrentCulture)));
            }
//            }
//            catch (FormatException)
//            {
//                return 1;
//            }
        }
Example #6
0
        private object SmallestDecimal(object constant)
        {
            if (null == constant)
            {
                return(0d);
            }
            else
            {
                string sval = (constant as string);
                if (null != sval)
                {
                    decimal r12;
                    if (decimal.TryParse(sval, NumberStyles.Number, NumberFormatInfo.InvariantInfo, out r12))
                    {
                        return(r12);
                    }

                    double r8;
                    if (double.TryParse(sval, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.InvariantInfo, out r8))
                    {
                        return(r8);
                    }
                }
                else
                {
                    IConvertible convertible = (constant as IConvertible);
                    if (null != convertible)
                    {
                        try
                        {
                            return(convertible.ToDecimal(NumberFormatInfo.InvariantInfo));
                        }
                        catch (System.ArgumentException e)
                        {
                            ExceptionBuilder.TraceExceptionWithoutRethrow(e);
                        }
                        catch (System.FormatException e)
                        {
                            ExceptionBuilder.TraceExceptionWithoutRethrow(e);
                        }
                        catch (System.InvalidCastException e)
                        {
                            ExceptionBuilder.TraceExceptionWithoutRethrow(e);
                        }
                        catch (System.OverflowException e)
                        {
                            ExceptionBuilder.TraceExceptionWithoutRethrow(e);
                        }
                        try
                        {
                            return(convertible.ToDouble(NumberFormatInfo.InvariantInfo));
                        }
                        catch (System.ArgumentException e)
                        {
                            ExceptionBuilder.TraceExceptionWithoutRethrow(e);
                        }
                        catch (System.FormatException e)
                        {
                            ExceptionBuilder.TraceExceptionWithoutRethrow(e);
                        }
                        catch (System.InvalidCastException e)
                        {
                            ExceptionBuilder.TraceExceptionWithoutRethrow(e);
                        }
                        catch (System.OverflowException e)
                        {
                            ExceptionBuilder.TraceExceptionWithoutRethrow(e);
                        }
                    }
                }
            }
            return(constant);
        }
 public static Literal DoDiv(IConvertible ic1, IConvertible ic2, TypeCode code1, TypeCode code2, BinaryExpression binaryExpression){
   TypeNode type = SystemTypes.Object;
   object val = null;
   switch(code1){
     case TypeCode.SByte:
     case TypeCode.Int16:
     case TypeCode.Int32:
       int i = ic1.ToInt32(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
         case TypeCode.Int32: 
         case TypeCode.Byte:
         case TypeCode.Char:
         case TypeCode.UInt16:
           val = i / ic2.ToInt32(null); 
           type = SystemTypes.Int32;
           break;
         case TypeCode.Int64: 
         case TypeCode.UInt32: 
         case TypeCode.UInt64: 
           val = i / ic2.ToInt64(null); 
           type = SystemTypes.Int64;
           break;
         case TypeCode.Single: 
           val = i / ic2.ToSingle(null); 
           type = SystemTypes.Single;
           break;
         case TypeCode.Double: 
           val = i / ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = i / ic2.ToDecimal(null);
           type = SystemTypes.Decimal;
           break;
         default: return null;
       }
       break;
     case TypeCode.Byte:
     case TypeCode.UInt16:
       ushort us = ic1.ToUInt16(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
         case TypeCode.Int32: 
           val = us / ic2.ToInt32(null); 
           type = SystemTypes.Int32;
           break;
         case TypeCode.Byte:
         case TypeCode.UInt16:
         case TypeCode.Char:
         case TypeCode.UInt32: 
           val = us / ic2.ToUInt32(null); 
           type = SystemTypes.UInt32;
           break;
         case TypeCode.Int64: 
           val = us / ic2.ToInt64(null); 
           type = SystemTypes.Int64;
           break;
         case TypeCode.UInt64: 
           val = us / ic2.ToUInt64(null); 
           type = SystemTypes.UInt64;
           break;
         case TypeCode.Single: 
         case TypeCode.Double: 
           val = us / ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = us / ic2.ToDecimal(null);
           type = SystemTypes.Decimal;
           break;
         default: return null;
       }
       break;
     case TypeCode.UInt32:
       uint ui = ic1.ToUInt32(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
         case TypeCode.Int32: 
         case TypeCode.Int64: 
           val = ui / ic2.ToInt64(null); 
           type = SystemTypes.Int64;
           break;
         case TypeCode.Byte:
         case TypeCode.UInt16:
         case TypeCode.Char:
         case TypeCode.UInt32: 
           val = ui / ic2.ToUInt32(null); 
           type = SystemTypes.UInt32;
           break;
         case TypeCode.UInt64: 
           val = ui / ic2.ToUInt64(null); 
           type = SystemTypes.UInt64;
           break;
         case TypeCode.Single: 
           val = ui / ic2.ToSingle(null); 
           type = SystemTypes.Single;
           break;
         case TypeCode.Double: 
           val = ui / ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = ui / ic2.ToDecimal(null);
           type = SystemTypes.Decimal;
           break;
         default: return null;
       }
       break;
     case TypeCode.Int64:
       long l = ic1.ToInt64(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
         case TypeCode.Int32: 
         case TypeCode.Int64: 
           val = l / ic2.ToInt64(null); 
           type = SystemTypes.Int64;
           break;
         case TypeCode.Byte:
         case TypeCode.UInt16:
         case TypeCode.Char:
         case TypeCode.UInt32: 
           val = l / ic2.ToUInt32(null); 
           type = SystemTypes.UInt32;
           break;
         case TypeCode.UInt64: 
           val = l / (long)ic2.ToUInt64(null); 
           type = SystemTypes.UInt64;
           break;
         case TypeCode.Single: 
           val = l / ic2.ToSingle(null); 
           type = SystemTypes.Single;
           break;
         case TypeCode.Double: 
           val = l / ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = l / ic2.ToDecimal(null);
           type = SystemTypes.Decimal;
           break;
         default: return null;
       }
       break;
     case TypeCode.UInt64:
       ulong ul = ic1.ToUInt64(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
         case TypeCode.Int32: 
         case TypeCode.Byte:
         case TypeCode.UInt16:
         case TypeCode.Char:
         case TypeCode.UInt32: 
           val = ul / ic2.ToUInt32(null); 
           type = SystemTypes.UInt32;
           break;
         case TypeCode.Int64: 
         case TypeCode.UInt64: 
           val = ul / ic2.ToUInt64(null); 
           type = SystemTypes.UInt64;
           break;
         case TypeCode.Single: 
           val = ul / ic2.ToSingle(null); 
           type = SystemTypes.Single;
           break;
         case TypeCode.Double: 
           val = ul / ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = ul / ic2.ToDecimal(null);
           type = SystemTypes.Decimal;
           break;
         default: return null;
       }
       break;
     case TypeCode.Single:
       float f = ic1.ToSingle(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
           val = f / ic2.ToInt16(null);
           type = SystemTypes.Single;
           break;
         case TypeCode.Int32: 
         case TypeCode.Int64: 
           val = f / (double)ic2.ToInt64(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Byte:
         case TypeCode.Char:
         case TypeCode.UInt16:
           val = f / ic2.ToUInt16(null); 
           type = SystemTypes.Single;
           break;
         case TypeCode.UInt32: 
         case TypeCode.UInt64: 
           val = f / (double)ic2.ToUInt64(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Single: 
           val = f / ic2.ToSingle(null); 
           type = SystemTypes.Single;
           break;
         case TypeCode.Double: 
           val = f / ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = f / (double)ic2.ToDecimal(null);
           type = SystemTypes.Double;
           break;
         default: return null;
       }
       break;
     case TypeCode.Double:
       double d = ic1.ToDouble(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
         case TypeCode.Int32: 
         case TypeCode.Int64: 
           val = d / ic2.ToInt64(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Byte:
         case TypeCode.Char:
         case TypeCode.UInt16:
         case TypeCode.UInt32: 
         case TypeCode.UInt64: 
           val = d / ic2.ToUInt64(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Single: 
         case TypeCode.Double: 
           val = d / ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = d / (double)ic2.ToDecimal(null);
           type = SystemTypes.Double;
           break;
         default: return null;
       }
       break;
     case TypeCode.Decimal:
       decimal dec = ic1.ToDecimal(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Byte:
         case TypeCode.Int16:
         case TypeCode.UInt16:
         case TypeCode.Char:
         case TypeCode.Int32: 
           val = dec / ic2.ToInt32(null); 
           type = SystemTypes.Decimal;
           break;
         case TypeCode.UInt32: 
         case TypeCode.Int64: 
         case TypeCode.UInt64: 
           val = dec / ic2.ToInt64(null); 
           type = SystemTypes.Decimal;
           break;
         case TypeCode.Decimal:
           val = dec / ic2.ToDecimal(null);
           type = SystemTypes.Decimal;
           break;
         default: return null;
       }
       break;
     default: return null;
   }
   return new Literal(val, type, binaryExpression.SourceContext);
 }
Example #8
0
        internal static Object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) {
            Contract.Requires(value != null, "[Convert.DefaultToType]value!=null");
            if (targetType==null) {
                throw new ArgumentNullException("targetType");
            }
            Contract.EndContractBlock();

            RuntimeType rtTargetType = targetType as RuntimeType;

            if (rtTargetType != null)
            {
                if (value.GetType() == targetType)
                {
                    return value;
                }

                if (rtTargetType == ConvertTypes[(int)TypeCode.Boolean])
                    return value.ToBoolean(provider);
                if (rtTargetType == ConvertTypes[(int)TypeCode.Char])
                    return value.ToChar(provider);
                if (rtTargetType == ConvertTypes[(int)TypeCode.SByte])
                    return value.ToSByte(provider);
                if (rtTargetType == ConvertTypes[(int)TypeCode.Byte])
                    return value.ToByte(provider);
                if (rtTargetType == ConvertTypes[(int)TypeCode.Int16])
                    return value.ToInt16(provider);
                if (rtTargetType == ConvertTypes[(int)TypeCode.UInt16])
                    return value.ToUInt16(provider);
                if (rtTargetType == ConvertTypes[(int)TypeCode.Int32])
                    return value.ToInt32(provider);
                if (rtTargetType == ConvertTypes[(int)TypeCode.UInt32])
                    return value.ToUInt32(provider);
                if (rtTargetType == ConvertTypes[(int)TypeCode.Int64])
                    return value.ToInt64(provider);
                if (rtTargetType == ConvertTypes[(int)TypeCode.UInt64])
                    return value.ToUInt64(provider);
                if (rtTargetType == ConvertTypes[(int)TypeCode.Single])
                    return value.ToSingle(provider);
                if (rtTargetType == ConvertTypes[(int)TypeCode.Double])
                    return value.ToDouble(provider);
                if (rtTargetType == ConvertTypes[(int)TypeCode.Decimal])
                    return value.ToDecimal(provider);
                if (rtTargetType == ConvertTypes[(int)TypeCode.DateTime])
                    return value.ToDateTime(provider);
                if (rtTargetType == ConvertTypes[(int)TypeCode.String])
                    return value.ToString(provider);
                if (rtTargetType == ConvertTypes[(int)TypeCode.Object])
                    return (Object)value;
                //  Need to special case Enum because typecode will be underlying type, e.g. Int32
                if (rtTargetType == EnumType)
                    return (Enum)value;
                if (rtTargetType == ConvertTypes[(int)TypeCode.DBNull])
                    throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull"));
                if (rtTargetType == ConvertTypes[(int)TypeCode.Empty])
                    throw new InvalidCastException(Environment.GetResourceString("InvalidCast_Empty"));
            }

            throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", value.GetType().FullName, targetType.FullName));
        }
        private static bool TryConvertByIConvertibleImplementation(object value, Type destinationType, IFormatProvider formatProvider, ref object result)
        {
            IConvertible convertible = value as IConvertible;

            if (convertible != null)
            {
                try
                {
                    if (destinationType == typeof(Boolean))
                    {
                        result = convertible.ToBoolean(formatProvider);
                        return(true);
                    }
                    if (destinationType == typeof(Byte))
                    {
                        result = convertible.ToByte(formatProvider);
                        return(true);
                    }
                    if (destinationType == typeof(Char))
                    {
                        result = convertible.ToChar(formatProvider);
                        return(true);
                    }
                    if (destinationType == typeof(DateTime))
                    {
                        result = convertible.ToDateTime(formatProvider);
                        return(true);
                    }
                    if (destinationType == typeof(Decimal))
                    {
                        result = convertible.ToDecimal(formatProvider);
                        return(true);
                    }
                    if (destinationType == typeof(Double))
                    {
                        result = convertible.ToDouble(formatProvider);
                        return(true);
                    }
                    if (destinationType == typeof(Int16))
                    {
                        result = convertible.ToInt16(formatProvider);
                        return(true);
                    }
                    if (destinationType == typeof(Int32))
                    {
                        result = convertible.ToInt32(formatProvider);
                        return(true);
                    }
                    if (destinationType == typeof(Int64))
                    {
                        result = convertible.ToInt64(formatProvider);
                        return(true);
                    }
                    if (destinationType == typeof(SByte))
                    {
                        result = convertible.ToSByte(formatProvider);
                        return(true);
                    }
                    if (destinationType == typeof(Single))
                    {
                        result = convertible.ToSingle(formatProvider);
                        return(true);
                    }
                    if (destinationType == typeof(UInt16))
                    {
                        result = convertible.ToUInt16(formatProvider);
                        return(true);
                    }
                    if (destinationType == typeof(UInt32))
                    {
                        result = convertible.ToUInt32(formatProvider);
                        return(true);
                    }
                    if (destinationType == typeof(UInt64))
                    {
                        result = convertible.ToUInt64(formatProvider);
                        return(true);
                    }
                }
                catch
                {
                    return(false);
                }
            }
            return(false);
        }
 private static int DecimalToInteger(IConvertible ValueInterface)
 {
     return Convert.ToInt32(ValueInterface.ToDecimal(null));
 }
 private static object ModDecimal(IConvertible Left, IConvertible Right)
 {
     decimal num = Left.ToDecimal(null);
     decimal num2 = Right.ToDecimal(null);
     return decimal.Remainder(num, num2);
 }
 private static object DivideDecimal(IConvertible Left, IConvertible Right)
 {
     decimal num = Left.ToDecimal(null);
     decimal num2 = Right.ToDecimal(null);
     try
     {
         return decimal.Divide(num, num2);
     }
     catch (OverflowException)
     {
         return (Convert.ToSingle(num) / Convert.ToSingle(num2));
     }
 }
 private static CompareClass CompareDecimal(IConvertible Left, IConvertible Right)
 {
     int num = decimal.Compare(Left.ToDecimal(null), Right.ToDecimal(null));
     if (num == 0)
     {
         return CompareClass.Equal;
     }
     if (num > 0)
     {
         return CompareClass.Greater;
     }
     return CompareClass.Less;
 }
 private static bool DecimalToBoolean(IConvertible ValueInterface)
 {
     return Convert.ToBoolean(ValueInterface.ToDecimal(null));
 }
 internal static bool JScriptStrictEquals(Object v1, Object v2, IConvertible ic1, IConvertible ic2, TypeCode t1, TypeCode t2, bool checkForDebuggerObjects){
   switch (t1){
     case TypeCode.Empty: return t2 == TypeCode.Empty;
     case TypeCode.Object:
       if (v1 == v2) return true;
       if (v1 is Missing || v1 is System.Reflection.Missing) v1 = null;
       if (v1 == v2) return true;
       if (v2 is Missing || v2 is System.Reflection.Missing) v2 = null;
       return v1 == v2;
     case TypeCode.DBNull: return t2 == TypeCode.DBNull;
     case TypeCode.Boolean: return t2 == TypeCode.Boolean && ic1.ToBoolean(null) == ic2.ToBoolean(null);
     
     case TypeCode.Char: 
       Char ch = ic1.ToChar(null);
       switch(t2){
         case TypeCode.Char: return ch == ic2.ToChar(null);
         case TypeCode.SByte:
         case TypeCode.Byte:
         case TypeCode.Int16:
         case TypeCode.UInt16:
         case TypeCode.Int32:
         case TypeCode.UInt32:
         case TypeCode.Int64: return ch == ic2.ToInt64(null);
         case TypeCode.UInt64: return ch == ic2.ToUInt64(null);
         case TypeCode.Single: 
         case TypeCode.Double: return ch == ic2.ToDouble(null);
         case TypeCode.Decimal: return ((Decimal)(int)ch) == ic2.ToDecimal(null);
         case TypeCode.String:
           String str = ic2.ToString(null);
           return str.Length == 1 && ch == str[0];
       }
       return false;
     
     case TypeCode.SByte:
       SByte sb1 = ic1.ToSByte(null);
       switch (t2){
         case TypeCode.Char: return sb1 == ic2.ToChar(null);
         case TypeCode.SByte:
         case TypeCode.Byte:
         case TypeCode.Int16:
         case TypeCode.UInt16:
         case TypeCode.Int32:
         case TypeCode.UInt32:
         case TypeCode.Int64: return sb1 == ic2.ToInt64(null);
         case TypeCode.UInt64: return sb1 >= 0 && ((UInt64)sb1) == ic2.ToUInt64(null);
         case TypeCode.Single: return sb1 == ic2.ToSingle(null);
         case TypeCode.Double: return sb1 == ic2.ToDouble(null);
         case TypeCode.Decimal: return ((Decimal)sb1) == ic2.ToDecimal(null);
       }
       return false;
       
     case TypeCode.Byte:
       Byte b1 = ic1.ToByte(null);
       switch (t2){
         case TypeCode.Char: return b1 == ic2.ToChar(null);
         case TypeCode.SByte:
         case TypeCode.Byte:
         case TypeCode.Int16:
         case TypeCode.UInt16:
         case TypeCode.Int32:
         case TypeCode.UInt32:
         case TypeCode.Int64: return b1 == ic2.ToInt64(null);
         case TypeCode.UInt64: return b1 == ic2.ToUInt64(null);
         case TypeCode.Single: return b1 == ic2.ToSingle(null);
         case TypeCode.Double: return b1 == ic2.ToDouble(null);
         case TypeCode.Decimal: return ((Decimal)b1) == ic2.ToDecimal(null);
       }
       return false;
       
     case TypeCode.Int16:
       Int16 s1 = ic1.ToInt16(null);
       switch (t2){
         case TypeCode.Char: return s1 == ic2.ToChar(null);
         case TypeCode.SByte:
         case TypeCode.Byte:
         case TypeCode.Int16:
         case TypeCode.UInt16:
         case TypeCode.Int32:
         case TypeCode.UInt32:
         case TypeCode.Int64: return s1 == ic2.ToInt64(null);
         case TypeCode.UInt64: return s1 >= 0 && ((UInt64)s1) == ic2.ToUInt64(null);
         case TypeCode.Single: return s1 == ic2.ToSingle(null);
         case TypeCode.Double: return s1 == ic2.ToDouble(null);
         case TypeCode.Decimal: return ((Decimal)s1) == ic2.ToDecimal(null);
      }
       return false;
       
     case TypeCode.UInt16:
       UInt16 us1 = ic1.ToUInt16(null);
       switch (t2){
         case TypeCode.Char: return us1 == ic2.ToChar(null);
         case TypeCode.SByte:
         case TypeCode.Byte:
         case TypeCode.Int16:
         case TypeCode.UInt16:
         case TypeCode.Int32:
         case TypeCode.UInt32:
         case TypeCode.Int64: return us1 == ic2.ToInt64(null);
         case TypeCode.UInt64: return us1 == ic2.ToUInt64(null);
         case TypeCode.Single: return us1 == ic2.ToSingle(null);
         case TypeCode.Double: return us1 == ic2.ToDouble(null);
         case TypeCode.Decimal: return ((Decimal)us1) == ic2.ToDecimal(null);
       }
       return false;
       
     case TypeCode.Int32:
       Int32 i1 = ic1.ToInt32(null);
       switch (t2){
         case TypeCode.Char: return i1 == ic2.ToChar(null);
         case TypeCode.SByte:
         case TypeCode.Byte:
         case TypeCode.Int16:
         case TypeCode.UInt16:
         case TypeCode.Int32:
         case TypeCode.UInt32:
         case TypeCode.Int64: return i1 == ic2.ToInt64(null);
         case TypeCode.UInt64: return i1 >= 0 && ((UInt64)i1) == ic2.ToUInt64(null);
         case TypeCode.Single: return i1 == ic2.ToSingle(null);
         case TypeCode.Double: return i1 == ic2.ToDouble(null);
         case TypeCode.Decimal: return ((Decimal)i1) == ic2.ToDecimal(null);
       }
       return false;
       
     case TypeCode.UInt32:
       UInt32 ui1 = ic1.ToUInt32(null);
       switch (t2){
         case TypeCode.Char: return ui1 == ic2.ToChar(null);
         case TypeCode.SByte:
         case TypeCode.Byte:
         case TypeCode.Int16:
         case TypeCode.UInt16:
         case TypeCode.Int32:
         case TypeCode.UInt32:
         case TypeCode.Int64: return ui1 == ic2.ToInt64(null);
         case TypeCode.UInt64: return ui1 == ic2.ToUInt64(null);
         case TypeCode.Single: return ui1 == ic2.ToSingle(null);
         case TypeCode.Double: return ui1 == ic2.ToDouble(null);
         case TypeCode.Decimal: return ((Decimal)ui1) == ic2.ToDecimal(null);
       }
       return false;
       
     case TypeCode.Int64:
       Int64 l1 = ic1.ToInt64(null);
       switch (t2){
         case TypeCode.Char: return l1 == ic2.ToChar(null);
         case TypeCode.SByte:
         case TypeCode.Byte:
         case TypeCode.Int16:
         case TypeCode.UInt16:
         case TypeCode.Int32:
         case TypeCode.UInt32:
         case TypeCode.Int64: return l1 == ic2.ToInt64(null);
         case TypeCode.UInt64: return l1 >= 0 && ((UInt64)l1) == ic2.ToUInt64(null);
         case TypeCode.Single: return l1 == ic2.ToSingle(null);
         case TypeCode.Double: return l1 == ic2.ToDouble(null);
         case TypeCode.Decimal: return ((Decimal)l1) == ic2.ToDecimal(null);
       }
       return false;
       
     case TypeCode.UInt64:
       UInt64 ul1 = ic1.ToUInt64(null);
       switch (t2){
         case TypeCode.Char: return ul1 == ic2.ToChar(null);
         case TypeCode.SByte:
         case TypeCode.Byte:
         case TypeCode.Int16:
         case TypeCode.UInt16:
         case TypeCode.Int32:
         case TypeCode.UInt32:
         case TypeCode.Int64:
           l1 = ic2.ToInt64(null);
           return l1 >= 0 && ul1 == (UInt64)l1;
         case TypeCode.UInt64: return ul1 == ic2.ToUInt64(null);
         case TypeCode.Single: return ul1 == ic2.ToSingle(null);
         case TypeCode.Double: return ul1 == ic2.ToDouble(null);
         case TypeCode.Decimal: return ((Decimal)ul1) == ic2.ToDecimal(null);
       }
       return false;
       
     case TypeCode.Single:
       Single f1 = ic1.ToSingle(null);
       switch (t2){
         case TypeCode.Char: return f1 == ic2.ToChar(null);
         case TypeCode.SByte:
         case TypeCode.Byte:
         case TypeCode.Int16:
         case TypeCode.UInt16:
         case TypeCode.Int32:
         case TypeCode.UInt32:
         case TypeCode.Int64: return f1 == ic2.ToInt64(null);
         case TypeCode.UInt64: return f1 == ic2.ToUInt64(null);
         case TypeCode.Single: return f1 == ic2.ToSingle(null);
         case TypeCode.Double: return f1 == ic2.ToSingle(null);
         case TypeCode.Decimal: return ((Decimal)f1) == ic2.ToDecimal(null);
       }
       return false;
       
     case TypeCode.Double:
       Double d1 = ic1.ToDouble(null);
       switch (t2){
         case TypeCode.Char: return d1 == ic2.ToChar(null);
         case TypeCode.SByte:
         case TypeCode.Byte:
         case TypeCode.Int16:
         case TypeCode.UInt16:
         case TypeCode.Int32:
         case TypeCode.UInt32:
         case TypeCode.Int64: return d1 == ic2.ToInt64(null);
         case TypeCode.UInt64: return d1 == ic2.ToUInt64(null);
         case TypeCode.Single: return ((float)d1) == ic2.ToSingle(null);
         case TypeCode.Double: return d1 == ic2.ToDouble(null);
         case TypeCode.Decimal: return ((Decimal)d1) == ic2.ToDecimal(null);
       }
       return false;
       
     case TypeCode.Decimal:
       Decimal de1 = ic1.ToDecimal(null);
       switch (t2){
         case TypeCode.Char: return de1 == (Decimal)(int)ic2.ToChar(null);
         case TypeCode.SByte:
         case TypeCode.Byte:
         case TypeCode.Int16:
         case TypeCode.UInt16:
         case TypeCode.Int32:
         case TypeCode.UInt32:
         case TypeCode.Int64: return de1 == ic2.ToInt64(null);
         case TypeCode.UInt64: return de1 == ic2.ToUInt64(null);
         case TypeCode.Single: return de1 == (Decimal)ic2.ToSingle(null);
         case TypeCode.Double: return de1 == (Decimal)ic2.ToDouble(null);
         case TypeCode.Decimal: return de1 == ic2.ToDecimal(null);
       }
       return false;
       
     case TypeCode.DateTime: return t2 == TypeCode.DateTime && ic1.ToDateTime(null) == ic2.ToDateTime(null);
     case TypeCode.String:
       if (t2 == TypeCode.Char){
         String str = ic1.ToString(null);
         return str.Length == 1 && str[0] == ic2.ToChar(null);
       }
       return t2 == TypeCode.String && (v1 == v2 || ic1.ToString(null).Equals(ic2.ToString(null)));
   }
   return false; //should never get here
 }
        decimal IConvertible.ToDecimal(IFormatProvider provider)
        {
            IConvertible conv = (IConvertible)value;

            return(conv.ToDecimal(provider));
        }
Example #17
0
        /// <summary>Returns a <see langword="String" /> value that corresponds to the specified object. </summary>
        /// <param name="Value">Required. Object to convert to a <see langword="String" /> value.</param>
        /// <returns>The <see langword="String" /> value corresponding to <paramref name="Value" />.</returns>
        public static string FromObject(object Value)
        {
            if (Value == null)
            {
                return((string)null);
            }
            string str = Value as string;

            if (str != null)
            {
                return(str);
            }
            IConvertible convertible = Value as IConvertible;

            if (convertible != null)
            {
                switch (convertible.GetTypeCode())
                {
                case TypeCode.Boolean:
                    return(StringType.FromBoolean(convertible.ToBoolean((IFormatProvider)null)));

                case TypeCode.Char:
                    return(StringType.FromChar(convertible.ToChar((IFormatProvider)null)));

                case TypeCode.Byte:
                    return(StringType.FromByte(convertible.ToByte((IFormatProvider)null)));

                case TypeCode.Int16:
                    return(StringType.FromShort(convertible.ToInt16((IFormatProvider)null)));

                case TypeCode.Int32:
                    return(StringType.FromInteger(convertible.ToInt32((IFormatProvider)null)));

                case TypeCode.Int64:
                    return(StringType.FromLong(convertible.ToInt64((IFormatProvider)null)));

                case TypeCode.Single:
                    return(StringType.FromSingle(convertible.ToSingle((IFormatProvider)null)));

                case TypeCode.Double:
                    return(StringType.FromDouble(convertible.ToDouble((IFormatProvider)null)));

                case TypeCode.Decimal:
                    return(StringType.FromDecimal(convertible.ToDecimal((IFormatProvider)null)));

                case TypeCode.DateTime:
                    return(StringType.FromDate(convertible.ToDateTime((IFormatProvider)null)));

                case TypeCode.String:
                    return(convertible.ToString((IFormatProvider)null));
                }
            }
            else
            {
                char[] chArray = Value as char[];
                if (chArray != null && chArray.Rank == 1)
                {
                    return(new string(CharArrayType.FromObject(Value)));
                }
            }
            throw new InvalidCastException(Utils.GetResourceString("InvalidCast_FromTo", Utils.VBFriendlyName(Value), "String"));
        }
Example #18
0
        private void EmitConstant(IConvertible ic)
        {
            this.StackSize++;
              TypeCode tc = ic.GetTypeCode();
              switch (tc) {
            case TypeCode.Boolean:
            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Char:
            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Int32:
            case TypeCode.UInt32:
            case TypeCode.Int64:
              long n = ic.ToInt64(null);
              switch (n) {
            case -1: this.generator.Emit(OperationCode.Ldc_I4_M1); break;
            case 0: this.generator.Emit(OperationCode.Ldc_I4_0); break;
            case 1: this.generator.Emit(OperationCode.Ldc_I4_1); break;
            case 2: this.generator.Emit(OperationCode.Ldc_I4_2); break;
            case 3: this.generator.Emit(OperationCode.Ldc_I4_3); break;
            case 4: this.generator.Emit(OperationCode.Ldc_I4_4); break;
            case 5: this.generator.Emit(OperationCode.Ldc_I4_5); break;
            case 6: this.generator.Emit(OperationCode.Ldc_I4_6); break;
            case 7: this.generator.Emit(OperationCode.Ldc_I4_7); break;
            case 8: this.generator.Emit(OperationCode.Ldc_I4_8); break;
            default:
              if (sbyte.MinValue <= n && n <= sbyte.MaxValue) {
                this.generator.Emit(OperationCode.Ldc_I4_S, (sbyte)n);
              } else if (int.MinValue <= n && n <= int.MaxValue ||
                n <= uint.MaxValue && (tc == TypeCode.Char || tc == TypeCode.UInt16 || tc == TypeCode.UInt32)) {
                if (n == uint.MaxValue)
                  this.generator.Emit(OperationCode.Ldc_I4_M1);
                else
                  this.generator.Emit(OperationCode.Ldc_I4, (int)n);
              } else {
                this.generator.Emit(OperationCode.Ldc_I8, n);
                tc = TypeCode.Empty; //Suppress conversion to long
              }
              break;
              }
              if (tc == TypeCode.Int64)
            this.generator.Emit(OperationCode.Conv_I8);
              return;

            case TypeCode.UInt64:
              this.generator.Emit(OperationCode.Ldc_I8, (long)ic.ToUInt64(null));
              return;

            case TypeCode.Single:
              this.generator.Emit(OperationCode.Ldc_R4, ic.ToSingle(null));
              return;

            case TypeCode.Double:
              this.generator.Emit(OperationCode.Ldc_R8, ic.ToDouble(null));
              return;

            case TypeCode.String:
              this.generator.Emit(OperationCode.Ldstr, ic.ToString(null));
              return;

            case TypeCode.Decimal:
              var bits = Decimal.GetBits(ic.ToDecimal(null));
              this.generator.Emit(OperationCode.Ldc_I4, bits[0]);
              this.generator.Emit(OperationCode.Ldc_I4, bits[1]); this.StackSize++;
              this.generator.Emit(OperationCode.Ldc_I4, bits[2]); this.StackSize++;
              if (bits[3] >= 0)
            this.generator.Emit(OperationCode.Ldc_I4_0);
              else
            this.generator.Emit(OperationCode.Ldc_I4_1);
              this.StackSize++;
              int scale = (bits[3]&0x7FFFFF)>>16;
              if (scale > 28) scale = 28;
              this.generator.Emit(OperationCode.Ldc_I4_S, scale); this.StackSize++;
              this.generator.Emit(OperationCode.Newobj, this.DecimalConstructor);
              this.StackSize -= 4;
              return;
              }
        }
        internal static bool JScriptStrictEquals(object v1, object v2, IConvertible ic1, IConvertible ic2, TypeCode t1, TypeCode t2, bool checkForDebuggerObjects)
        {
            long num7;
            switch (t1)
            {
                case TypeCode.Empty:
                    return (t2 == TypeCode.Empty);

                case TypeCode.Object:
                    if (v1 != v2)
                    {
                        if ((v1 is Microsoft.JScript.Missing) || (v1 is System.Reflection.Missing))
                        {
                            v1 = null;
                        }
                        if (v1 == v2)
                        {
                            return true;
                        }
                        if ((v2 is Microsoft.JScript.Missing) || (v2 is System.Reflection.Missing))
                        {
                            v2 = null;
                        }
                        if (checkForDebuggerObjects)
                        {
                            IDebuggerObject obj2 = v1 as IDebuggerObject;
                            if (obj2 != null)
                            {
                                IDebuggerObject o = v2 as IDebuggerObject;
                                if (o != null)
                                {
                                    return obj2.IsEqual(o);
                                }
                            }
                        }
                        return (v1 == v2);
                    }
                    return true;

                case TypeCode.DBNull:
                    return (t2 == TypeCode.DBNull);

                case TypeCode.Boolean:
                    if (t2 != TypeCode.Boolean)
                    {
                        return false;
                    }
                    return (ic1.ToBoolean(null) == ic2.ToBoolean(null));

                case TypeCode.Char:
                {
                    char ch = ic1.ToChar(null);
                    switch (t2)
                    {
                        case TypeCode.Char:
                            return (ch == ic2.ToChar(null));

                        case TypeCode.SByte:
                        case TypeCode.Byte:
                        case TypeCode.Int16:
                        case TypeCode.UInt16:
                        case TypeCode.Int32:
                        case TypeCode.UInt32:
                        case TypeCode.Int64:
                            return (ch == ic2.ToInt64(null));

                        case TypeCode.UInt64:
                            return (ch == ic2.ToUInt64(null));

                        case TypeCode.Single:
                        case TypeCode.Double:
                            return (((double) ch) == ic2.ToDouble(null));

                        case TypeCode.Decimal:
                            return (ch == ic2.ToDecimal(null));

                        case TypeCode.String:
                        {
                            string str = ic2.ToString(null);
                            return ((str.Length == 1) && (ch == str[0]));
                        }
                    }
                    break;
                }
                case TypeCode.SByte:
                {
                    sbyte num = ic1.ToSByte(null);
                    switch (t2)
                    {
                        case TypeCode.Char:
                            return (num == ic2.ToChar(null));

                        case TypeCode.SByte:
                        case TypeCode.Byte:
                        case TypeCode.Int16:
                        case TypeCode.UInt16:
                        case TypeCode.Int32:
                        case TypeCode.UInt32:
                        case TypeCode.Int64:
                            return (num == ic2.ToInt64(null));

                        case TypeCode.UInt64:
                            return ((num >= 0) && (num == ic2.ToUInt64(null)));

                        case TypeCode.Single:
                            return (num == ic2.ToSingle(null));

                        case TypeCode.Double:
                            return (num == ic2.ToDouble(null));

                        case TypeCode.Decimal:
                            return (num == ic2.ToDecimal(null));
                    }
                    return false;
                }
                case TypeCode.Byte:
                {
                    byte num2 = ic1.ToByte(null);
                    switch (t2)
                    {
                        case TypeCode.Char:
                            return (num2 == ic2.ToChar(null));

                        case TypeCode.SByte:
                        case TypeCode.Byte:
                        case TypeCode.Int16:
                        case TypeCode.UInt16:
                        case TypeCode.Int32:
                        case TypeCode.UInt32:
                        case TypeCode.Int64:
                            return (num2 == ic2.ToInt64(null));

                        case TypeCode.UInt64:
                            return (num2 == ic2.ToUInt64(null));

                        case TypeCode.Single:
                            return (num2 == ic2.ToSingle(null));

                        case TypeCode.Double:
                            return (num2 == ic2.ToDouble(null));

                        case TypeCode.Decimal:
                            return (num2 == ic2.ToDecimal(null));
                    }
                    return false;
                }
                case TypeCode.Int16:
                {
                    short num3 = ic1.ToInt16(null);
                    switch (t2)
                    {
                        case TypeCode.Char:
                            return (num3 == ic2.ToChar(null));

                        case TypeCode.SByte:
                        case TypeCode.Byte:
                        case TypeCode.Int16:
                        case TypeCode.UInt16:
                        case TypeCode.Int32:
                        case TypeCode.UInt32:
                        case TypeCode.Int64:
                            return (num3 == ic2.ToInt64(null));

                        case TypeCode.UInt64:
                            return ((num3 >= 0) && (num3 == ic2.ToUInt64(null)));

                        case TypeCode.Single:
                            return (num3 == ic2.ToSingle(null));

                        case TypeCode.Double:
                            return (num3 == ic2.ToDouble(null));

                        case TypeCode.Decimal:
                            return (num3 == ic2.ToDecimal(null));
                    }
                    return false;
                }
                case TypeCode.UInt16:
                {
                    ushort num4 = ic1.ToUInt16(null);
                    switch (t2)
                    {
                        case TypeCode.Char:
                            return (num4 == ic2.ToChar(null));

                        case TypeCode.SByte:
                        case TypeCode.Byte:
                        case TypeCode.Int16:
                        case TypeCode.UInt16:
                        case TypeCode.Int32:
                        case TypeCode.UInt32:
                        case TypeCode.Int64:
                            return (num4 == ic2.ToInt64(null));

                        case TypeCode.UInt64:
                            return (num4 == ic2.ToUInt64(null));

                        case TypeCode.Single:
                            return (num4 == ic2.ToSingle(null));

                        case TypeCode.Double:
                            return (num4 == ic2.ToDouble(null));

                        case TypeCode.Decimal:
                            return (num4 == ic2.ToDecimal(null));
                    }
                    return false;
                }
                case TypeCode.Int32:
                {
                    int num5 = ic1.ToInt32(null);
                    switch (t2)
                    {
                        case TypeCode.Char:
                            return (num5 == ic2.ToChar(null));

                        case TypeCode.SByte:
                        case TypeCode.Byte:
                        case TypeCode.Int16:
                        case TypeCode.UInt16:
                        case TypeCode.Int32:
                        case TypeCode.UInt32:
                        case TypeCode.Int64:
                            return (num5 == ic2.ToInt64(null));

                        case TypeCode.UInt64:
                            return ((num5 >= 0) && (num5 == ic2.ToUInt64(null)));

                        case TypeCode.Single:
                            return (num5 == ic2.ToSingle(null));

                        case TypeCode.Double:
                            return (num5 == ic2.ToDouble(null));

                        case TypeCode.Decimal:
                            return (num5 == ic2.ToDecimal(null));
                    }
                    return false;
                }
                case TypeCode.UInt32:
                {
                    uint num6 = ic1.ToUInt32(null);
                    switch (t2)
                    {
                        case TypeCode.Char:
                            return (num6 == ic2.ToChar(null));

                        case TypeCode.SByte:
                        case TypeCode.Byte:
                        case TypeCode.Int16:
                        case TypeCode.UInt16:
                        case TypeCode.Int32:
                        case TypeCode.UInt32:
                        case TypeCode.Int64:
                            return (num6 == ic2.ToInt64(null));

                        case TypeCode.UInt64:
                            return (num6 == ic2.ToUInt64(null));

                        case TypeCode.Single:
                            return (num6 == ic2.ToSingle(null));

                        case TypeCode.Double:
                            return (num6 == ic2.ToDouble(null));

                        case TypeCode.Decimal:
                            return (num6 == ic2.ToDecimal(null));
                    }
                    return false;
                }
                case TypeCode.Int64:
                    num7 = ic1.ToInt64(null);
                    switch (t2)
                    {
                        case TypeCode.Char:
                            return (num7 == ic2.ToChar(null));

                        case TypeCode.SByte:
                        case TypeCode.Byte:
                        case TypeCode.Int16:
                        case TypeCode.UInt16:
                        case TypeCode.Int32:
                        case TypeCode.UInt32:
                        case TypeCode.Int64:
                            return (num7 == ic2.ToInt64(null));

                        case TypeCode.UInt64:
                            return ((num7 >= 0L) && (num7 == ic2.ToUInt64(null)));

                        case TypeCode.Single:
                            return (num7 == ic2.ToSingle(null));

                        case TypeCode.Double:
                            return (num7 == ic2.ToDouble(null));

                        case TypeCode.Decimal:
                            return (num7 == ic2.ToDecimal(null));
                    }
                    return false;

                case TypeCode.UInt64:
                {
                    ulong num8 = ic1.ToUInt64(null);
                    switch (t2)
                    {
                        case TypeCode.Char:
                            return (num8 == ic2.ToChar(null));

                        case TypeCode.SByte:
                        case TypeCode.Byte:
                        case TypeCode.Int16:
                        case TypeCode.UInt16:
                        case TypeCode.Int32:
                        case TypeCode.UInt32:
                        case TypeCode.Int64:
                            num7 = ic2.ToInt64(null);
                            return ((num7 >= 0L) && (num8 == num7));

                        case TypeCode.UInt64:
                            return (num8 == ic2.ToUInt64(null));

                        case TypeCode.Single:
                            return (num8 == ic2.ToSingle(null));

                        case TypeCode.Double:
                            return (num8 == ic2.ToDouble(null));

                        case TypeCode.Decimal:
                            return (num8 == ic2.ToDecimal(null));
                    }
                    return false;
                }
                case TypeCode.Single:
                {
                    float num9 = ic1.ToSingle(null);
                    switch (t2)
                    {
                        case TypeCode.Char:
                            return (num9 == ((float) ic2.ToChar(null)));

                        case TypeCode.SByte:
                        case TypeCode.Byte:
                        case TypeCode.Int16:
                        case TypeCode.UInt16:
                        case TypeCode.Int32:
                        case TypeCode.UInt32:
                        case TypeCode.Int64:
                            return (num9 == ic2.ToInt64(null));

                        case TypeCode.UInt64:
                            return (num9 == ic2.ToUInt64(null));

                        case TypeCode.Single:
                            return (num9 == ic2.ToSingle(null));

                        case TypeCode.Double:
                            return (num9 == ic2.ToSingle(null));

                        case TypeCode.Decimal:
                            return (((decimal) num9) == ic2.ToDecimal(null));
                    }
                    return false;
                }
                case TypeCode.Double:
                {
                    double num10 = ic1.ToDouble(null);
                    switch (t2)
                    {
                        case TypeCode.Char:
                            return (num10 == ((double) ic2.ToChar(null)));

                        case TypeCode.SByte:
                        case TypeCode.Byte:
                        case TypeCode.Int16:
                        case TypeCode.UInt16:
                        case TypeCode.Int32:
                        case TypeCode.UInt32:
                        case TypeCode.Int64:
                            return (num10 == ic2.ToInt64(null));

                        case TypeCode.UInt64:
                            return (num10 == ic2.ToUInt64(null));

                        case TypeCode.Single:
                            return (((float) num10) == ic2.ToSingle(null));

                        case TypeCode.Double:
                            return (num10 == ic2.ToDouble(null));

                        case TypeCode.Decimal:
                            return (((decimal) num10) == ic2.ToDecimal(null));
                    }
                    return false;
                }
                case TypeCode.Decimal:
                {
                    decimal num11 = ic1.ToDecimal(null);
                    switch (t2)
                    {
                        case TypeCode.Char:
                            return (num11 == ic2.ToChar(null));

                        case TypeCode.SByte:
                        case TypeCode.Byte:
                        case TypeCode.Int16:
                        case TypeCode.UInt16:
                        case TypeCode.Int32:
                        case TypeCode.UInt32:
                        case TypeCode.Int64:
                            return (num11 == ic2.ToInt64(null));

                        case TypeCode.UInt64:
                            return (num11 == ic2.ToUInt64(null));

                        case TypeCode.Single:
                            return (num11 == ((decimal) ic2.ToSingle(null)));

                        case TypeCode.Double:
                            return (num11 == ((decimal) ic2.ToDouble(null)));

                        case TypeCode.Decimal:
                            return (num11 == ic2.ToDecimal(null));
                    }
                    return false;
                }
                case TypeCode.DateTime:
                    if (t2 != TypeCode.DateTime)
                    {
                        return false;
                    }
                    return (ic1.ToDateTime(null) == ic2.ToDateTime(null));

                case TypeCode.String:
                {
                    if (t2 != TypeCode.Char)
                    {
                        if (t2 != TypeCode.String)
                        {
                            return false;
                        }
                        if (v1 != v2)
                        {
                            return ic1.ToString(null).Equals(ic2.ToString(null));
                        }
                        return true;
                    }
                    string str2 = ic1.ToString(null);
                    if (str2.Length != 1)
                    {
                        return false;
                    }
                    return (str2[0] == ic2.ToChar(null));
                }
                default:
                    return false;
            }
            return false;
        }
 private static double DecimalToDouble(IConvertible ValueInterface)
 {
     return Convert.ToDouble(ValueInterface.ToDecimal(null));
 }
 public static Literal DoSubOvf(IConvertible ic1, IConvertible ic2, TypeCode code1, TypeCode code2, BinaryExpression binaryExpression){
   TypeNode type = SystemTypes.Object;
   object val = null;
   checked{switch(code1){
     case TypeCode.SByte:
     case TypeCode.Int16:
     case TypeCode.Int32:
       int i = ic1.ToInt32(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
         case TypeCode.Int32: 
         case TypeCode.Byte:
         case TypeCode.Char:
         case TypeCode.UInt16:
           val = i - ic2.ToInt32(null); 
           type = SystemTypes.Int32;
           break;
         case TypeCode.Int64: 
         case TypeCode.UInt32: 
         case TypeCode.UInt64: 
           val = i - ic2.ToInt64(null); 
           type = SystemTypes.Int64;
           break;
         case TypeCode.Single: 
           val = i - ic2.ToSingle(null); 
           type = SystemTypes.Single;
           break;
         case TypeCode.Double: 
           val = i - ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = i - ic2.ToDecimal(null);
           type = SystemTypes.Decimal;
           break;
         default: return null;
       }
       break;
     case TypeCode.Byte:
     case TypeCode.UInt16:
       ushort us = ic1.ToUInt16(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
         case TypeCode.Int32: 
           val = us - ic2.ToInt32(null); 
           type = SystemTypes.Int32;
           break;
         case TypeCode.Byte:
         case TypeCode.UInt16:
         case TypeCode.Char:
         case TypeCode.UInt32: 
           val = us - ic2.ToUInt32(null); 
           type = SystemTypes.UInt32;
           break;
         case TypeCode.Int64: 
           val = us - ic2.ToInt64(null); 
           type = SystemTypes.Int64;
           break;
         case TypeCode.UInt64: 
           val = us - ic2.ToUInt64(null); 
           type = SystemTypes.UInt64;
           break;
         case TypeCode.Single: 
         case TypeCode.Double: 
           val = us - ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = us - ic2.ToDecimal(null);
           type = SystemTypes.Decimal;
           break;
         default: return null;
       }
       break;
     case TypeCode.Char:
       char ch = ic1.ToChar(null);
       if (code2 != TypeCode.Char) goto default;
       val = ch - ic2.ToChar(null);
       type = SystemTypes.Int32;
       break;
     case TypeCode.UInt32:
       uint ui = ic1.ToUInt32(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
         case TypeCode.Int32: 
         case TypeCode.Int64: 
           val = ui - ic2.ToInt64(null); 
           type = SystemTypes.Int64;
           break;
         case TypeCode.Byte:
         case TypeCode.UInt16:
         case TypeCode.Char:
         case TypeCode.UInt32: 
           val = ui - ic2.ToUInt32(null); 
           type = SystemTypes.UInt32;
           break;
         case TypeCode.UInt64: 
           val = ui - ic2.ToUInt64(null); 
           type = SystemTypes.UInt64;
           break;
         case TypeCode.Single: 
           val = ui - ic2.ToSingle(null); 
           type = SystemTypes.Single;
           break;
         case TypeCode.Double: 
           val = ui - ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = ui - ic2.ToDecimal(null);
           type = SystemTypes.Decimal;
           break;
         default: return null;
       }
       break;
     case TypeCode.Int64:
       long l = ic1.ToInt64(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
         case TypeCode.Int32: 
         case TypeCode.Int64: 
           val = l - ic2.ToInt64(null); 
           type = SystemTypes.Int64;
           break;
         case TypeCode.Byte:
         case TypeCode.UInt16:
         case TypeCode.Char:
         case TypeCode.UInt32: 
           val = l - ic2.ToUInt32(null); 
           type = SystemTypes.UInt32;
           break;
         case TypeCode.UInt64: 
           val = l - (long)ic2.ToUInt64(null); 
           type = SystemTypes.UInt64;
           break;
         case TypeCode.Single: 
           val = l - ic2.ToSingle(null); 
           type = SystemTypes.Single;
           break;
         case TypeCode.Double: 
           val = l - ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = l - ic2.ToDecimal(null);
           type = SystemTypes.Decimal;
           break;
         default: return null;
       }
       break;
     case TypeCode.UInt64:
       ulong ul = ic1.ToUInt64(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
         case TypeCode.Int32: 
         case TypeCode.Byte:
         case TypeCode.UInt16:
         case TypeCode.Char:
         case TypeCode.UInt32: 
           val = ul - ic2.ToUInt32(null); 
           type = SystemTypes.UInt64;
           break;
         case TypeCode.Int64: 
         case TypeCode.UInt64: 
           val = ul - ic2.ToUInt64(null); 
           type = SystemTypes.UInt64;
           break;
         case TypeCode.Single: 
           val = ul - ic2.ToSingle(null); 
           type = SystemTypes.Single;
           break;
         case TypeCode.Double: 
           val = ul - ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = ul - ic2.ToDecimal(null);
           type = SystemTypes.Decimal;
           break;
         default: return null;
       }
       break;
     case TypeCode.Single:
       float f = ic1.ToSingle(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
           val = f - ic2.ToInt16(null);
           type = SystemTypes.Single;
           break;
         case TypeCode.Int32: 
         case TypeCode.Int64: 
           val = f - (double)ic2.ToInt64(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Byte:
         case TypeCode.Char:
         case TypeCode.UInt16:
           val = f - ic2.ToUInt16(null); 
           type = SystemTypes.Single;
           break;
         case TypeCode.UInt32: 
         case TypeCode.UInt64: 
           val = f - (double)ic2.ToUInt64(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Single: 
         case TypeCode.Double: 
           val = f - ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = f - (double)ic2.ToDecimal(null);
           type = SystemTypes.Double;
           break;
         default: return null;
       }
       break;
     case TypeCode.Double:
       double d = ic1.ToDouble(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
         case TypeCode.Int32: 
         case TypeCode.Int64: 
           val = d - ic2.ToInt64(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Byte:
         case TypeCode.Char:
         case TypeCode.UInt16:
         case TypeCode.UInt32: 
         case TypeCode.UInt64: 
           val = d - ic2.ToUInt64(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Single: 
         case TypeCode.Double: 
           val = d - ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = d - (double)ic2.ToDecimal(null);
           type = SystemTypes.Double;
           break;
         default: return null;
       }
       break;
     case TypeCode.Decimal:
       decimal dec = ic1.ToDecimal(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Byte:
         case TypeCode.Int16:
         case TypeCode.UInt16:
         case TypeCode.Char:
         case TypeCode.Int32: 
           val = dec - ic2.ToInt32(null); 
           type = SystemTypes.Decimal;
           break;
         case TypeCode.UInt32: 
         case TypeCode.Int64: 
         case TypeCode.UInt64: 
           val = dec - ic2.ToInt64(null); 
           type = SystemTypes.Decimal;
           break;
         case TypeCode.Decimal:
           val = dec - ic2.ToDecimal(null);
           type = SystemTypes.Decimal;
           break;
         default: return null;
       }
       break;
     default: return null;
   }
   }
   return new Literal(val, type, binaryExpression.SourceContext);
 }
 private static long DecimalToLong(IConvertible ValueInterface)
 {
     return Convert.ToInt64(ValueInterface.ToDecimal(null));
 }
Example #23
0
 public override decimal ToDecimal(IFormatProvider provider)
 {
     return(value.ToDecimal(provider));
 }
Example #24
0
 /// <summary> 根据表达式2元操作类型,计算2个常量的值
 /// </summary>
 /// <param name="nodeType"></param>
 /// <param name="a">常量1</param>
 /// <param name="b">常量2</param>
 private void Math(ExpressionType nodeType, IConvertible a, IConvertible b)
 {
     switch (nodeType)
     {
         case ExpressionType.Add:
             unchecked { _state.Number = a.ToDecimal(null) + b.ToDecimal(null); }
             return;
         case ExpressionType.AddChecked:
             checked { _state.Number = a.ToDecimal(null) + b.ToDecimal(null); }
             return;
         case ExpressionType.Subtract:
             unchecked { _state.Number = a.ToDecimal(null) - b.ToDecimal(null); }
             return;
         case ExpressionType.SubtractChecked:
             checked { _state.Number = a.ToDecimal(null) - b.ToDecimal(null); }
             return;
         case ExpressionType.Multiply:
             unchecked { _state.Number = a.ToDecimal(null) * b.ToDecimal(null); }
             return;
         case ExpressionType.MultiplyChecked:
             checked { _state.Number = a.ToDecimal(null) * b.ToDecimal(null); }
             return;
         case ExpressionType.Divide:
             _state.Number = a.ToDecimal(null) / b.ToDecimal(null);
             return;
         case ExpressionType.Modulo:
             _state.Number = a.ToDecimal(null) % b.ToDecimal(null);
             return;
         case ExpressionType.And:
             _state.Number = (long)a & (long)b;
             return;
         case ExpressionType.Or:
             _state.Number = (long)a | (long)b;
             return;
         case ExpressionType.RightShift:
             if (a is int == false)
             {
                 Throw();
             }
             _state.Number = (int)a >> (int)b;
             return;
         case ExpressionType.LeftShift:
             if (a is int == false)
             {
                 Throw();
             }
             _state.Number = (int)a << (int)b;
             return;
         case ExpressionType.LessThan:
             _state.Boolean = ((IComparable)a).CompareTo((IComparable)b) < 0;
             return;
         case ExpressionType.LessThanOrEqual:
             _state.Boolean = ((IComparable)a).CompareTo((IComparable)b) <= 0;
             return;
         case ExpressionType.GreaterThan:
             _state.Boolean = ((IComparable)a).CompareTo((IComparable)b) > 0;
             return;
         case ExpressionType.GreaterThanOrEqual:
             _state.Boolean = ((IComparable)a).CompareTo((IComparable)b) >= 0;
             return;
         case ExpressionType.Equal:
             _state.Boolean = ((IComparable)a).CompareTo((IComparable)b) == 0;
             return;
         case ExpressionType.NotEqual:
             _state.Boolean = ((IComparable)a).CompareTo((IComparable)b) != 0;
             return;
         case ExpressionType.ExclusiveOr:
             if (a is int == false)
             {
                 Throw();
             }
             _state.Number = (int)a ^ (int)b;
             return;
         default:
             Throw();
             throw new NotImplementedException();
     }
 }
Example #25
0
 internal static String ToString(Object value, PreferredType pref, IConvertible ic, bool explicitOK){
   Enum e = value as Enum;
   if (e != null) return e.ToString("G");
   EnumWrapper ew = value as EnumWrapper;
   if (ew != null) return ew.ToString();
   TypeCode code = Convert.GetTypeCode(value, ic);
   if (pref == PreferredType.LocaleString){
     switch (code){
       case TypeCode.SByte:
       case TypeCode.Byte:
       case TypeCode.Int16:
       case TypeCode.UInt16:
       case TypeCode.Int32:
       case TypeCode.UInt32:
       case TypeCode.Single:
       case TypeCode.Double: {
         double d = ic.ToDouble(null);
         return d.ToString(d <= -1e+15 || d >= 1e+15 ? "g" : "n", NumberFormatInfo.CurrentInfo);
       }
       case TypeCode.Int64: return ic.ToInt64(null).ToString("n", NumberFormatInfo.CurrentInfo);
       case TypeCode.UInt64: return ic.ToUInt64(null).ToString("n", NumberFormatInfo.CurrentInfo);
       case TypeCode.Decimal: return ic.ToDecimal(null).ToString("n", NumberFormatInfo.CurrentInfo);
     }
   }
   switch (code){
     case TypeCode.Empty: return explicitOK ? "undefined" : null;
     case TypeCode.Object: return Convert.ToString(Convert.ToPrimitive(value, pref, ref ic), ic);
     case TypeCode.DBNull: return explicitOK ? "null" : null;
     case TypeCode.Boolean: return ic.ToBoolean(null) ? "true" : "false";
     case TypeCode.Char:
     case TypeCode.SByte:
     case TypeCode.Byte:
     case TypeCode.Int16:
     case TypeCode.UInt16:
     case TypeCode.Int32:
     case TypeCode.UInt32:
     case TypeCode.Int64:
     case TypeCode.UInt64:
     case TypeCode.Decimal:
     case TypeCode.String: return ic.ToString(null);
     case TypeCode.DateTime: return Convert.ToString(DateConstructor.ob.Construct(ic.ToDateTime(null)));
     case TypeCode.Single:
     case TypeCode.Double: return Convert.ToString(ic.ToDouble(null));
   }
   return null; //Should never get here
 }
Example #26
0
        internal static Object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) {
            BCLDebug.Assert(value!=null, "[Convert.DefaultToType]value!=null");

            if (targetType==null) {
                throw new ArgumentNullException("targetType");
            }
            
            if (value.GetType()==targetType) {
                return value;
            }

            if (targetType==ConvertTypes[(int)TypeCode.Boolean])
                return value.ToBoolean(provider);
            if (targetType==ConvertTypes[(int)TypeCode.Char])
                return value.ToChar(provider);
            if (targetType==ConvertTypes[(int)TypeCode.SByte])
                return value.ToSByte(provider);
            if (targetType==ConvertTypes[(int)TypeCode.Byte])
                return value.ToByte(provider);
            if (targetType==ConvertTypes[(int)TypeCode.Int16]) 
                return value.ToInt16(provider);
            if (targetType==ConvertTypes[(int)TypeCode.UInt16])
                return value.ToUInt16(provider);
            if (targetType==ConvertTypes[(int)TypeCode.Int32])
                return value.ToInt32(provider);
            if (targetType==ConvertTypes[(int)TypeCode.UInt32])
                return value.ToUInt32(provider);
            if (targetType==ConvertTypes[(int)TypeCode.Int64])
                return value.ToInt64(provider);
            if (targetType==ConvertTypes[(int)TypeCode.UInt64])
                return value.ToUInt64(provider);
            if (targetType==ConvertTypes[(int)TypeCode.Single])
                return value.ToSingle(provider);
            if (targetType==ConvertTypes[(int)TypeCode.Double])
                return value.ToDouble(provider);
            if (targetType==ConvertTypes[(int)TypeCode.Decimal])
                return value.ToDecimal(provider);
            if (targetType==ConvertTypes[(int)TypeCode.DateTime])
                return value.ToDateTime(provider);
            if (targetType==ConvertTypes[(int)TypeCode.String]) {
                return value.ToString(provider);
            }
            if (targetType==ConvertTypes[(int)TypeCode.Object])
                return (Object)value;
            if (targetType==ConvertTypes[(int)TypeCode.DBNull])
                throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull"));
            if (targetType==ConvertTypes[(int)TypeCode.Empty]) 
                throw new InvalidCastException(Environment.GetResourceString("InvalidCast_Empty"));
            throw new InvalidCastException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidCast_FromTo"), value.GetType().FullName, targetType.FullName));
        }
Example #27
0
        /// <summary>
        /// Writes a <see cref="Object"/> value.
        /// An error will raised if the value cannot be written as a single JSON token.
        /// </summary>
        /// <param name="value">The <see cref="Object"/> value to write.</param>
        public virtual void WriteValue(object value)
        {
            if (value == null)
            {
                WriteNull();
                return;
            }
            else if (value is IConvertible)
            {
                IConvertible convertible = value as IConvertible;

                switch (convertible.GetTypeCode())
                {
                case TypeCode.String:
                    WriteValue(convertible.ToString(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.Char:
                    WriteValue(convertible.ToChar(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.Boolean:
                    WriteValue(convertible.ToBoolean(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.SByte:
                    WriteValue(convertible.ToSByte(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.Int16:
                    WriteValue(convertible.ToInt16(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.UInt16:
                    WriteValue(convertible.ToUInt16(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.Int32:
                    WriteValue(convertible.ToInt32(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.Byte:
                    WriteValue(convertible.ToByte(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.UInt32:
                    WriteValue(convertible.ToUInt32(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.Int64:
                    WriteValue(convertible.ToInt64(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.UInt64:
                    WriteValue(convertible.ToUInt64(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.Single:
                    WriteValue(convertible.ToSingle(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.Double:
                    WriteValue(convertible.ToDouble(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.DateTime:
                    WriteValue(convertible.ToDateTime(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.Decimal:
                    WriteValue(convertible.ToDecimal(CultureInfo.InvariantCulture));
                    return;

                case TypeCode.DBNull:
                    WriteNull();
                    return;
                }
            }
#if !PocketPC && !NET20
            else if (value is DateTimeOffset)
            {
                WriteValue((DateTimeOffset)value);
                return;
            }
#endif
            else if (value is byte[])
            {
                WriteValue((byte[])value);
                return;
            }
            else if (value is Guid)
            {
                WriteValue((Guid)value);
                return;
            }
            else if (value is Uri)
            {
                WriteValue((Uri)value);
                return;
            }
            else if (value is TimeSpan)
            {
                WriteValue((TimeSpan)value);
                return;
            }

            throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
        }
 private static float DecimalToSingle(IConvertible ValueInterface)
 {
     return Convert.ToSingle(ValueInterface.ToDecimal(null));
 }
Example #29
0
        object IConvertible.ToType(System.Type targetType, IFormatProvider provider)
        {
            if (targetType == null)
            {
                throw new ArgumentNullException("targetType");
            }

            if (targetType == typeof(mpq))
            {
                return(this);
            }

            IConvertible convertible = this;

            if (targetType == typeof(sbyte))
            {
                return(convertible.ToSByte(provider));
            }
            if (targetType == typeof(byte))
            {
                return(convertible.ToByte(provider));
            }
            if (targetType == typeof(short))
            {
                return(convertible.ToInt16(provider));
            }
            if (targetType == typeof(ushort))
            {
                return(convertible.ToUInt16(provider));
            }
            if (targetType == typeof(int))
            {
                return(convertible.ToInt32(provider));
            }
            if (targetType == typeof(uint))
            {
                return(convertible.ToUInt32(provider));
            }
            if (targetType == typeof(long))
            {
                return(convertible.ToInt64(provider));
            }
            if (targetType == typeof(ulong))
            {
                return(convertible.ToUInt64(provider));
            }
            if (targetType == typeof(float))
            {
                return(convertible.ToSingle(provider));
            }
            if (targetType == typeof(double))
            {
                return(convertible.ToDouble(provider));
            }
            if (targetType == typeof(Decimal))
            {
                return(convertible.ToDecimal(provider));
            }
            if (targetType == typeof(string))
            {
                return(convertible.ToString(provider));
            }
            if (targetType == typeof(object))
            {
                return(convertible);
            }

            throw new InvalidCastException();
        }
Example #30
0
	// Default implementation of the "ToType" methods in
	// the primitive classes like Byte, Int32, Boolean, etc.
	internal static Object DefaultToType(IConvertible obj, Type targetType,
										 IFormatProvider provider,
										 bool recursive)
			{
				if(targetType != null)
				{
					if(obj.GetType() == targetType)
					{
						return obj;
					}
					else if(targetType == ConvertTypes[(int)TypeCode.Boolean])
					{
						return (Object)(obj.ToBoolean(provider));
					}
					else if(targetType == ConvertTypes[(int)TypeCode.Char])
					{
						return (Object)(obj.ToChar(provider));
					}
					else if(targetType == ConvertTypes[(int)TypeCode.SByte])
					{
						return (Object)(obj.ToSByte(provider));
					}
					else if(targetType == ConvertTypes[(int)TypeCode.Byte])
					{
						return (Object)(obj.ToByte(provider));
					}
					else if(targetType == ConvertTypes[(int)TypeCode.Int16])
					{
						return (Object)(obj.ToInt16(provider));
					}
					else if(targetType == ConvertTypes[(int)TypeCode.UInt16])
					{
						return (Object)(obj.ToUInt16(provider));
					}
					else if(targetType == ConvertTypes[(int)TypeCode.Int32])
					{
						return (Object)(obj.ToInt32(provider));
					}
					else if(targetType == ConvertTypes[(int)TypeCode.UInt32])
					{
						return (Object)(obj.ToUInt32(provider));
					}
					else if(targetType == ConvertTypes[(int)TypeCode.Int64])
					{
						return (Object)(obj.ToInt64(provider));
					}
					else if(targetType == ConvertTypes[(int)TypeCode.UInt64])
					{
						return (Object)(obj.ToUInt64(provider));
					}
					else if(targetType == ConvertTypes[(int)TypeCode.Single])
					{
						return (Object)(obj.ToSingle(provider));
					}
					else if(targetType == ConvertTypes[(int)TypeCode.Double])
					{
						return (Object)(obj.ToDouble(provider));
					}
					else if(targetType == ConvertTypes[(int)TypeCode.Decimal])
					{
						return (Object)(obj.ToDecimal(provider));
					}
					else if(targetType == ConvertTypes[(int)TypeCode.DateTime])
					{
						return (Object)(obj.ToDateTime(provider));
					}
					else if(targetType == ConvertTypes[(int)TypeCode.String])
					{
						return (Object)(obj.ToString(provider));
					}
					else if(targetType == ConvertTypes[(int)TypeCode.Object])
					{
						return obj;
					}
					else if(targetType == ConvertTypes[(int)TypeCode.Empty])
					{
						throw new InvalidCastException
							(_("InvalidCast_Empty"));
					}
					else if(targetType == ConvertTypes[(int)TypeCode.DBNull])
					{
						throw new InvalidCastException
							(_("InvalidCast_DBNull"));
					}
					else if(recursive)
					{
						throw new InvalidCastException
							(String.Format
								(_("InvalidCast_FromTo"),
		 					     obj.GetType().FullName, targetType.FullName));
					}
					else
					{
						// We weren't called from a "ToType" method,
						// so we can use it to handle the default case.
						return obj.ToType(targetType, provider);
					}
				}
				else
				{
					throw new ArgumentNullException("targetType");
				}
			}
Example #31
0
        public override decimal GetDecimal(int i)
        {
            IConvertible c = (IConvertible)GetValue(i);

            return(c.ToDecimal(CultureInfo.CurrentCulture));
        }
        /// <summary>
        /// Negates a <see cref="T:IConvertible"/> value by round tripping through
        /// the System.Decimal type.
        /// </summary>
        /// <param name="value">The value to negate.</param>
        /// <param name="formatProvider">The culture information.</param>
        /// <returns>The negated value as the original input type.</returns>
        private static object Negate(IConvertible value, IFormatProvider formatProvider)
        {
            TypeCode inputType = value.GetTypeCode();

            decimal input = value.ToDecimal(formatProvider);
            decimal output = Decimal.Negate(input);

            return System.Convert.ChangeType(output, inputType, formatProvider);
        }
Example #33
0
        private void TranslateToIL(ILGenerator il, Object val, Type rtype)
        {
            IConvertible ic = Convert.GetIConvertible(val);

            switch (Convert.GetTypeCode(val, ic))
            {
            case TypeCode.Empty:
                il.Emit(OpCodes.Ldnull);
                if (rtype.IsValueType)
                {
                    Convert.Emit(this, il, Typeob.Object, rtype);
                }
                return;

            case TypeCode.Object:
                break;

            case TypeCode.DBNull:
                il.Emit(OpCodes.Ldsfld, typeof(DBNull).GetField("Value"));
                Convert.Emit(this, il, Typeob.Null, rtype);
                return;

            case TypeCode.Boolean:
                ConstantWrapper.TranslateToILInt(il, ic.ToInt32(null));
                Convert.Emit(this, il, Typeob.Boolean, rtype);
                return;

            case TypeCode.Char:
            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Int32:
                ConstantWrapper.TranslateToILInt(il, ic.ToInt32(null));
                if (rtype.IsEnum)
                {
                    return;
                }
                if (val is EnumWrapper)
                {
                    Convert.Emit(this, il, ((EnumWrapper)val).type.GetTypeBuilderOrEnumBuilder(), rtype);
                }
                else
                {
                    Convert.Emit(this, il, val.GetType(), rtype);
                }
                return;

            case TypeCode.UInt32:
                ConstantWrapper.TranslateToILInt(il, (int)ic.ToUInt32(null));
                if (rtype.IsEnum)
                {
                    return;
                }
                if (val is EnumWrapper)
                {
                    Convert.Emit(this, il, ((EnumWrapper)val).type.GetTypeBuilderOrEnumBuilder(), rtype);
                }
                else
                {
                    Convert.Emit(this, il, Typeob.UInt32, rtype);
                }
                return;

            case TypeCode.Int64:
                long l = ic.ToInt64(null);
                if (Int32.MinValue <= l && l <= Int32.MaxValue)
                {
                    ConstantWrapper.TranslateToILInt(il, (int)l);
                    il.Emit(OpCodes.Conv_I8);
                }
                else
                {
                    il.Emit(OpCodes.Ldc_I8, l);
                }
                if (rtype.IsEnum)
                {
                    return;
                }
                if (val is EnumWrapper)
                {
                    Convert.Emit(this, il, ((EnumWrapper)val).type.GetTypeBuilderOrEnumBuilder(), rtype);
                }
                else
                {
                    Convert.Emit(this, il, Typeob.Int64, rtype);
                }
                return;

            case TypeCode.UInt64:
                ulong ul = ic.ToUInt64(null);
                if (ul <= Int32.MaxValue)
                {
                    ConstantWrapper.TranslateToILInt(il, (int)ul);
                    il.Emit(OpCodes.Conv_I8);
                }
                else
                {
                    il.Emit(OpCodes.Ldc_I8, (long)ul);
                }
                if (rtype.IsEnum)
                {
                    return;
                }
                if (val is EnumWrapper)
                {
                    Convert.Emit(this, il, ((EnumWrapper)val).type.GetTypeBuilderOrEnumBuilder(), rtype);
                }
                else
                {
                    Convert.Emit(this, il, Typeob.UInt64, rtype);
                }
                return;

            case TypeCode.Single:
                float f = ic.ToSingle(null);
                if (f == f && (f != 0 || !Single.IsNegativeInfinity(1 / f)))
                {
                    int i = (int)f;
                    if (-128 <= i && i <= 127 && f == (float)i)
                    {
                        ConstantWrapper.TranslateToILInt(il, i);
                        il.Emit(OpCodes.Conv_R4);
                    }
                    else
                    {
                        il.Emit(OpCodes.Ldc_R4, f);
                    }
                }
                else
                {
                    il.Emit(OpCodes.Ldc_R4, f);
                }
                Convert.Emit(this, il, Typeob.Single, rtype);
                return;

            case TypeCode.Double:
                double d = ic.ToDouble(null);
                if (d == d && (d != 0 || !Double.IsNegativeInfinity(1 / d)))
                {
                    int i = (int)d;
                    if (-128 <= i && i <= 127 && d == (double)i)
                    {
                        ConstantWrapper.TranslateToILInt(il, i);
                        il.Emit(OpCodes.Conv_R8);
                    }
                    else
                    {
                        il.Emit(OpCodes.Ldc_R8, d);
                    }
                }
                else
                {
                    il.Emit(OpCodes.Ldc_R8, d);
                }
                Convert.Emit(this, il, Typeob.Double, rtype);
                return;

            case TypeCode.Decimal:
                int[] bits = Decimal.GetBits(ic.ToDecimal(null));
                ConstantWrapper.TranslateToILInt(il, bits[0]);
                ConstantWrapper.TranslateToILInt(il, bits[1]);
                ConstantWrapper.TranslateToILInt(il, bits[2]);
                il.Emit(bits[3] < 0 ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0); //bool isNegative
                ConstantWrapper.TranslateToILInt(il, (bits[3] & 0x7FFFFFFF) >> 16);
                il.Emit(OpCodes.Newobj, CompilerGlobals.decimalConstructor);
                Convert.Emit(this, il, Typeob.Decimal, rtype);
                return;

            case TypeCode.DateTime:
                l = ic.ToDateTime(null).Ticks;
                il.Emit(OpCodes.Ldc_I8, l);
                Convert.Emit(this, il, Typeob.Int64, rtype);
                return;

            case TypeCode.String:
                String str = ic.ToString(null);
                if (rtype == Typeob.Char && str.Length == 1)
                {
                    ConstantWrapper.TranslateToILInt(il, (int)str[0]);
                    return;
                }
                il.Emit(OpCodes.Ldstr, str);
                Convert.Emit(this, il, Typeob.String, rtype);
                return;
            }
            if (val is Enum)
            {
                if (rtype == Typeob.String)
                {
                    this.TranslateToIL(il, val.ToString(), rtype);
                }
                else if (rtype.IsPrimitive)
                {
                    this.TranslateToIL(il, System.Convert.ChangeType(val, Enum.GetUnderlyingType(val.GetType())), rtype);
                }
                else
                {
                    Type et = val.GetType();
                    Type ut = Enum.GetUnderlyingType(et);
                    this.TranslateToIL(il, System.Convert.ChangeType(val, ut), ut);
                    il.Emit(OpCodes.Box, et);
                    Convert.Emit(this, il, Typeob.Object, rtype);
                }
                return;
            }
            if (val is EnumWrapper)
            {
                if (rtype == Typeob.String)
                {
                    this.TranslateToIL(il, val.ToString(), rtype);
                }
                else if (rtype.IsPrimitive)
                {
                    this.TranslateToIL(il, ((EnumWrapper)val).ToNumericValue(), rtype);
                }
                else
                {
                    Type et = ((EnumWrapper)val).type.owner.GetTypeBuilderOrEnumBuilder();
                    Type ut = ((EnumWrapper)val).value.GetType();
                    this.TranslateToIL(il, ((EnumWrapper)val).value, ut);
                    il.Emit(OpCodes.Box, et);
                    Convert.Emit(this, il, Typeob.Object, rtype);
                }
                return;
            }
            if (val is Type)
            {
                il.Emit(OpCodes.Ldtoken, (Type)val);
                il.Emit(OpCodes.Call, CompilerGlobals.getTypeFromHandleMethod);
                Convert.Emit(this, il, Typeob.Type, rtype);
            }
            else if (val is Namespace)
            {
                il.Emit(OpCodes.Ldstr, ((Namespace)val).Name);
                this.EmitILToLoadEngine(il);
                il.Emit(OpCodes.Call, CompilerGlobals.getNamespaceMethod);
                Convert.Emit(this, il, typeof(Namespace), rtype);
            }
            else if (val is ClassScope)
            {
                il.Emit(OpCodes.Ldtoken, ((ClassScope)val).GetTypeBuilderOrEnumBuilder());
                il.Emit(OpCodes.Call, CompilerGlobals.getTypeFromHandleMethod);
                Convert.Emit(this, il, Typeob.Type, rtype);
            }
            else if (val is TypedArray)
            {
                il.Emit(OpCodes.Ldtoken, Convert.ToType((TypedArray)val));
                il.Emit(OpCodes.Call, CompilerGlobals.getTypeFromHandleMethod);
                Convert.Emit(this, il, Typeob.Type, rtype);
            }
            else if (val is NumberObject)
            {
                this.TranslateToIL(il, ((NumberObject)val).value, Typeob.Object);
                this.EmitILToLoadEngine(il);
                il.Emit(OpCodes.Call, CompilerGlobals.toObjectMethod);
                Convert.Emit(this, il, Typeob.NumberObject, rtype);
            }
            else if (val is StringObject)
            {
                il.Emit(OpCodes.Ldstr, ((StringObject)val).value);
                this.EmitILToLoadEngine(il);
                il.Emit(OpCodes.Call, CompilerGlobals.toObjectMethod);
                Convert.Emit(this, il, Typeob.StringObject, rtype);
            }
            else if (val is BooleanObject)
            {
                il.Emit(((BooleanObject)val).value ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
                il.Emit(OpCodes.Box, Typeob.Boolean);
                this.EmitILToLoadEngine(il);
                il.Emit(OpCodes.Call, CompilerGlobals.toObjectMethod);
                Convert.Emit(this, il, Typeob.BooleanObject, rtype);
            }
            else if (val is ActiveXObjectConstructor)
            {
                il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("ActiveXObject").GetGetMethod());
                Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
            }
            else if (val is ArrayConstructor)
            {
                il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("Array").GetGetMethod());
                Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
            }
            else if (val is BooleanConstructor)
            {
                il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("Boolean").GetGetMethod());
                Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
            }
            else if (val is DateConstructor)
            {
                il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("Date").GetGetMethod());
                Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
            }
            else if (val is EnumeratorConstructor)
            {
                il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("Enumerator").GetGetMethod());
                Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
            }
            else if (val is ErrorConstructor)
            {
                ErrorConstructor error = (ErrorConstructor)val;
                if (error == ErrorConstructor.evalOb)
                {
                    il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("EvalError").GetGetMethod());
                }
                else if (error == ErrorConstructor.rangeOb)
                {
                    il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("RangeError").GetGetMethod());
                }
                else if (error == ErrorConstructor.referenceOb)
                {
                    il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("ReferenceError").GetGetMethod());
                }
                else if (error == ErrorConstructor.syntaxOb)
                {
                    il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("SyntaxError").GetGetMethod());
                }
                else if (error == ErrorConstructor.typeOb)
                {
                    il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("TypeError").GetGetMethod());
                }
                else if (error == ErrorConstructor.uriOb)
                {
                    il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("URIError").GetGetMethod());
                }
                else
                {
                    il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("Error").GetGetMethod());
                }
                Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
            }
            else if (val is FunctionConstructor)
            {
                il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("Function").GetGetMethod());
                Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
            }
            else if (val is MathObject)
            {
                il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("Math").GetGetMethod());
                Convert.Emit(this, il, Typeob.JSObject, rtype);
            }
            else if (val is NumberConstructor)
            {
                il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("Number").GetGetMethod());
                Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
            }
            else if (val is ObjectConstructor)
            {
                il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("Object").GetGetMethod());
                Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
            }
            else if (val is RegExpConstructor)
            {
                il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("RegExp").GetGetMethod());
                Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
            }
            else if (val is StringConstructor)
            {
                il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("String").GetGetMethod());
                Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
            }
            else if (val is VBArrayConstructor)
            {
                il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("VBArray").GetGetMethod());
                Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
            }
            else if (val is IntPtr)
            {
                il.Emit(OpCodes.Ldc_I8, (long)(IntPtr)val);
                il.Emit(OpCodes.Conv_I);
                Convert.Emit(this, il, typeof(IntPtr), rtype);
            }
            else if (val is UIntPtr)
            {
                il.Emit(OpCodes.Ldc_I8, (long)(UIntPtr)val);
                il.Emit(OpCodes.Conv_U);
                Convert.Emit(this, il, typeof(UIntPtr), rtype);
            }
            else if (val is Missing)
            {
                il.Emit(OpCodes.Ldsfld, CompilerGlobals.missingField);
                Convert.Emit(this, il, Typeob.Object, rtype);
            }
            else if (val is System.Reflection.Missing)
            {
                if (rtype.IsPrimitive)
                {
                    this.TranslateToIL(il, Double.NaN, rtype);
                }
                else if (rtype != Typeob.Object && !rtype.IsValueType)
                {
                    il.Emit(OpCodes.Ldnull);
                }
                else
                {
                    il.Emit(OpCodes.Ldsfld, CompilerGlobals.systemReflectionMissingField);
                    Convert.Emit(this, il, Typeob.Object, rtype);
                }
            }
            else if (val != this.value) //Value was coerced to some type we have no compile time knowlegde of
            {
                this.TranslateToIL(il, this.value, rtype);
            }
            else
            {
                throw new JScriptException(JSError.InternalError, this.context); //It should not be possible to wrap any other kind of object
            }
        }
Example #34
0
    private static bool FromConvertible <TTarget>(IConvertible converter, out TTarget result2)
    {
        object result    = null;
        bool   hasChange = false;

        switch (Type.GetTypeCode(typeof(TTarget)))
        {
        case TypeCode.Boolean:
            result    = converter.ToBoolean(CultureInfo.CurrentCulture);
            hasChange = true;
            break;

        case TypeCode.Char:
            result    = converter.ToChar(CultureInfo.CurrentCulture);
            hasChange = true;
            break;

        case TypeCode.SByte:
            result    = converter.ToSByte(CultureInfo.CurrentCulture);
            hasChange = true;
            break;

        case TypeCode.Byte:
            result    = converter.ToByte(CultureInfo.CurrentCulture);
            hasChange = true;
            break;

        case TypeCode.Int16:
            result    = converter.ToInt16(CultureInfo.CurrentCulture);
            hasChange = true;
            break;

        case TypeCode.UInt16:
            result    = converter.ToUInt16(CultureInfo.CurrentCulture);
            hasChange = true;
            break;

        case TypeCode.Int32:
            result    = converter.ToInt32(CultureInfo.CurrentCulture);
            hasChange = true;
            break;

        case TypeCode.UInt32:
            result    = converter.ToUInt32(CultureInfo.CurrentCulture);
            hasChange = true;
            break;

        case TypeCode.Int64:
            result    = converter.ToInt64(CultureInfo.CurrentCulture);
            hasChange = true;
            break;

        case TypeCode.UInt64:
            result    = converter.ToUInt64(CultureInfo.CurrentCulture);
            hasChange = true;
            break;

        case TypeCode.Single:
            result    = converter.ToSingle(CultureInfo.CurrentCulture);
            hasChange = true;
            break;

        case TypeCode.Double:
            result    = converter.ToDouble(CultureInfo.CurrentCulture);
            hasChange = true;
            break;

        case TypeCode.Decimal:
            result    = converter.ToDecimal(CultureInfo.CurrentCulture);
            hasChange = true;
            break;

        case TypeCode.DateTime:
            result    = converter.ToDateTime(CultureInfo.CurrentCulture);
            hasChange = true;
            break;

        case TypeCode.String:
            result    = converter.ToString(CultureInfo.CurrentCulture);
            hasChange = true;
            break;
        }
        result2 = result == null ? default(TTarget) : (TTarget)result;
        return(hasChange);
    }
Example #35
0
 internal static uint ToUint32(Object value, IConvertible ic){
   switch (Convert.GetTypeCode(value, ic)){
     case TypeCode.Empty: return 0;
     case TypeCode.DBNull: return 0;
     case TypeCode.Boolean: return ic.ToBoolean(null) ? (uint)1 : (uint)0;
     case TypeCode.Char: return (uint)ic.ToChar(null);
     case TypeCode.Byte:
     case TypeCode.UInt16:
     case TypeCode.UInt32: return ic.ToUInt32(null);
     case TypeCode.UInt64: return (uint)ic.ToUInt64(null);
     case TypeCode.SByte:
     case TypeCode.Int16:
     case TypeCode.Int32:
     case TypeCode.Int64: return (uint)ic.ToInt64(null);
     case TypeCode.Single:
     case TypeCode.Double: return (uint)Runtime.DoubleToInt64(ic.ToDouble(null));
     case TypeCode.Decimal: return (uint)Runtime.UncheckedDecimalToInt64(ic.ToDecimal(null));
     case TypeCode.Object:
     case TypeCode.DateTime:
       Object pval = Convert.ToPrimitive(value, PreferredType.Number, ref ic);
       if (pval != value)
         return Convert.ToUint32(pval, ic);
       else
         return 0;
     case TypeCode.String: return (uint)Runtime.DoubleToInt64(Convert.ToNumber(ic.ToString(null)));
   }
   return 0; //should never get here
 }
Example #36
0
        internal static Object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
        {
            Contract.Requires(value != null, "[Convert.DefaultToType]value!=null");
            if (targetType == null)
            {
                throw new ArgumentNullException("targetType");
            }
            Contract.EndContractBlock();

            if (value.GetType() == targetType)
                return value;

            if (targetType == CommonRuntimeTypes.Boolean)
                return value.ToBoolean(provider);
            if (targetType == CommonRuntimeTypes.Char)
                return value.ToChar(provider);
            if (targetType == CommonRuntimeTypes.SByte)
                return value.ToSByte(provider);
            if (targetType == CommonRuntimeTypes.Byte)
                return value.ToByte(provider);
            if (targetType == CommonRuntimeTypes.Int16)
                return value.ToInt16(provider);
            if (targetType == CommonRuntimeTypes.UInt16)
                return value.ToUInt16(provider);
            if (targetType == CommonRuntimeTypes.Int32)
                return value.ToInt32(provider);
            if (targetType == CommonRuntimeTypes.UInt32)
                return value.ToUInt32(provider);
            if (targetType == CommonRuntimeTypes.Int64)
                return value.ToInt64(provider);
            if (targetType == CommonRuntimeTypes.UInt64)
                return value.ToUInt64(provider);
            if (targetType == CommonRuntimeTypes.Single)
                return value.ToSingle(provider);
            if (targetType == CommonRuntimeTypes.Double)
                return value.ToDouble(provider);
            if (targetType == CommonRuntimeTypes.Decimal)
                return value.ToDecimal(provider);
            if (targetType == CommonRuntimeTypes.DateTime)
                return value.ToDateTime(provider);
            if (targetType == CommonRuntimeTypes.String)
                return value.ToString(provider);
            if (targetType == CommonRuntimeTypes.Object)
                return (Object)value;
            if (targetType == CommonRuntimeTypes.Enum)
                return (Enum)value;

            throw new InvalidCastException(SR.Format(SR.InvalidCast_FromTo, value.GetType().ToString(), targetType.Name));
        }
Example #37
0
        public virtual void WriteValue(object value)
        {
            if (value == null)
            {
                this.WriteNull();
            }
            else
            {
                if (ConvertUtils.IsConvertible(value))
                {
                    IConvertible convertible = ConvertUtils.ToConvertible(value);
                    switch (convertible.GetTypeCode())
                    {
                    case TypeCode.DBNull:
                        this.WriteNull();
                        return;

                    case TypeCode.Boolean:
                        this.WriteValue(convertible.ToBoolean((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.Char:
                        this.WriteValue(convertible.ToChar((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.SByte:
                        this.WriteValue(convertible.ToSByte((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.Byte:
                        this.WriteValue(convertible.ToByte((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.Int16:
                        this.WriteValue(convertible.ToInt16((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.UInt16:
                        this.WriteValue(convertible.ToUInt16((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.Int32:
                        this.WriteValue(convertible.ToInt32((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.UInt32:
                        this.WriteValue(convertible.ToUInt32((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.Int64:
                        this.WriteValue(convertible.ToInt64((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.UInt64:
                        this.WriteValue(convertible.ToUInt64((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.Single:
                        this.WriteValue(convertible.ToSingle((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.Double:
                        this.WriteValue(convertible.ToDouble((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.Decimal:
                        this.WriteValue(convertible.ToDecimal((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.DateTime:
                        this.WriteValue(convertible.ToDateTime((IFormatProvider)CultureInfo.InvariantCulture));
                        return;

                    case TypeCode.String:
                        this.WriteValue(convertible.ToString((IFormatProvider)CultureInfo.InvariantCulture));
                        return;
                    }
                }
                else if (value is byte[])
                {
                    this.WriteValue((byte[])value);
                    return;
                }
                else if (value is Guid)
                {
                    this.WriteValue((Guid)value);
                    return;
                }
                else if (value is Uri)
                {
                    this.WriteValue((Uri)value);
                    return;
                }
                else if (value is TimeSpan)
                {
                    this.WriteValue((TimeSpan)value);
                    return;
                }
                throw JsonWriterException.Create(this, StringUtils.FormatWith("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.", (IFormatProvider)CultureInfo.InvariantCulture, (object)value.GetType()), (Exception)null);
            }
        }
Example #38
0
        public decimal ToDecimal(IFormatProvider provider)
        {
            IConvertible o = GetConvertible();

            return(o.ToDecimal(provider));
        }
 private static byte DecimalToByte(IConvertible ValueInterface)
 {
     return(Convert.ToByte(ValueInterface.ToDecimal(null)));
 }
      private static double JScriptCompare2(Object v1, Object v2, IConvertible ic1, IConvertible ic2, TypeCode t1, TypeCode t2){
        if (t1 == TypeCode.Object){
          v1 = Convert.ToPrimitive(v1, PreferredType.Number, ref ic1);
          t1 = Convert.GetTypeCode(v1, ic1);
        }
        if (t2 == TypeCode.Object){
          v2 = Convert.ToPrimitive(v2, PreferredType.Number, ref ic2);
          t2 = Convert.GetTypeCode(v2, ic2);
        }
        switch (t1){
          case TypeCode.Char:
            if (t2 == TypeCode.String) return String.CompareOrdinal(Convert.ToString(v1, ic1), ic2.ToString(null));
            goto case TypeCode.UInt16;

          case TypeCode.SByte:
          case TypeCode.Byte:
          case TypeCode.Int16:
          case TypeCode.UInt16:
          case TypeCode.Int32:
          case TypeCode.UInt32:
          case TypeCode.Int64:
            long l = ic1.ToInt64(null);
            switch (t2){
              case TypeCode.SByte:
              case TypeCode.Byte:
              case TypeCode.Int16:
              case TypeCode.UInt16:
              case TypeCode.Int32:
              case TypeCode.UInt32:
              case TypeCode.Int64:
                return l - ic2.ToInt64(null);
              case TypeCode.UInt64:
                if (l < 0) return -1;
                ulong ul2 = ic2.ToUInt64(null);
                if (((ulong)l) < ul2) return -1;
                if (((ulong)l) == ul2) return 0;
                return 1;
              case TypeCode.Single:
              case TypeCode.Double:
                return ((double)l) - ic2.ToDouble(null);
              case TypeCode.Decimal:
                return (double)(new Decimal(l) - ic2.ToDecimal(null));
              default:
                Object bd2 = Convert.ToNumber(v2, ic2);
                return JScriptCompare2(v1, bd2, ic1, Convert.GetIConvertible(bd2), t1, TypeCode.Double);
            }

          case TypeCode.UInt64:
            ulong ul = ic1.ToUInt64(null);
            switch (t2){
              case TypeCode.SByte:
              case TypeCode.Byte:
              case TypeCode.Int16:
              case TypeCode.UInt16:
              case TypeCode.Int32:
              case TypeCode.UInt32:
              case TypeCode.Int64:
                long l2 = ic2.ToInt64(null);
                if (l2 < 0) return 1;
                if (ul == (ulong)l2) return 0;
                return -1;
              case TypeCode.UInt64:
                ulong ul2 = ic2.ToUInt64(null);
                if (ul < ul2) return -1;
                if (ul == ul2) return 0;
                return 1;
              case TypeCode.Single:
              case TypeCode.Double:
                return ((double)ul) - ic2.ToDouble(null);
              case TypeCode.Decimal:
                return (double)(new Decimal(ul) - ic2.ToDecimal(null));
              default:
                Object bd2 = Convert.ToNumber(v2, ic2);
                return JScriptCompare2(v1, bd2, ic1, Convert.GetIConvertible(bd2), t1, TypeCode.Double);
            }

          case TypeCode.Decimal:
            Decimal dec1 = ic1.ToDecimal(null);
            switch (t2){
              case TypeCode.SByte:
              case TypeCode.Byte:
              case TypeCode.Int16:
              case TypeCode.UInt16:
              case TypeCode.Int32:
              case TypeCode.UInt32:
              case TypeCode.Int64:
                return (double)(dec1 - new Decimal(ic2.ToInt64(null)));
              case TypeCode.UInt64:
                return (double)(dec1 - new Decimal(ic2.ToUInt64(null)));
              case TypeCode.Single:
              case TypeCode.Double:
                return (double)(dec1 - new Decimal(ic2.ToDouble(null)));
              case TypeCode.Decimal:
                return (double)(dec1 - ic2.ToDecimal(null));
              default:
                return (double)(dec1 - new Decimal(Convert.ToNumber(v2, ic2)));
            }

          case TypeCode.String:
            switch (t2){
              case TypeCode.String: return String.CompareOrdinal(ic1.ToString(null), ic2.ToString(null));
              case TypeCode.Char : return String.CompareOrdinal(ic1.ToString(null), Convert.ToString(v2, ic2));
            }
            goto default;

          default:
            double d1 = Convert.ToNumber(v1, ic1);
            double d2 = Convert.ToNumber(v2, ic2);
            if (d1 == d2) return 0; //d1 and d2 could be infinities
            return d1 - d2;
        }
      }
		internal static object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
		{
			if (targetType == null)
			{
				throw new ArgumentNullException("targetType");
			}
			RuntimeType left = targetType as RuntimeType;
			if (left != null)
			{
				if (value.GetType() == targetType)
				{
					return value;
				}
				if (left == Convert.ConvertTypes[3])
				{
					return value.ToBoolean(provider);
				}
				if (left == Convert.ConvertTypes[4])
				{
					return value.ToChar(provider);
				}
				if (left == Convert.ConvertTypes[5])
				{
					return value.ToSByte(provider);
				}
				if (left == Convert.ConvertTypes[6])
				{
					return value.ToByte(provider);
				}
				if (left == Convert.ConvertTypes[7])
				{
					return value.ToInt16(provider);
				}
				if (left == Convert.ConvertTypes[8])
				{
					return value.ToUInt16(provider);
				}
				if (left == Convert.ConvertTypes[9])
				{
					return value.ToInt32(provider);
				}
				if (left == Convert.ConvertTypes[10])
				{
					return value.ToUInt32(provider);
				}
				if (left == Convert.ConvertTypes[11])
				{
					return value.ToInt64(provider);
				}
				if (left == Convert.ConvertTypes[12])
				{
					return value.ToUInt64(provider);
				}
				if (left == Convert.ConvertTypes[13])
				{
					return value.ToSingle(provider);
				}
				if (left == Convert.ConvertTypes[14])
				{
					return value.ToDouble(provider);
				}
				if (left == Convert.ConvertTypes[15])
				{
					return value.ToDecimal(provider);
				}
				if (left == Convert.ConvertTypes[16])
				{
					return value.ToDateTime(provider);
				}
				if (left == Convert.ConvertTypes[18])
				{
					return value.ToString(provider);
				}
				if (left == Convert.ConvertTypes[1])
				{
					return value;
				}
				if (left == Convert.EnumType)
				{
					return (Enum)value;
				}
				if (left == Convert.ConvertTypes[2])
				{
					throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull"));
				}
				if (left == Convert.ConvertTypes[0])
				{
					throw new InvalidCastException(Environment.GetResourceString("InvalidCast_Empty"));
				}
			}
			throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", new object[]
			{
				value.GetType().FullName, 
				targetType.FullName
			}));
		}
Example #42
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        internal static void MarshalHelperConvertObjectToVariant(Object o, ref Variant v)
        {
            IConvertible ic = o as IConvertible;

            if (o == null)
            {
                v = Empty;
            }
            else if (ic == null)
            {
                // This path should eventually go away. But until
                // the work is done to have all of our wrapper types implement
                // IConvertible, this is a cheapo way to get the work done.
                v = new Variant(o);
            }
            else
            {
                IFormatProvider provider = CultureInfo.InvariantCulture;
                switch (ic.GetTypeCode())
                {
                case TypeCode.Empty:
                    v = Empty;
                    break;

                case TypeCode.Object:
                    v = new Variant((Object)o);
                    break;

                case TypeCode.DBNull:
                    v = DBNull;
                    break;

                case TypeCode.Boolean:
                    v = new Variant(ic.ToBoolean(provider));
                    break;

                case TypeCode.Char:
                    v = new Variant(ic.ToChar(provider));
                    break;

                case TypeCode.SByte:
                    v = new Variant(ic.ToSByte(provider));
                    break;

                case TypeCode.Byte:
                    v = new Variant(ic.ToByte(provider));
                    break;

                case TypeCode.Int16:
                    v = new Variant(ic.ToInt16(provider));
                    break;

                case TypeCode.UInt16:
                    v = new Variant(ic.ToUInt16(provider));
                    break;

                case TypeCode.Int32:
                    v = new Variant(ic.ToInt32(provider));
                    break;

                case TypeCode.UInt32:
                    v = new Variant(ic.ToUInt32(provider));
                    break;

                case TypeCode.Int64:
                    v = new Variant(ic.ToInt64(provider));
                    break;

                case TypeCode.UInt64:
                    v = new Variant(ic.ToUInt64(provider));
                    break;

                case TypeCode.Single:
                    v = new Variant(ic.ToSingle(provider));
                    break;

                case TypeCode.Double:
                    v = new Variant(ic.ToDouble(provider));
                    break;

                case TypeCode.Decimal:
                    v = new Variant(ic.ToDecimal(provider));
                    break;

                case TypeCode.DateTime:
                    v = new Variant(ic.ToDateTime(provider));
                    break;

                case TypeCode.String:
                    v = new Variant(ic.ToString(provider));
                    break;

                default:
                    throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnknownTypeCode", ic.GetTypeCode()));
                }
            }
        }
 public static Literal DoAddOvf(IConvertible ic1, IConvertible ic2, TypeCode code1, TypeCode code2, BinaryExpression binaryExpression){
   TypeNode type = SystemTypes.Object;
   object val = null;
   checked{switch(code1){
     case TypeCode.SByte:
     case TypeCode.Int16:
     case TypeCode.Int32:
       int i = ic1.ToInt32(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
         case TypeCode.Int32: 
         case TypeCode.Byte:
         case TypeCode.Char:
         case TypeCode.UInt16:
           val = i + ic2.ToInt32(null); 
           type = SystemTypes.Int32;
           break;
         case TypeCode.Int64: 
         case TypeCode.UInt32: 
         case TypeCode.UInt64: 
           val = i + ic2.ToInt64(null); 
           type = SystemTypes.Int64;
           break;
         case TypeCode.Single: 
           val = i + ic2.ToSingle(null); 
           type = SystemTypes.Single;
           break;
         case TypeCode.Double: 
           val = i + ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = i + ic2.ToDecimal(null);
           type = SystemTypes.Decimal;
           break;
         default: return null;
       }
       break;
     case TypeCode.Byte:
     case TypeCode.UInt16:
       ushort us = ic1.ToUInt16(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
         case TypeCode.Int32: 
           val = us + ic2.ToInt32(null); 
           type = SystemTypes.Int32;
           break;
         case TypeCode.Byte:
         case TypeCode.UInt16:
         case TypeCode.Char:
         case TypeCode.UInt32: 
           val = us + ic2.ToUInt32(null); 
           type = SystemTypes.UInt32;
           break;
         case TypeCode.Int64: 
           val = us + ic2.ToInt64(null); 
           type = SystemTypes.Int64;
           break;
         case TypeCode.UInt64: 
           val = us + ic2.ToUInt64(null); 
           type = SystemTypes.UInt64;
           break;
         case TypeCode.Single: 
           val = us + ic2.ToSingle(null); 
           type = SystemTypes.Single;
           break;
         case TypeCode.Double: 
           val = us + ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = us + ic2.ToDecimal(null);
           type = SystemTypes.Decimal;
           break;
         default: return null;
       }
       break;
     case TypeCode.UInt32:
       uint ui = ic1.ToUInt32(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
         case TypeCode.Int32: 
         case TypeCode.Int64: 
           val = ui + ic2.ToInt64(null); 
           type = SystemTypes.Int64;
           break;
         case TypeCode.Byte:
         case TypeCode.UInt16:
         case TypeCode.Char:
         case TypeCode.UInt32: 
           val = ui + ic2.ToUInt32(null); 
           type = SystemTypes.UInt32;
           break;
         case TypeCode.UInt64: 
           val = ui + ic2.ToUInt64(null); 
           type = SystemTypes.UInt64;
           break;
         case TypeCode.Single: 
           val = ui + ic2.ToSingle(null); 
           type = SystemTypes.Single;
           break;
         case TypeCode.Double: 
           val = ui + ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = ui + ic2.ToDecimal(null);
           type = SystemTypes.Decimal;
           break;
         default: return null;
       }
       break;
     case TypeCode.Int64:
       long l = ic1.ToInt64(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
         case TypeCode.Int32: 
         case TypeCode.Int64: 
           val = l + ic2.ToInt64(null); 
           type = SystemTypes.Int64;
           break;
         case TypeCode.Byte:
         case TypeCode.UInt16:
         case TypeCode.Char:
         case TypeCode.UInt32: 
           val = l + ic2.ToUInt32(null); 
           type = SystemTypes.UInt32;
           break;
         case TypeCode.UInt64: 
           if (l >= 0){
             val = ((ulong)l) + ic2.ToUInt64(null);
             type = SystemTypes.UInt64;
           }else{
             val = l + (long)ic2.ToUInt64(null); 
             type = SystemTypes.Int64;
           }
           break;
         case TypeCode.Single: 
           val = l + ic2.ToSingle(null); 
           type = SystemTypes.Single;
           break;
         case TypeCode.Double: 
           val = l + ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = l + ic2.ToDecimal(null);
           type = SystemTypes.Decimal;
           break;
         default: return null;
       }
       break;
     case TypeCode.UInt64:
       ulong ul = ic1.ToUInt64(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
         case TypeCode.Int32: 
         case TypeCode.Byte:
         case TypeCode.UInt16:
         case TypeCode.Char:
         case TypeCode.UInt32: 
           val = ul + ic2.ToUInt32(null); 
           type = SystemTypes.UInt32;
           break;
         case TypeCode.Int64: 
         case TypeCode.UInt64: 
           val = ul + ic2.ToUInt64(null); 
           type = SystemTypes.UInt64;
           break;
         case TypeCode.Single: 
           val = ul + ic2.ToSingle(null); 
           type = SystemTypes.Single;
           break;
         case TypeCode.Double: 
           val = ul + ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = ul + ic2.ToDecimal(null);
           type = SystemTypes.Decimal;
           break;
         default: return null;
       }
       break;
     case TypeCode.Single:
       float f = ic1.ToSingle(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
           val = f + ic2.ToInt16(null);
           type = SystemTypes.Single;
           break;
         case TypeCode.Int32: 
         case TypeCode.Int64: 
           val = f + (double)ic2.ToInt64(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Byte:
         case TypeCode.Char:
         case TypeCode.UInt16:
           val = f + ic2.ToUInt16(null); 
           type = SystemTypes.Single;
           break;
         case TypeCode.UInt32: 
         case TypeCode.UInt64: 
           val = f + (double)ic2.ToUInt64(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Single: 
         case TypeCode.Double: 
           val = f + ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = f + (double)ic2.ToDecimal(null);
           type = SystemTypes.Double;
           break;
         default: return null;
       }
       break;
     case TypeCode.Double:
       double d = ic1.ToDouble(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Int16:
         case TypeCode.Int32: 
         case TypeCode.Int64: 
           val = d + ic2.ToInt64(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Byte:
         case TypeCode.Char:
         case TypeCode.UInt16:
         case TypeCode.UInt32: 
         case TypeCode.UInt64: 
           val = d + ic2.ToUInt64(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Single: 
         case TypeCode.Double: 
           val = d + ic2.ToDouble(null); 
           type = SystemTypes.Double;
           break;
         case TypeCode.Decimal:
           val = d + (double)ic2.ToDecimal(null);
           type = SystemTypes.Double;
           break;
         default: return null;
       }
       break;
     case TypeCode.Decimal:
       decimal dec = ic1.ToDecimal(null);
       switch(code2){
         case TypeCode.SByte:
         case TypeCode.Byte:
         case TypeCode.Int16:
         case TypeCode.UInt16:
         case TypeCode.Char:
         case TypeCode.Int32: 
           val = dec + ic2.ToInt32(null); 
           type = SystemTypes.Decimal;
           break;
         case TypeCode.UInt32: 
         case TypeCode.Int64: 
         case TypeCode.UInt64: 
           val = dec + ic2.ToInt64(null); 
           type = SystemTypes.Decimal;
           break;
         case TypeCode.Decimal:
           val = dec + ic2.ToDecimal(null);
           type = SystemTypes.Decimal;
           break;
         default: return null;
       }
       break;
     case TypeCode.String:
       string str = ic1.ToString(null);
       switch (code2) {
         case TypeCode.String:
           val = str + ic2.ToString(null);
           type = SystemTypes.String;
           break;
         default: return null;
       }
       break;
     default: return null;
   }}
   return new Literal(val, type, binaryExpression.SourceContext);
 }
Example #44
0
        [System.Security.SecurityCritical]  // auto-generated
        internal static void MarshalHelperCastVariant(Object pValue, int vt, ref Variant v)
        {
            IConvertible iv = pValue as IConvertible;

            if (iv == null)
            {
                switch (vt)
                {
                case 9:     /*VT_DISPATCH*/
                    v = new Variant(new DispatchWrapper(pValue));
                    break;

                case 12:     /*VT_VARIANT*/
                    v = new Variant(pValue);
                    break;

                case 13:     /*VT_UNKNOWN*/
                    v = new Variant(new UnknownWrapper(pValue));
                    break;

                case 36:     /*VT_RECORD*/
                    v = new Variant(pValue);
                    break;

                case 8:     /*VT_BSTR*/
                    if (pValue == null)
                    {
                        v         = new Variant(null);
                        v.m_flags = CV_STRING;
                    }
                    else
                    {
                        throw new InvalidCastException(Environment.GetResourceString("InvalidCast_CannotCoerceByRefVariant"));
                    }
                    break;

                default:
                    throw new InvalidCastException(Environment.GetResourceString("InvalidCast_CannotCoerceByRefVariant"));
                }
            }
            else
            {
                IFormatProvider provider = CultureInfo.InvariantCulture;
                switch (vt)
                {
                case 0:     /*VT_EMPTY*/
                    v = Empty;
                    break;

                case 1:     /*VT_NULL*/
                    v = DBNull;
                    break;

                case 2:     /*VT_I2*/
                    v = new Variant(iv.ToInt16(provider));
                    break;

                case 3:     /*VT_I4*/
                    v = new Variant(iv.ToInt32(provider));
                    break;

                case 4:     /*VT_R4*/
                    v = new Variant(iv.ToSingle(provider));
                    break;

                case 5:     /*VT_R8*/
                    v = new Variant(iv.ToDouble(provider));
                    break;

                case 6:     /*VT_CY*/
                    v = new Variant(new CurrencyWrapper(iv.ToDecimal(provider)));
                    break;

                case 7:     /*VT_DATE*/
                    v = new Variant(iv.ToDateTime(provider));
                    break;

                case 8:     /*VT_BSTR*/
                    v = new Variant(iv.ToString(provider));
                    break;

                case 9:     /*VT_DISPATCH*/
                    v = new Variant(new DispatchWrapper((Object)iv));
                    break;

                case 10:     /*VT_ERROR*/
                    v = new Variant(new ErrorWrapper(iv.ToInt32(provider)));
                    break;

                case 11:     /*VT_BOOL*/
                    v = new Variant(iv.ToBoolean(provider));
                    break;

                case 12:     /*VT_VARIANT*/
                    v = new Variant((Object)iv);
                    break;

                case 13:     /*VT_UNKNOWN*/
                    v = new Variant(new UnknownWrapper((Object)iv));
                    break;

                case 14:     /*VT_DECIMAL*/
                    v = new Variant(iv.ToDecimal(provider));
                    break;

                // case 15: /*unused*/
                //  NOT SUPPORTED

                case 16:     /*VT_I1*/
                    v = new Variant(iv.ToSByte(provider));
                    break;

                case 17:     /*VT_UI1*/
                    v = new Variant(iv.ToByte(provider));
                    break;

                case 18:     /*VT_UI2*/
                    v = new Variant(iv.ToUInt16(provider));
                    break;

                case 19:     /*VT_UI4*/
                    v = new Variant(iv.ToUInt32(provider));
                    break;

                case 20:     /*VT_I8*/
                    v = new Variant(iv.ToInt64(provider));
                    break;

                case 21:     /*VT_UI8*/
                    v = new Variant(iv.ToUInt64(provider));
                    break;

                case 22:     /*VT_INT*/
                    v = new Variant(iv.ToInt32(provider));
                    break;

                case 23:     /*VT_UINT*/
                    v = new Variant(iv.ToUInt32(provider));
                    break;

                default:
                    throw new InvalidCastException(Environment.GetResourceString("InvalidCast_CannotCoerceByRefVariant"));
                }
            }
        }
Example #45
0
        /// <summary>
        /// Converts the <see cref="Object"/> to its JSON string representation.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <returns>A JSON string representation of the <see cref="Object"/>.</returns>
        public static string ToString(object value)
        {
            if (value == null)
            {
                return(Null);
            }

            IConvertible convertible = value as IConvertible;

            if (convertible != null)
            {
                switch (convertible.GetTypeCode())
                {
                case TypeCode.String:
                    return(ToString(convertible.ToString(CultureInfo.InvariantCulture)));

                case TypeCode.Char:
                    return(ToString(convertible.ToChar(CultureInfo.InvariantCulture)));

                case TypeCode.Boolean:
                    return(ToString(convertible.ToBoolean(CultureInfo.InvariantCulture)));

                case TypeCode.SByte:
                    return(ToString(convertible.ToSByte(CultureInfo.InvariantCulture)));

                case TypeCode.Int16:
                    return(ToString(convertible.ToInt16(CultureInfo.InvariantCulture)));

                case TypeCode.UInt16:
                    return(ToString(convertible.ToUInt16(CultureInfo.InvariantCulture)));

                case TypeCode.Int32:
                    return(ToString(convertible.ToInt32(CultureInfo.InvariantCulture)));

                case TypeCode.Byte:
                    return(ToString(convertible.ToByte(CultureInfo.InvariantCulture)));

                case TypeCode.UInt32:
                    return(ToString(convertible.ToUInt32(CultureInfo.InvariantCulture)));

                case TypeCode.Int64:
                    return(ToString(convertible.ToInt64(CultureInfo.InvariantCulture)));

                case TypeCode.UInt64:
                    return(ToString(convertible.ToUInt64(CultureInfo.InvariantCulture)));

                case TypeCode.Single:
                    return(ToString(convertible.ToSingle(CultureInfo.InvariantCulture)));

                case TypeCode.Double:
                    return(ToString(convertible.ToDouble(CultureInfo.InvariantCulture)));

                case TypeCode.DateTime:
                    return(ToString(convertible.ToDateTime(CultureInfo.InvariantCulture)));

                case TypeCode.Decimal:
                    return(ToString(convertible.ToDecimal(CultureInfo.InvariantCulture)));

                case TypeCode.DBNull:
                    return(Null);
                }
            }
            else if (value is DateTimeOffset)
            {
                return(ToString((DateTimeOffset)value));
            }
            else if (value is Guid)
            {
                return(ToString((Guid)value));
            }
            else if (value is Uri)
            {
                return(ToString((Uri)value));
            }
            else if (value is TimeSpan)
            {
                return(ToString((TimeSpan)value));
            }

            throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
        }
Example #46
0
 decimal IConvertible.ToDecimal(IFormatProvider provider)
 {
     return(_value.ToDecimal(provider));
 }
 private static bool DecimalToBoolean(IConvertible ValueInterface)
 {
     return(Convert.ToBoolean(ValueInterface.ToDecimal(null)));
 }
Example #48
0
 public decimal ToDecimal()
 {
     return(_value.ToDecimal(null));
 }
Example #49
0
 internal static bool ToBoolean(Object value, IConvertible ic){
   switch (Convert.GetTypeCode(value, ic)){
     case TypeCode.Empty: return false;
     case TypeCode.Object:
       if (value is Missing || value is System.Reflection.Missing) return false;
       Type t = value.GetType();
       MethodInfo meth = t.GetMethod("op_True", BindingFlags.ExactBinding|BindingFlags.Public|BindingFlags.Static, null, new Type[]{t}, null);
       if (meth != null && (meth.Attributes & MethodAttributes.SpecialName) != 0 && meth.ReturnType == Typeob.Boolean){
         meth = new JSMethodInfo(meth);
         return (bool)meth.Invoke(null, BindingFlags.SuppressChangeType, null, new Object[]{value}, null);
       }
       return true;
     case TypeCode.DBNull: return false;
     case TypeCode.Boolean: return ic.ToBoolean(null);
     case TypeCode.Char: return ic.ToChar(null) != (Char)0;
     case TypeCode.SByte:
     case TypeCode.Byte:
     case TypeCode.Int16:
     case TypeCode.UInt16:
     case TypeCode.Int32: return ic.ToInt32(null) != 0;
     case TypeCode.UInt32:
     case TypeCode.Int64: return ic.ToInt64(null) != 0;
     case TypeCode.UInt64: return ic.ToUInt64(null) != 0;
     case TypeCode.Single:
     case TypeCode.Double:
       double d = ic.ToDouble(null);
       if (d != d) return false; else return d != 0;
     case TypeCode.Decimal: return ic.ToDecimal(null) != (Decimal)0;
     case TypeCode.DateTime: return true;
     case TypeCode.String: return ic.ToString(null).Length != 0;
   }
   return false; //should never get here
 }
Example #50
0
 private static long DecimalToLong(IConvertible ValueInterface)
 {
     return(Convert.ToInt64(ValueInterface.ToDecimal(null)));
 }
Example #51
0
 private static float DecimalToSingle(IConvertible ValueInterface)
 {
     return(Convert.ToSingle(ValueInterface.ToDecimal(null)));
 }
Example #52
0
        internal static void MarshalHelperConvertObjectToVariant(object o, ref System.Variant v)
        {
            IConvertible convertible = RemotingServices.IsTransparentProxy(o) ? null : (o as IConvertible);

            if (o == null)
            {
                v = System.Variant.Empty;
                return;
            }
            if (convertible == null)
            {
                v = new System.Variant(o);
                return;
            }
            IFormatProvider invariantCulture = CultureInfo.InvariantCulture;

            switch (convertible.GetTypeCode())
            {
            case TypeCode.Empty:
                v = System.Variant.Empty;
                return;

            case TypeCode.Object:
                v = new System.Variant(o);
                return;

            case TypeCode.DBNull:
                v = System.Variant.DBNull;
                return;

            case TypeCode.Boolean:
                v = new System.Variant(convertible.ToBoolean(invariantCulture));
                return;

            case TypeCode.Char:
                v = new System.Variant(convertible.ToChar(invariantCulture));
                return;

            case TypeCode.SByte:
                v = new System.Variant(convertible.ToSByte(invariantCulture));
                return;

            case TypeCode.Byte:
                v = new System.Variant(convertible.ToByte(invariantCulture));
                return;

            case TypeCode.Int16:
                v = new System.Variant(convertible.ToInt16(invariantCulture));
                return;

            case TypeCode.UInt16:
                v = new System.Variant(convertible.ToUInt16(invariantCulture));
                return;

            case TypeCode.Int32:
                v = new System.Variant(convertible.ToInt32(invariantCulture));
                return;

            case TypeCode.UInt32:
                v = new System.Variant(convertible.ToUInt32(invariantCulture));
                return;

            case TypeCode.Int64:
                v = new System.Variant(convertible.ToInt64(invariantCulture));
                return;

            case TypeCode.UInt64:
                v = new System.Variant(convertible.ToUInt64(invariantCulture));
                return;

            case TypeCode.Single:
                v = new System.Variant(convertible.ToSingle(invariantCulture));
                return;

            case TypeCode.Double:
                v = new System.Variant(convertible.ToDouble(invariantCulture));
                return;

            case TypeCode.Decimal:
                v = new System.Variant(convertible.ToDecimal(invariantCulture));
                return;

            case TypeCode.DateTime:
                v = new System.Variant(convertible.ToDateTime(invariantCulture));
                return;

            case TypeCode.String:
                v = new System.Variant(convertible.ToString(invariantCulture));
                return;
            }
            throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnknownTypeCode", new object[]
            {
                convertible.GetTypeCode()
            }));
        }
Example #53
0
        public void SetAsIConvertible(IConvertible value) {
            Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise

            TypeCode tc = value.GetTypeCode();
            CultureInfo ci = CultureInfo.CurrentCulture;

            switch (tc) {
                case TypeCode.Empty: break;
                case TypeCode.Object: AsUnknown = value; break;
                case TypeCode.DBNull: SetAsNull(); break;
                case TypeCode.Boolean: AsBool = value.ToBoolean(ci); break;
                case TypeCode.Char: AsUi2 = value.ToChar(ci); break;
                case TypeCode.SByte: AsI1 = value.ToSByte(ci); break;
                case TypeCode.Byte: AsUi1 = value.ToByte(ci); break;
                case TypeCode.Int16: AsI2 = value.ToInt16(ci); break;
                case TypeCode.UInt16: AsUi2 = value.ToUInt16(ci); break;
                case TypeCode.Int32: AsI4 = value.ToInt32(ci); break;
                case TypeCode.UInt32: AsUi4 = value.ToUInt32(ci); break;
                case TypeCode.Int64: AsI8 = value.ToInt64(ci); break;
                case TypeCode.UInt64: AsI8 = value.ToInt64(ci); break;
                case TypeCode.Single: AsR4 = value.ToSingle(ci); break;
                case TypeCode.Double: AsR8 = value.ToDouble(ci); break;
                case TypeCode.Decimal: AsDecimal = value.ToDecimal(ci); break;
                case TypeCode.DateTime: AsDate = value.ToDateTime(ci); break;
                case TypeCode.String: AsBstr = value.ToString(ci); break;

                default:
                    throw Assert.Unreachable;
            }
        }
Example #54
0
        internal static void MarshalHelperCastVariant(object pValue, int vt, ref System.Variant v)
        {
            IConvertible convertible = pValue as IConvertible;

            if (convertible == null)
            {
                switch (vt)
                {
                case 8:
                    if (pValue == null)
                    {
                        v         = new System.Variant(null);
                        v.m_flags = 14;
                        return;
                    }
                    throw new InvalidCastException(Environment.GetResourceString("InvalidCast_CannotCoerceByRefVariant"));

                case 9:
                    v = new System.Variant(new DispatchWrapper(pValue));
                    return;

                case 10:
                case 11:
                    break;

                case 12:
                    v = new System.Variant(pValue);
                    return;

                case 13:
                    v = new System.Variant(new UnknownWrapper(pValue));
                    return;

                default:
                    if (vt == 36)
                    {
                        v = new System.Variant(pValue);
                        return;
                    }
                    break;
                }
                throw new InvalidCastException(Environment.GetResourceString("InvalidCast_CannotCoerceByRefVariant"));
            }
            IFormatProvider invariantCulture = CultureInfo.InvariantCulture;

            switch (vt)
            {
            case 0:
                v = System.Variant.Empty;
                return;

            case 1:
                v = System.Variant.DBNull;
                return;

            case 2:
                v = new System.Variant(convertible.ToInt16(invariantCulture));
                return;

            case 3:
                v = new System.Variant(convertible.ToInt32(invariantCulture));
                return;

            case 4:
                v = new System.Variant(convertible.ToSingle(invariantCulture));
                return;

            case 5:
                v = new System.Variant(convertible.ToDouble(invariantCulture));
                return;

            case 6:
                v = new System.Variant(new CurrencyWrapper(convertible.ToDecimal(invariantCulture)));
                return;

            case 7:
                v = new System.Variant(convertible.ToDateTime(invariantCulture));
                return;

            case 8:
                v = new System.Variant(convertible.ToString(invariantCulture));
                return;

            case 9:
                v = new System.Variant(new DispatchWrapper(convertible));
                return;

            case 10:
                v = new System.Variant(new ErrorWrapper(convertible.ToInt32(invariantCulture)));
                return;

            case 11:
                v = new System.Variant(convertible.ToBoolean(invariantCulture));
                return;

            case 12:
                v = new System.Variant(convertible);
                return;

            case 13:
                v = new System.Variant(new UnknownWrapper(convertible));
                return;

            case 14:
                v = new System.Variant(convertible.ToDecimal(invariantCulture));
                return;

            case 16:
                v = new System.Variant(convertible.ToSByte(invariantCulture));
                return;

            case 17:
                v = new System.Variant(convertible.ToByte(invariantCulture));
                return;

            case 18:
                v = new System.Variant(convertible.ToUInt16(invariantCulture));
                return;

            case 19:
                v = new System.Variant(convertible.ToUInt32(invariantCulture));
                return;

            case 20:
                v = new System.Variant(convertible.ToInt64(invariantCulture));
                return;

            case 21:
                v = new System.Variant(convertible.ToUInt64(invariantCulture));
                return;

            case 22:
                v = new System.Variant(convertible.ToInt32(invariantCulture));
                return;

            case 23:
                v = new System.Variant(convertible.ToUInt32(invariantCulture));
                return;
            }
            throw new InvalidCastException(Environment.GetResourceString("InvalidCast_CannotCoerceByRefVariant"));
        }
 private static int DecimalToInteger(IConvertible ValueInterface)
 {
     return(Convert.ToInt32(ValueInterface.ToDecimal(null)));
 }
Example #56
0
 private static byte DecimalToByte(IConvertible ValueInterface)
 {
     return(Convert.ToByte(ValueInterface.ToDecimal((IFormatProvider)null)));
 }
Example #57
0
        private static double JScriptCompare2(Object v1, Object v2, IConvertible ic1, IConvertible ic2, TypeCode t1, TypeCode t2)
        {
            if (t1 == TypeCode.Object)
            {
                v1 = Convert.ToPrimitive(v1, PreferredType.Number, ref ic1);
                t1 = Convert.GetTypeCode(v1, ic1);
            }
            if (t2 == TypeCode.Object)
            {
                v2 = Convert.ToPrimitive(v2, PreferredType.Number, ref ic2);
                t2 = Convert.GetTypeCode(v2, ic2);
            }
            switch (t1)
            {
            case TypeCode.Char:
                if (t2 == TypeCode.String)
                {
                    return(String.CompareOrdinal(Convert.ToString(v1, ic1), ic2.ToString(null)));
                }
                goto case TypeCode.UInt16;

            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Int32:
            case TypeCode.UInt32:
            case TypeCode.Int64:
                long l = ic1.ToInt64(null);
                switch (t2)
                {
                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    return(l - ic2.ToInt64(null));

                case TypeCode.UInt64:
                    if (l < 0)
                    {
                        return(-1);
                    }
                    ulong ul2 = ic2.ToUInt64(null);
                    if (((ulong)l) < ul2)
                    {
                        return(-1);
                    }
                    if (((ulong)l) == ul2)
                    {
                        return(0);
                    }
                    return(1);

                case TypeCode.Single:
                case TypeCode.Double:
                    return(((double)l) - ic2.ToDouble(null));

                case TypeCode.Decimal:
                    return((double)(new Decimal(l) - ic2.ToDecimal(null)));

                default:
                    Object bd2 = Convert.ToNumber(v2, ic2);
                    return(JScriptCompare2(v1, bd2, ic1, Convert.GetIConvertible(bd2), t1, TypeCode.Double));
                }

            case TypeCode.UInt64:
                ulong ul = ic1.ToUInt64(null);
                switch (t2)
                {
                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    long l2 = ic2.ToInt64(null);
                    if (l2 < 0)
                    {
                        return(1);
                    }
                    if (ul == (ulong)l2)
                    {
                        return(0);
                    }
                    return(-1);

                case TypeCode.UInt64:
                    ulong ul2 = ic2.ToUInt64(null);
                    if (ul < ul2)
                    {
                        return(-1);
                    }
                    if (ul == ul2)
                    {
                        return(0);
                    }
                    return(1);

                case TypeCode.Single:
                case TypeCode.Double:
                    return(((double)ul) - ic2.ToDouble(null));

                case TypeCode.Decimal:
                    return((double)(new Decimal(ul) - ic2.ToDecimal(null)));

                default:
                    Object bd2 = Convert.ToNumber(v2, ic2);
                    return(JScriptCompare2(v1, bd2, ic1, Convert.GetIConvertible(bd2), t1, TypeCode.Double));
                }

            case TypeCode.Decimal:
                Decimal dec1 = ic1.ToDecimal(null);
                switch (t2)
                {
                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    return((double)(dec1 - new Decimal(ic2.ToInt64(null))));

                case TypeCode.UInt64:
                    return((double)(dec1 - new Decimal(ic2.ToUInt64(null))));

                case TypeCode.Single:
                case TypeCode.Double:
                    return((double)(dec1 - new Decimal(ic2.ToDouble(null))));

                case TypeCode.Decimal:
                    return((double)(dec1 - ic2.ToDecimal(null)));

                default:
                    return((double)(dec1 - new Decimal(Convert.ToNumber(v2, ic2))));
                }

            case TypeCode.String:
                switch (t2)
                {
                case TypeCode.String: return(String.CompareOrdinal(ic1.ToString(null), ic2.ToString(null)));

                case TypeCode.Char: return(String.CompareOrdinal(ic1.ToString(null), Convert.ToString(v2, ic2)));
                }
                goto default;

            default:
                double d1 = Convert.ToNumber(v1, ic1);
                double d2 = Convert.ToNumber(v2, ic2);
                if (d1 == d2)
                {
                    return(0);      //d1 and d2 could be infinities
                }
                return(d1 - d2);
            }
        }
        internal static bool JScriptStrictEquals(object v1, object v2, IConvertible ic1, IConvertible ic2, TypeCode t1, TypeCode t2, bool checkForDebuggerObjects)
        {
            long num7;

            switch (t1)
            {
            case TypeCode.Empty:
                return(t2 == TypeCode.Empty);

            case TypeCode.Object:
                if (v1 != v2)
                {
                    if ((v1 is Microsoft.JScript.Missing) || (v1 is System.Reflection.Missing))
                    {
                        v1 = null;
                    }
                    if (v1 == v2)
                    {
                        return(true);
                    }
                    if ((v2 is Microsoft.JScript.Missing) || (v2 is System.Reflection.Missing))
                    {
                        v2 = null;
                    }
                    if (checkForDebuggerObjects)
                    {
                        IDebuggerObject obj2 = v1 as IDebuggerObject;
                        if (obj2 != null)
                        {
                            IDebuggerObject o = v2 as IDebuggerObject;
                            if (o != null)
                            {
                                return(obj2.IsEqual(o));
                            }
                        }
                    }
                    return(v1 == v2);
                }
                return(true);

            case TypeCode.DBNull:
                return(t2 == TypeCode.DBNull);

            case TypeCode.Boolean:
                if (t2 != TypeCode.Boolean)
                {
                    return(false);
                }
                return(ic1.ToBoolean(null) == ic2.ToBoolean(null));

            case TypeCode.Char:
            {
                char ch = ic1.ToChar(null);
                switch (t2)
                {
                case TypeCode.Char:
                    return(ch == ic2.ToChar(null));

                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    return(ch == ic2.ToInt64(null));

                case TypeCode.UInt64:
                    return(ch == ic2.ToUInt64(null));

                case TypeCode.Single:
                case TypeCode.Double:
                    return(((double)ch) == ic2.ToDouble(null));

                case TypeCode.Decimal:
                    return(ch == ic2.ToDecimal(null));

                case TypeCode.String:
                {
                    string str = ic2.ToString(null);
                    return((str.Length == 1) && (ch == str[0]));
                }
                }
                break;
            }

            case TypeCode.SByte:
            {
                sbyte num = ic1.ToSByte(null);
                switch (t2)
                {
                case TypeCode.Char:
                    return(num == ic2.ToChar(null));

                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    return(num == ic2.ToInt64(null));

                case TypeCode.UInt64:
                    return((num >= 0) && (num == ic2.ToUInt64(null)));

                case TypeCode.Single:
                    return(num == ic2.ToSingle(null));

                case TypeCode.Double:
                    return(num == ic2.ToDouble(null));

                case TypeCode.Decimal:
                    return(num == ic2.ToDecimal(null));
                }
                return(false);
            }

            case TypeCode.Byte:
            {
                byte num2 = ic1.ToByte(null);
                switch (t2)
                {
                case TypeCode.Char:
                    return(num2 == ic2.ToChar(null));

                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    return(num2 == ic2.ToInt64(null));

                case TypeCode.UInt64:
                    return(num2 == ic2.ToUInt64(null));

                case TypeCode.Single:
                    return(num2 == ic2.ToSingle(null));

                case TypeCode.Double:
                    return(num2 == ic2.ToDouble(null));

                case TypeCode.Decimal:
                    return(num2 == ic2.ToDecimal(null));
                }
                return(false);
            }

            case TypeCode.Int16:
            {
                short num3 = ic1.ToInt16(null);
                switch (t2)
                {
                case TypeCode.Char:
                    return(num3 == ic2.ToChar(null));

                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    return(num3 == ic2.ToInt64(null));

                case TypeCode.UInt64:
                    return((num3 >= 0) && (num3 == ic2.ToUInt64(null)));

                case TypeCode.Single:
                    return(num3 == ic2.ToSingle(null));

                case TypeCode.Double:
                    return(num3 == ic2.ToDouble(null));

                case TypeCode.Decimal:
                    return(num3 == ic2.ToDecimal(null));
                }
                return(false);
            }

            case TypeCode.UInt16:
            {
                ushort num4 = ic1.ToUInt16(null);
                switch (t2)
                {
                case TypeCode.Char:
                    return(num4 == ic2.ToChar(null));

                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    return(num4 == ic2.ToInt64(null));

                case TypeCode.UInt64:
                    return(num4 == ic2.ToUInt64(null));

                case TypeCode.Single:
                    return(num4 == ic2.ToSingle(null));

                case TypeCode.Double:
                    return(num4 == ic2.ToDouble(null));

                case TypeCode.Decimal:
                    return(num4 == ic2.ToDecimal(null));
                }
                return(false);
            }

            case TypeCode.Int32:
            {
                int num5 = ic1.ToInt32(null);
                switch (t2)
                {
                case TypeCode.Char:
                    return(num5 == ic2.ToChar(null));

                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    return(num5 == ic2.ToInt64(null));

                case TypeCode.UInt64:
                    return((num5 >= 0) && (num5 == ic2.ToUInt64(null)));

                case TypeCode.Single:
                    return(num5 == ic2.ToSingle(null));

                case TypeCode.Double:
                    return(num5 == ic2.ToDouble(null));

                case TypeCode.Decimal:
                    return(num5 == ic2.ToDecimal(null));
                }
                return(false);
            }

            case TypeCode.UInt32:
            {
                uint num6 = ic1.ToUInt32(null);
                switch (t2)
                {
                case TypeCode.Char:
                    return(num6 == ic2.ToChar(null));

                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    return(num6 == ic2.ToInt64(null));

                case TypeCode.UInt64:
                    return(num6 == ic2.ToUInt64(null));

                case TypeCode.Single:
                    return(num6 == ic2.ToSingle(null));

                case TypeCode.Double:
                    return(num6 == ic2.ToDouble(null));

                case TypeCode.Decimal:
                    return(num6 == ic2.ToDecimal(null));
                }
                return(false);
            }

            case TypeCode.Int64:
                num7 = ic1.ToInt64(null);
                switch (t2)
                {
                case TypeCode.Char:
                    return(num7 == ic2.ToChar(null));

                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    return(num7 == ic2.ToInt64(null));

                case TypeCode.UInt64:
                    return((num7 >= 0L) && (num7 == ic2.ToUInt64(null)));

                case TypeCode.Single:
                    return(num7 == ic2.ToSingle(null));

                case TypeCode.Double:
                    return(num7 == ic2.ToDouble(null));

                case TypeCode.Decimal:
                    return(num7 == ic2.ToDecimal(null));
                }
                return(false);

            case TypeCode.UInt64:
            {
                ulong num8 = ic1.ToUInt64(null);
                switch (t2)
                {
                case TypeCode.Char:
                    return(num8 == ic2.ToChar(null));

                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    num7 = ic2.ToInt64(null);
                    return((num7 >= 0L) && (num8 == num7));

                case TypeCode.UInt64:
                    return(num8 == ic2.ToUInt64(null));

                case TypeCode.Single:
                    return(num8 == ic2.ToSingle(null));

                case TypeCode.Double:
                    return(num8 == ic2.ToDouble(null));

                case TypeCode.Decimal:
                    return(num8 == ic2.ToDecimal(null));
                }
                return(false);
            }

            case TypeCode.Single:
            {
                float num9 = ic1.ToSingle(null);
                switch (t2)
                {
                case TypeCode.Char:
                    return(num9 == ((float)ic2.ToChar(null)));

                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    return(num9 == ic2.ToInt64(null));

                case TypeCode.UInt64:
                    return(num9 == ic2.ToUInt64(null));

                case TypeCode.Single:
                    return(num9 == ic2.ToSingle(null));

                case TypeCode.Double:
                    return(num9 == ic2.ToSingle(null));

                case TypeCode.Decimal:
                    return(((decimal)num9) == ic2.ToDecimal(null));
                }
                return(false);
            }

            case TypeCode.Double:
            {
                double num10 = ic1.ToDouble(null);
                switch (t2)
                {
                case TypeCode.Char:
                    return(num10 == ((double)ic2.ToChar(null)));

                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    return(num10 == ic2.ToInt64(null));

                case TypeCode.UInt64:
                    return(num10 == ic2.ToUInt64(null));

                case TypeCode.Single:
                    return(((float)num10) == ic2.ToSingle(null));

                case TypeCode.Double:
                    return(num10 == ic2.ToDouble(null));

                case TypeCode.Decimal:
                    return(((decimal)num10) == ic2.ToDecimal(null));
                }
                return(false);
            }

            case TypeCode.Decimal:
            {
                decimal num11 = ic1.ToDecimal(null);
                switch (t2)
                {
                case TypeCode.Char:
                    return(num11 == ic2.ToChar(null));

                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    return(num11 == ic2.ToInt64(null));

                case TypeCode.UInt64:
                    return(num11 == ic2.ToUInt64(null));

                case TypeCode.Single:
                    return(num11 == ((decimal)ic2.ToSingle(null)));

                case TypeCode.Double:
                    return(num11 == ((decimal)ic2.ToDouble(null)));

                case TypeCode.Decimal:
                    return(num11 == ic2.ToDecimal(null));
                }
                return(false);
            }

            case TypeCode.DateTime:
                if (t2 != TypeCode.DateTime)
                {
                    return(false);
                }
                return(ic1.ToDateTime(null) == ic2.ToDateTime(null));

            case TypeCode.String:
            {
                if (t2 != TypeCode.Char)
                {
                    if (t2 != TypeCode.String)
                    {
                        return(false);
                    }
                    if (v1 != v2)
                    {
                        return(ic1.ToString(null).Equals(ic2.ToString(null)));
                    }
                    return(true);
                }
                string str2 = ic1.ToString(null);
                if (str2.Length != 1)
                {
                    return(false);
                }
                return(str2[0] == ic2.ToChar(null));
            }

            default:
                return(false);
            }
            return(false);
        }
        private static double JScriptCompare2(object v1, object v2, IConvertible ic1, IConvertible ic2, TypeCode t1, TypeCode t2)
        {
            double num7;
            if (t1 == TypeCode.Object)
            {
                v1 = Microsoft.JScript.Convert.ToPrimitive(v1, PreferredType.Number, ref ic1);
                t1 = Microsoft.JScript.Convert.GetTypeCode(v1, ic1);
            }
            if (t2 == TypeCode.Object)
            {
                v2 = Microsoft.JScript.Convert.ToPrimitive(v2, PreferredType.Number, ref ic2);
                t2 = Microsoft.JScript.Convert.GetTypeCode(v2, ic2);
            }
            switch (t1)
            {
                case TypeCode.Char:
                    if (t2 != TypeCode.String)
                    {
                        break;
                    }
                    return (double) string.CompareOrdinal(Microsoft.JScript.Convert.ToString(v1, ic1), ic2.ToString(null));

                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    break;

                case TypeCode.UInt64:
                {
                    ulong num3 = ic1.ToUInt64(null);
                    switch (t2)
                    {
                        case TypeCode.SByte:
                        case TypeCode.Byte:
                        case TypeCode.Int16:
                        case TypeCode.UInt16:
                        case TypeCode.Int32:
                        case TypeCode.UInt32:
                        case TypeCode.Int64:
                        {
                            long num4 = ic2.ToInt64(null);
                            if (num4 < 0L)
                            {
                                return 1.0;
                            }
                            if (num3 == num4)
                            {
                                return 0.0;
                            }
                            return -1.0;
                        }
                        case TypeCode.UInt64:
                        {
                            ulong num5 = ic2.ToUInt64(null);
                            if (num3 < num5)
                            {
                                return -1.0;
                            }
                            if (num3 == num5)
                            {
                                return 0.0;
                            }
                            return 1.0;
                        }
                        case TypeCode.Single:
                        case TypeCode.Double:
                            return (num3 - ic2.ToDouble(null));

                        case TypeCode.Decimal:
                            return (double) (new decimal(num3) - ic2.ToDecimal(null));
                    }
                    object obj3 = Microsoft.JScript.Convert.ToNumber(v2, ic2);
                    return JScriptCompare2(v1, obj3, ic1, Microsoft.JScript.Convert.GetIConvertible(obj3), t1, TypeCode.Double);
                }
                case TypeCode.Decimal:
                {
                    decimal num6 = ic1.ToDecimal(null);
                    switch (t2)
                    {
                        case TypeCode.SByte:
                        case TypeCode.Byte:
                        case TypeCode.Int16:
                        case TypeCode.UInt16:
                        case TypeCode.Int32:
                        case TypeCode.UInt32:
                        case TypeCode.Int64:
                            return (double) (num6 - new decimal(ic2.ToInt64(null)));

                        case TypeCode.UInt64:
                            return (double) (num6 - new decimal(ic2.ToUInt64(null)));

                        case TypeCode.Single:
                        case TypeCode.Double:
                            return (double) (num6 - new decimal(ic2.ToDouble(null)));

                        case TypeCode.Decimal:
                            return (double) (num6 - ic2.ToDecimal(null));
                    }
                    return (double) (num6 - new decimal(Microsoft.JScript.Convert.ToNumber(v2, ic2)));
                }
                case TypeCode.String:
                {
                    TypeCode code5 = t2;
                    if (code5 == TypeCode.Char)
                    {
                        return (double) string.CompareOrdinal(ic1.ToString(null), Microsoft.JScript.Convert.ToString(v2, ic2));
                    }
                    if (code5 != TypeCode.String)
                    {
                        goto Label_0355;
                    }
                    return (double) string.CompareOrdinal(ic1.ToString(null), ic2.ToString(null));
                }
                default:
                    goto Label_0355;
            }
            long num = ic1.ToInt64(null);
            switch (t2)
            {
                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                    return (double) (num - ic2.ToInt64(null));

                case TypeCode.UInt64:
                    if (num >= 0L)
                    {
                        ulong num2 = ic2.ToUInt64(null);
                        if (num < num2)
                        {
                            return -1.0;
                        }
                        if (num == num2)
                        {
                            return 0.0;
                        }
                        return 1.0;
                    }
                    return -1.0;

                case TypeCode.Single:
                case TypeCode.Double:
                    return (num - ic2.ToDouble(null));

                case TypeCode.Decimal:
                    return (double) (new decimal(num) - ic2.ToDecimal(null));

                default:
                {
                    object obj2 = Microsoft.JScript.Convert.ToNumber(v2, ic2);
                    return JScriptCompare2(v1, obj2, ic1, Microsoft.JScript.Convert.GetIConvertible(obj2), t1, TypeCode.Double);
                }
            }
        Label_0355:
            num7 = Microsoft.JScript.Convert.ToNumber(v1, ic1);
            double num8 = Microsoft.JScript.Convert.ToNumber(v2, ic2);
            if (num7 == num8)
            {
                return 0.0;
            }
            return (num7 - num8);
        }
Example #60
0
        /// <summary>
        /// Returns true if the constant is an integral value that falls in the range of the target type.
        /// The target type does have to be an integral type. If it is not, this method always returns false.
        /// </summary>
        public static bool IsIntegerInRangeOf(ICompileTimeConstant constExpression, ITypeReference targetType)
        {
            switch (targetType.TypeCode)
            {
            case PrimitiveTypeCode.UInt8: {
                IConvertible /*?*/ ic = constExpression.Value as IConvertible;
                if (ic == null)
                {
                    return(false);
                }
                switch (ic.GetTypeCode())
                {
                case System.TypeCode.Byte:
                    return(true);

                case System.TypeCode.SByte:
                    return(byte.MinValue <= ic.ToSByte(null));

                case System.TypeCode.Int16:
                    short s = ic.ToInt16(null);
                    return(byte.MinValue <= s && s <= byte.MaxValue);

                case System.TypeCode.Int32:
                    int i = ic.ToInt32(null);
                    return(byte.MinValue <= i && i <= byte.MaxValue);

                case System.TypeCode.Int64:
                    long lng = ic.ToInt64(null);
                    return(byte.MinValue <= lng && lng <= byte.MaxValue);

                case System.TypeCode.UInt16:
                    return(ic.ToUInt16(null) <= byte.MaxValue);

                case System.TypeCode.UInt32:
                    return(ic.ToUInt32(null) <= byte.MaxValue);

                case System.TypeCode.UInt64:
                    return(ic.ToUInt64(null) <= byte.MaxValue);

                case System.TypeCode.Decimal:
                    decimal d = ic.ToDecimal(null);
                    return(byte.MinValue <= d && d <= byte.MaxValue);
                }
                return(false);
            }

            case PrimitiveTypeCode.UInt16: {
                IConvertible /*?*/ ic = constExpression.Value as IConvertible;
                if (ic == null)
                {
                    return(false);
                }
                switch (ic.GetTypeCode())
                {
                case System.TypeCode.Byte:
                case System.TypeCode.UInt16:
                    return(true);

                case System.TypeCode.SByte:
                    return(ushort.MinValue <= ic.ToSByte(null));

                case System.TypeCode.Int16:
                    return(ushort.MinValue <= ic.ToInt16(null));

                case System.TypeCode.Int32:
                    int i = ic.ToInt32(null);
                    return(ushort.MinValue <= i && i <= ushort.MaxValue);

                case System.TypeCode.Int64:
                    long lng = ic.ToInt64(null);
                    return(ushort.MinValue <= lng && lng <= ushort.MaxValue);

                case System.TypeCode.UInt32:
                    return(ic.ToUInt32(null) <= ushort.MaxValue);

                case System.TypeCode.UInt64:
                    return(ic.ToUInt64(null) <= ushort.MaxValue);

                case System.TypeCode.Decimal:
                    decimal d = ic.ToDecimal(null);
                    return(ushort.MinValue <= d && d <= ushort.MaxValue);
                }
                return(false);
            }

            case PrimitiveTypeCode.UInt32: {
                IConvertible /*?*/ ic = constExpression.Value as IConvertible;
                if (ic == null)
                {
                    return(false);
                }
                switch (ic.GetTypeCode())
                {
                case System.TypeCode.Byte:
                case System.TypeCode.UInt16:
                case System.TypeCode.UInt32:
                    return(true);

                case System.TypeCode.SByte:
                    return(uint.MinValue <= ic.ToSByte(null));

                case System.TypeCode.Int16:
                    return(uint.MinValue <= ic.ToInt16(null));

                case System.TypeCode.Int32:
                    return(uint.MinValue <= ic.ToInt32(null));

                case System.TypeCode.Int64:
                    long lng = ic.ToInt64(null);
                    return(uint.MinValue <= lng && lng <= uint.MaxValue);

                case System.TypeCode.UInt64:
                    return(ic.ToUInt64(null) <= uint.MaxValue);

                case System.TypeCode.Decimal:
                    decimal d = ic.ToDecimal(null);
                    return(uint.MinValue <= d && d <= uint.MaxValue);
                }
                return(false);
            }

            case PrimitiveTypeCode.UInt64: {
                IConvertible /*?*/ ic = constExpression.Value as IConvertible;
                if (ic == null)
                {
                    return(false);
                }
                switch (ic.GetTypeCode())
                {
                case System.TypeCode.Byte:
                case System.TypeCode.UInt16:
                case System.TypeCode.UInt32:
                case System.TypeCode.UInt64:
                    return(true);

                case System.TypeCode.SByte:
                    return(0 <= ic.ToSByte(null));

                case System.TypeCode.Int16:
                    return(0 <= ic.ToInt16(null));

                case System.TypeCode.Int32:
                    return(0 <= ic.ToInt32(null));

                case System.TypeCode.Int64:
                    return(0 <= ic.ToInt64(null));

                case System.TypeCode.Decimal:
                    decimal d = ic.ToDecimal(null);
                    return(0 <= d && d <= ulong.MaxValue);
                }
                return(false);
            }

            case PrimitiveTypeCode.Int8: {
                IConvertible /*?*/ ic = constExpression.Value as IConvertible;
                if (ic == null)
                {
                    return(false);
                }
                switch (ic.GetTypeCode())
                {
                case System.TypeCode.SByte:
                    return(true);

                case System.TypeCode.Int16:
                    short s = ic.ToInt16(null);
                    return(sbyte.MinValue <= s && s <= sbyte.MaxValue);

                case System.TypeCode.Int32:
                    int i = ic.ToInt32(null);
                    return(sbyte.MinValue <= i && i <= sbyte.MaxValue);

                case System.TypeCode.Int64:
                    long lng = ic.ToInt64(null);
                    return(sbyte.MinValue <= lng && lng <= sbyte.MaxValue);

                case System.TypeCode.Byte:
                    return(ic.ToByte(null) <= sbyte.MaxValue);

                case System.TypeCode.UInt16:
                    return(ic.ToUInt16(null) <= sbyte.MaxValue);

                case System.TypeCode.UInt32:
                    return(ic.ToUInt32(null) <= sbyte.MaxValue);

                case System.TypeCode.UInt64:
                    return(ic.ToUInt64(null) <= (ulong)sbyte.MaxValue);

                case System.TypeCode.Decimal:
                    decimal d = ic.ToDecimal(null);
                    return(sbyte.MinValue <= d && d <= sbyte.MaxValue);
                }
                return(false);
            }

            case PrimitiveTypeCode.Int16: {
                IConvertible /*?*/ ic = constExpression.Value as IConvertible;
                if (ic == null)
                {
                    return(false);
                }
                switch (ic.GetTypeCode())
                {
                case System.TypeCode.SByte:
                case System.TypeCode.Byte:
                case System.TypeCode.Int16:
                    return(true);

                case System.TypeCode.Int32:
                    int i = ic.ToInt32(null);
                    return(short.MinValue <= i && i <= short.MaxValue);

                case System.TypeCode.Int64:
                    long lng = ic.ToInt64(null);
                    return(short.MinValue <= lng && lng <= short.MaxValue);

                case System.TypeCode.UInt16:
                    return(ic.ToUInt16(null) <= short.MaxValue);

                case System.TypeCode.UInt32:
                    return(ic.ToUInt32(null) <= short.MaxValue);

                case System.TypeCode.UInt64:
                    return(ic.ToUInt64(null) <= (ulong)short.MaxValue);

                case System.TypeCode.Decimal:
                    decimal d = ic.ToDecimal(null);
                    return(short.MinValue <= d && d <= short.MaxValue);
                }
                return(false);
            }

            case PrimitiveTypeCode.Int32: {
                IConvertible /*?*/ ic = constExpression.Value as IConvertible;
                if (ic == null)
                {
                    return(false);
                }
                switch (ic.GetTypeCode())
                {
                case System.TypeCode.SByte:
                case System.TypeCode.Byte:
                case System.TypeCode.Int16:
                case System.TypeCode.UInt16:
                case System.TypeCode.Int32:
                    return(true);

                case System.TypeCode.Int64:
                    long lng = ic.ToInt64(null);
                    return(int.MinValue <= lng && lng <= int.MaxValue);

                case System.TypeCode.UInt32:
                    return(ic.ToUInt32(null) <= int.MaxValue);

                case System.TypeCode.UInt64:
                    return(ic.ToUInt64(null) <= int.MaxValue);

                case System.TypeCode.Decimal:
                    decimal d = ic.ToDecimal(null);
                    return(int.MinValue <= d && d <= int.MaxValue);
                }
                return(false);
            }

            case PrimitiveTypeCode.Int64: {
                IConvertible /*?*/ ic = constExpression.Value as IConvertible;
                if (ic == null)
                {
                    return(false);
                }
                switch (ic.GetTypeCode())
                {
                case System.TypeCode.SByte:
                case System.TypeCode.Byte:
                case System.TypeCode.Int16:
                case System.TypeCode.UInt16:
                case System.TypeCode.Int32:
                case System.TypeCode.UInt32:
                case System.TypeCode.Int64:
                    return(true);

                case System.TypeCode.UInt64:
                    return(ic.ToUInt64(null) <= int.MaxValue);

                case System.TypeCode.Decimal:
                    decimal d = ic.ToDecimal(null);
                    return(long.MinValue <= d && d <= long.MaxValue);
                }
                return(false);
            }
            }
            return(false);
        }