Exemple #1
0
    public bool NegTest2()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("NegTest2: Convert a int32 which is bigger than char to char ");

        try
        {
            Int32        i1    = 65536;
            IConvertible Icon1 = (IConvertible)i1;
            char         c1    = Icon1.ToChar(null);
            TestLibrary.TestFramework.LogError("103", "OverflowException was not thrown as expected  ");
            retVal = false;
        }
        catch (OverflowException)
        {
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("104", "Unexpected exception: " + e);
            retVal = false;
        }

        return(retVal);
    }
    public bool NegTest3()
    {
        bool            retVal    = true;
        CultureInfo     myculture = new CultureInfo("en-us");
        IFormatProvider provider  = myculture.NumberFormat;

        TestLibrary.TestFramework.BeginScenario("NegTest3:The Int64 value which is out the range of Char IConvertible To Char 3");
        try
        {
            long int64A = TestLibrary.Generator.GetInt64(-55);
            if (int64A <= 65535)
            {
                int64A = int64A + 65536;
            }
            IConvertible iConvert = (IConvertible)(int64A);
            char         charA    = iConvert.ToChar(provider);
            retVal = false;
            TestLibrary.TestFramework.LogError("N005", "Int64 value out of the range of char but not throw exception");
        }
        catch (OverflowException) { }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("N006", "Unexpected exception: " + e);
            retVal = false;
        }
        return(retVal);
    }
Exemple #3
0
        private object PromoteNumericArgument(Type type, object argument)
        {
            IConvertible convertible = (IConvertible)argument;

            switch (Type.GetTypeCode(type))
            {
            case TypeCode.Byte: return(convertible.ToByte(null));

            case TypeCode.SByte: return(convertible.ToSByte(null));

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

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

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

            case TypeCode.UInt16: return(convertible.ToUInt16(null));

            case TypeCode.UInt32: return(convertible.ToUInt32(null));

            case TypeCode.UInt64: return(convertible.ToUInt64(null));

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

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

            case TypeCode.Boolean: return(convertible.ToBoolean(null));

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

            case TypeCode.Char: return(convertible.ToChar(null));
            }
            throw new ArgumentException();
        }
    public bool PosTest2()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("PosTest2: Convert zero to char ");

        try
        {
            Int16        i1    = 0;
            IConvertible Icon1 = (IConvertible)i1;
            char         c1    = Icon1.ToChar(null);
            if (c1 != i1)
            {
                TestLibrary.TestFramework.LogError("003", "The result is not the value as expected ");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("004", "Unexpected exception: " + e);
            retVal = false;
        }

        return(retVal);
    }
    public bool PosTest3()
    {
        bool            retVal    = true;
        CultureInfo     myculture = new CultureInfo("el-GR");
        IFormatProvider provider  = myculture.NumberFormat;

        TestLibrary.TestFramework.BeginScenario("PosTest3:The Int64 value which is in the range of Char IConvertible To Char 3");
        try
        {
            long         int64A   = (long)this.GetInt32(1, 65535);
            IConvertible iConvert = (IConvertible)(int64A);
            char         charA    = iConvert.ToChar(provider);
            if (charA != (char)(int64A))
            {
                TestLibrary.TestFramework.LogError("005", "the ActualResult is not the ExpectResult");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("006", "Unexpected exception: " + e);
            retVal = false;
        }
        return(retVal);
    }
Exemple #6
0
        public void TestToChar()
        {
            JulianDate   julianDate  = new JulianDate(100);
            IConvertible convertible = julianDate;

            convertible.ToChar(null);
        }
Exemple #7
0
    public bool PosTest2()
    {
        bool retVal = true;

        const string c_TEST_ID   = "P002";
        const string c_TEST_DESC = "PosTest2: Char.MaxValue";
        string       errorDesc;

        char expectedChar = char.MaxValue;
        char actualChar;

        TestLibrary.TestFramework.BeginScenario(c_TEST_DESC);
        try
        {
            IConvertible converter = expectedChar;
            actualChar = converter.ToChar(null);

            if (actualChar != expectedChar)
            {
                errorDesc = string.Format("Character is not \\u{0:x} as expected: Actual(\\u{1:x}",
                                          (int)expectedChar, (int)actualChar);
                TestLibrary.TestFramework.LogError("003" + " TestId-" + c_TEST_ID, errorDesc);
                retVal = false;
            }
        }
        catch (Exception e)
        {
            errorDesc = "Unexpected exception: " + e;
            TestLibrary.TestFramework.LogError("004" + " TestId-" + c_TEST_ID, errorDesc);
            retVal = false;
        }

        return(retVal);
    }
Exemple #8
0
    public bool NegTest1()
    {
        bool retVal = true;

        TestLibrary.TestFramework.BeginScenario("NegTest1: Convert a negative int32 to char");

        try
        {
            Int32        i1    = (Int32)TestLibrary.Generator.GetChar(-55);
            IConvertible Icon1 = (IConvertible)(-i1);
            char         c1    = Icon1.ToChar(null);
            TestLibrary.TestFramework.LogError("101", "OverflowException was not thrown as expected  ");
            retVal = false;
        }
        catch (OverflowException)
        {
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("102", "Unexpected exception: " + e);
            retVal = false;
        }

        return(retVal);
    }
Exemple #9
0
        ///////////////////////////////////////////////////////////////////////////////////////////////

        public static bool ToCharacter(Number number, ref char value)
        {
            bool result = false;

            try
            {
                if (number != null)
                {
                    if (number.IsCharacter())
                    {
                        value = (char)number.Value;

                        result = true;
                    }
                    else
                    {
                        IConvertible convertible = number.Value as IConvertible;

                        if (convertible != null)
                        {
                            value = convertible.ToChar(null);

                            result = true;
                        }
                    }
                }
            }
            catch
            {
                // do nothing.
            }

            return(result);
        }
Exemple #10
0
    public bool PosTest1()
    {
        bool retVal = true;

        // Add your scenario description here
        TestLibrary.TestFramework.BeginScenario("PosTest1: Convert a random (Int32)char to char ");

        try
        {
            Int32        i1    = (Int32)TestLibrary.Generator.GetChar(-55);
            IConvertible Icon1 = (IConvertible)i1;
            char         c1    = Icon1.ToChar(null);
            if (c1 != i1)
            {
                TestLibrary.TestFramework.LogError("001", "The result is not the value as expected ");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("002", "Unexpected exception: " + e);
            retVal = false;
        }

        return(retVal);
    }
Exemple #11
0
    public bool PosTest3()
    {
        bool retVal = true;

        // Add your scenario description here
        TestLibrary.TestFramework.BeginScenario("PosTest3: Check the border value of char ");

        try
        {
            Int32        i1    = 65535;
            IConvertible Icon1 = (IConvertible)i1;
            char         c1    = Icon1.ToChar(null);
            if (c1 != i1)
            {
                TestLibrary.TestFramework.LogError("005", "The result is not the value as expected ");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("006", "Unexpected exception: " + e);
            retVal = false;
        }

        return(retVal);
    }
Exemple #12
0
    public bool PosTest1()
    {
        bool            retVal    = true;
        CultureInfo     myculture = new CultureInfo("en-us");
        IFormatProvider provider  = myculture.NumberFormat;

        TestLibrary.TestFramework.BeginScenario("PosTest1:The sbyte MaxValue IConvertible To Char");
        try
        {
            sbyte        sbyteVal = sbyte.MaxValue;
            IConvertible iConvert = (IConvertible)(sbyteVal);
            char         charVal  = iConvert.ToChar(provider);
            if (charVal != 127)
            {
                TestLibrary.TestFramework.LogError("001", "the ActualResult is not the ExpectResult");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("002", "Unexpected exception: " + e);
            retVal = false;
        }
        return(retVal);
    }
Exemple #13
0
    public bool PosTest3()
    {
        bool            retVal    = true;
        CultureInfo     myculture = new CultureInfo("en-us");
        IFormatProvider provider  = myculture.NumberFormat;

        TestLibrary.TestFramework.BeginScenario("PosTest3:The random sbyte IConvertible To Char 2");
        try
        {
            sbyte        sbyteVal = (sbyte)(this.GetInt32(0, 128));
            IConvertible iConvert = (IConvertible)(sbyteVal);
            char         charVal  = iConvert.ToChar(provider);
            if (charVal != (char)(sbyteVal))
            {
                TestLibrary.TestFramework.LogError("005", "the ActualResult is not the ExpectResult");
                retVal = false;
            }
        }
        catch (Exception e)
        {
            TestLibrary.TestFramework.LogError("006", "Unexpected exception: " + e);
            retVal = false;
        }
        return(retVal);
    }
        private static bool TryConvert(object input, IFormatProvider provider, out char value)
        {
            if (IsNullOrEmptyString(input))
            {
                value = '\0';
                return(false);
            }

            if (!(input is string))
            {
                IConvertible ic = input as IConvertible;
                if (ic != null)
                {
                    try
                    {
                        value = ic.ToChar(provider);
                        return(true);
                    }
                    catch
                    {
                    }
                }
            }

            string s = Convert.ToString(input, provider);

            return(char.TryParse(s, out value));
        }
Exemple #15
0
        /// <summary>
        /// Converts object value to invariant format (understood by JavaScript)
        /// </summary>
        /// <param name="value">Object value</param>
        /// <param name="objTypeCode">Object TypeCode</param>
        /// <param name="safeConversion">Check and remove unusual unicode characters from the result string.</param>
        /// <returns>Object value converted to string</returns>
        internal static string XmlConvertToString(IConvertible value, TypeCode objTypeCode, bool safeConversion = false)
        {
            if (value == null)
            {
                return("null");
            }

            switch (objTypeCode)
            {
            case TypeCode.Boolean:
                return(XmlConvert.ToString(value.ToBoolean(CultureInfo.InvariantCulture)));      // boolean as lowercase

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

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

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

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

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

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

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

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

            case TypeCode.Single:
                return(XmlConvertToString(value.ToSingle(CultureInfo.InvariantCulture)));

            case TypeCode.Double:
                return(XmlConvertToString(value.ToDouble(CultureInfo.InvariantCulture)));

            case TypeCode.Decimal:
                return(XmlConvertToString(value.ToDecimal(CultureInfo.InvariantCulture)));

            case TypeCode.DateTime:
                return(XmlConvert.ToString(value.ToDateTime(CultureInfo.InvariantCulture), XmlDateTimeSerializationMode.Utc));

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

            case TypeCode.String:
                return(safeConversion ? RemoveInvalidXmlChars(value.ToString(CultureInfo.InvariantCulture)) : value.ToString(CultureInfo.InvariantCulture));

            default:
                return(XmlConvertToStringInvariant(value, safeConversion));
            }
        }
Exemple #16
0
 private static void CallEII()
 {
     // <Snippet7>
       int codePoint = 1067;
       IConvertible iConv = codePoint;
       char ch = iConv.ToChar(null);
       Console.WriteLine("Converted {0} to {1}.", codePoint, ch);
       // </Snippet7>
 }
        public void op_ToChar_IFormatProvider()
        {
            const char expected = (char)123;

            IConvertible value  = (AlphaDecimal)123;
            var          actual = value.ToChar(CultureInfo.InvariantCulture);

            Assert.Equal(expected, actual);
        }
Exemple #18
0
        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="conv"></param>
        /// <param name="formatProvider"></param>
        /// <returns></returns>
        private static T To <T>(IConvertible conv, IFormatProvider formatProvider = null)
        {
            formatProvider = formatProvider ?? CultureInfo.CurrentCulture;
            switch (typeof(T).Name)
            {
            case TypeNames.BOOLEAN:
                return(conv.ToBoolean(formatProvider).To <T>());

            case TypeNames.BYTE:
                return(conv.ToByte(formatProvider).To <T>());

            case TypeNames.CHAR:
                return(conv.ToChar(formatProvider).To <T>());

            case TypeNames.DATE_TIME:
                return(conv.ToDateTime(formatProvider).To <T>());

            case TypeNames.DECIMAL:
                return(conv.ToDecimal(formatProvider).To <T>());

            case TypeNames.DOUBLE:
                return(conv.ToDouble(formatProvider).To <T>());

            case TypeNames.INT16:
                return(conv.ToInt16(formatProvider).To <T>());

            case TypeNames.INT32:
                return(conv.ToInt32(formatProvider).To <T>());

            case TypeNames.INT64:
                return(conv.ToInt64(formatProvider).To <T>());

            case TypeNames.S_BYTE:
                return(conv.ToSByte(formatProvider).To <T>());

            case TypeNames.SINGLE:
                return(conv.ToSingle(formatProvider).To <T>());

            case TypeNames.STRING:
                return(conv.ToString(formatProvider).To <T>());

            case TypeNames.U_INT16:
                return(conv.ToUInt16(formatProvider).To <T>());

            case TypeNames.U_INT32:
                return(conv.ToUInt32(formatProvider).To <T>());

            case TypeNames.U_INT64:
                return(conv.ToUInt64(formatProvider).To <T>());

            default:
                return(default(T));
            }
        }
Exemple #19
0
        static void AppendValue(StringBuilder builder, IConvertible convertable)
        {
            switch (convertable.GetTypeCode())
            {
            case TypeCode.SByte:
                builder.AppendFormat(CultureInfo.InvariantCulture, "0x{0:X2}: {0}\r\n", convertable.ToSByte(null));
                break;

            case TypeCode.Int16:
                builder.AppendFormat(CultureInfo.InvariantCulture, "0x{0:X4}: {0}\r\n", convertable.ToInt16(null));
                break;

            case TypeCode.Int32:
                builder.AppendFormat(CultureInfo.InvariantCulture, "0x{0:X8}: {0}\r\n", convertable.ToInt32(null));
                break;

            case TypeCode.Int64:
                builder.AppendFormat(CultureInfo.InvariantCulture, "0x{0:X16}: {0}\r\n", convertable.ToInt64(null));
                break;

            case TypeCode.Byte:
                builder.AppendFormat(CultureInfo.InvariantCulture, "0x{0:X2}: {0}\r\n", convertable.ToByte(null));
                break;

            case TypeCode.UInt16:
                builder.AppendFormat(CultureInfo.InvariantCulture, "0x{0:X4}: {0}\r\n", convertable.ToUInt16(null));
                break;

            case TypeCode.UInt32:
                builder.AppendFormat(CultureInfo.InvariantCulture, "0x{0:X8}: {0}\r\n", convertable.ToUInt32(null));
                break;

            case TypeCode.UInt64:
                builder.AppendFormat(CultureInfo.InvariantCulture, "0x{0:X16}: {0}\r\n", convertable.ToUInt64(null));
                break;

            case TypeCode.Char:
                AppendCharValue(builder, convertable.ToChar(null));
                break;

            case TypeCode.String:
                AppendStringValue(builder, convertable.ToString(null));
                break;

            case TypeCode.DateTime:
                AppendDateTime(builder, convertable.ToDateTime(null));
                break;

            default:
                builder.AppendFormat(CultureInfo.InvariantCulture, "{0}\r\n", convertable);
                break;
            }
        }
Exemple #20
0
        public void ToCharTest()
        {
            IConvertible    target   = (Half)64UL;
            IFormatProvider provider = CultureInfo.InvariantCulture;

            try
            {
                char actual = target.ToChar(provider);
                Assert.True(false);
            }
            catch (InvalidCastException) { }
        }
Exemple #21
0
 static void Test_ToChar(IConvertible c)
 {
     Console.WriteLine("ToChar");
     try
     {
         Console.WriteLine(c.ToChar(null));
     }
     catch (Exception exc)
     {
         Console.WriteLine(exc.GetType());
     }
 }
        private static IEnumerable <byte> GetBytesFromConvertible(Type type, IConvertible value, uint defaultStringLength)
        {
            IEnumerable <byte> varValueBytes = null;

            if (value == null)
            {
                return(null);
            }

            switch (value.GetTypeCode())
            {
            case TypeCode.Boolean: varValueBytes = BitConverter.GetBytes(value.ToBoolean(null)); break;

            case TypeCode.Byte: varValueBytes = new byte[] { value.ToByte(null) }; break;

            case TypeCode.Char: varValueBytes = BitConverter.GetBytes(value.ToChar(null)); break;

            case TypeCode.Int16: varValueBytes = BitConverter.GetBytes(value.ToInt16(null)); break;

            case TypeCode.Int32: varValueBytes = BitConverter.GetBytes(value.ToInt32(null)); break;

            case TypeCode.Int64: varValueBytes = BitConverter.GetBytes(value.ToInt64(null)); break;

            case TypeCode.UInt16: varValueBytes = BitConverter.GetBytes(value.ToUInt16(null)); break;

            case TypeCode.UInt32: varValueBytes = BitConverter.GetBytes(value.ToUInt32(null)); break;

            case TypeCode.UInt64: varValueBytes = BitConverter.GetBytes(value.ToUInt64(null)); break;

            case TypeCode.Single: varValueBytes = BitConverter.GetBytes(value.ToSingle(null)); break;

            case TypeCode.Double: varValueBytes = BitConverter.GetBytes(value.ToDouble(null)); break;

            case TypeCode.DateTime: varValueBytes = BitConverter.GetBytes(value.ToInt32(null)); break;

            case TypeCode.String: varValueBytes = value.ToString().ToAdsBytes(); break;

            case TypeCode.Object:
                if (Type.Equals(typeof(Date), type))
                {
                    varValueBytes = BitConverter.GetBytes(value.ToInt32(null));
                }
                if (Type.Equals(typeof(Time), type))
                {
                    varValueBytes = BitConverter.GetBytes(value.ToInt32(null));
                }
                break;
            }

            return(varValueBytes);
        }
Exemple #23
0
        /// <summary>
        /// Returns the specified value as an array of bytes in the target endian-order.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <returns>An array of bytes with length 1.</returns>
        /// <typeparam name="T">Native value type to get bytes for.</typeparam>
        /// <exception cref="ArgumentException"><paramref name="value"/> type is not primitive.</exception>
        /// <exception cref="InvalidOperationException">Cannot get bytes for <paramref name="value"/> type.</exception>
        public byte[] GetBytes <T>(T value) where T : struct
        {
            if (!typeof(T).IsPrimitive)
            {
                throw new ArgumentException("Value type is not primitive", "value");
            }

            IConvertible nativeValue = (IConvertible)value;

            switch (nativeValue.GetTypeCode())
            {
            case TypeCode.Char:
                return(GetBytes(nativeValue.ToChar(null)));

            case TypeCode.Boolean:
                return(GetBytes(nativeValue.ToBoolean(null)));

            case TypeCode.SByte:
                return(GetBytes(nativeValue.ToSByte(null)));

            case TypeCode.Byte:
                return(GetBytes(nativeValue.ToByte(null)));

            case TypeCode.Int16:
                return(GetBytes(nativeValue.ToInt16(null)));

            case TypeCode.UInt16:
                return(GetBytes(nativeValue.ToUInt16(null)));

            case TypeCode.Int32:
                return(GetBytes(nativeValue.ToInt32(null)));

            case TypeCode.UInt32:
                return(GetBytes(nativeValue.ToUInt32(null)));

            case TypeCode.Int64:
                return(GetBytes(nativeValue.ToInt64(null)));

            case TypeCode.UInt64:
                return(GetBytes(nativeValue.ToUInt64(null)));

            case TypeCode.Single:
                return(GetBytes(nativeValue.ToSingle(null)));

            case TypeCode.Double:
                return(GetBytes(nativeValue.ToDouble(null)));

            default:
                throw new InvalidOperationException("Cannot get bytes for value type " + nativeValue.GetTypeCode());
            }
        }
Exemple #24
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;
            }
        }
Exemple #25
0
 private static void ConvertIntToChar()
 {
     try
     {
         int          value     = 25;
         IConvertible converter = value;
         Char         character = converter.ToChar(null);
         Logging.Log($"Conversion from Int to Char succeeded.");
     }
     catch (System.InvalidCastException exception)
     {
         Logging.Log("Conversion failed.");
         Logging.Log(exception);
     }
 }
Exemple #26
0
        // InvalidCastException
        // --------------------
        public void Test_5()
        {
            bool flag = false;

            try
            {
                IConvertible conv = flag;
                Char         ch   = conv.ToChar(null); //错误:类型转换错误
            }
            catch (InvalidCastException ex)
            {
                Console.WriteLine("Exception caught: {0}", ex);
            }
            finally
            {
                Console.WriteLine("Done!");
            }
        }
Exemple #27
0
        public static object DefaultConvertToType(IConvertible value, Type destinationType, IFormatProvider provider)
        {
            if (value.GetType() == destinationType)
            {
                return(value);
            }

            switch (Type.GetTypeCode(destinationType))
            {
            case TypeCode.Boolean: return(value.ToBoolean(provider));

            case TypeCode.Byte: return(value.ToByte(provider));

            case TypeCode.Char: return(value.ToChar(provider));

            case TypeCode.DateTime: return(value.ToDateTime(provider));

            case TypeCode.Decimal: return(value.ToDecimal(provider));

            case TypeCode.Double: return(value.ToDouble(provider));

            case TypeCode.Int16: return(value.ToInt16(provider));

            case TypeCode.Int32: return(value.ToInt32(provider));

            case TypeCode.Int64: return(value.ToInt64(provider));

            case TypeCode.SByte: return(value.ToSByte(provider));

            case TypeCode.Single: return(value.ToSingle(provider));

            case TypeCode.String: return(value.ToString(provider));

            case TypeCode.UInt16: return(value.ToUInt16(provider));

            case TypeCode.UInt32: return(value.ToUInt32(provider));

            case TypeCode.UInt64: return(value.ToUInt64(provider));

            default:
                throw new InvalidCastException("Can't convert from " + value.GetType().FullName + " to " +
                                               (destinationType == null ? "NULL" : destinationType.FullName));
            }
        }
            public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
            {
                if (value is null)
                {
                    return(base.ConvertFrom(context, culture, value));
                }

                Type fromType = value.GetType();

                if (fromType.IsNullable())
                {
                    fromType = fromType.GetGenericArguments()[0];
                }
                TypeCode fromTypeCode = Type.GetTypeCode(fromType);

                IConvertible convertible = value as IConvertible;

                switch (fromTypeCode)
                {
                case TypeCode.DBNull: return(default);

                case TypeCode.Boolean: return((Boolean)value ? '1' : '0');

                case TypeCode.Char: return((Char)value);

                case TypeCode.SByte:
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.Byte:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                case TypeCode.UInt64:
                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Decimal:
                case TypeCode.DateTime:
                    return(convertible.ToChar(defaultProvider));

                default: return(base.ConvertFrom(context, culture, value));
                }
            }
Exemple #29
0
        static void invalidcast()
        {
            Console.WriteLine("in this task i have used a boolean value and tried to cast it as a character value, which is going to throw the exception");
            Console.WriteLine("-----------------");
            Console.WriteLine();
            Console.WriteLine("after execution:");
            bool value = true;

            try
            {
                IConvertible conv = value;
                Char         ch   = conv.ToChar(null);
                Console.WriteLine("Conversion succeeded.");
            }
            catch (InvalidCastException exception)
            {
                Console.WriteLine(exception.GetType().ToString() + " was detected " + "\nwith message: " + exception.Message);
                Console.WriteLine();
            }
        }
        public static char FromObject(object Value)
        {
            if (Value == null)
            {
                return('\0');
            }
            IConvertible convertible = Value as IConvertible;

            if (convertible != null)
            {
                switch (convertible.GetTypeCode())
                {
                case TypeCode.Char:
                    return(convertible.ToChar(null));

                case TypeCode.String:
                    return(FromString(convertible.ToString(null)));
                }
            }
            throw new InvalidCastException(Utils.GetResourceString("InvalidCast_FromTo", new string[] { Utils.VBFriendlyName(Value), "Char" }));
        }
        internal static double ToNumber(object value, IConvertible ic)
        {
            switch (GetTypeCode(value, ic))
            {
                case TypeCode.Empty:
                    return double.NaN;

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

                case TypeCode.Boolean:
                    return (ic.ToBoolean(null) ? ((double) 1) : ((double) 0));

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

                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                    return (double) ic.ToInt32(null);

                case TypeCode.UInt32:
                case TypeCode.Int64:
                    return (double) ic.ToInt64(null);

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

                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Decimal:
                    return ic.ToDouble(null);

                case TypeCode.String:
                    return ToNumber(ic.ToString(null));
            }
            return 0.0;
        }
        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;
        }
Exemple #33
0
        protected void EncodeConvertible(IConvertible value, Stream output)
        {
            output.WriteByte((byte)value.GetTypeCode());
            byte[] result;
            switch (value.GetTypeCode())
            {
            // the following encode directly on the stream
            case TypeCode.Boolean: output.WriteByte((byte)((bool)value ? 1 : 0)); return;
            case TypeCode.Byte: output.WriteByte(value.ToByte(null)); return;
            case TypeCode.SByte: output.WriteByte((byte)(value.ToSByte(null) + 128)); return;

            case TypeCode.Object:
                formatter.Serialize(output, value);
                return;

            case TypeCode.String: {
                long lengthPosition = output.Position;
                output.Write(new byte[4], 0, 4);
                StreamWriter w = new StreamWriter(output, Encoding.UTF8);
                w.Write((string)value);
                w.Flush();
                long savedPosition = output.Position;
                uint payloadLength = (uint)(output.Position - lengthPosition - 4);
                output.Position = lengthPosition;
                output.Write(DataConverter.Converter.GetBytes(payloadLength), 0, 4);
                output.Position = savedPosition;
                return;
            }

            // the following obtain byte arrays which are dumped below
            case TypeCode.Char: result = DataConverter.Converter.GetBytes(value.ToChar(null)); break;
            case TypeCode.Single: result = DataConverter.Converter.GetBytes(value.ToSingle(null)); break;
            case TypeCode.Double: result = DataConverter.Converter.GetBytes(value.ToDouble(null)); break;
            case TypeCode.Int16: result = DataConverter.Converter.GetBytes(value.ToInt16(null)); break;
            case TypeCode.Int32: result = DataConverter.Converter.GetBytes(value.ToInt32(null)); break;
            case TypeCode.Int64: result = DataConverter.Converter.GetBytes(value.ToInt64(null)); break;
            case TypeCode.UInt16: result = DataConverter.Converter.GetBytes(value.ToUInt16(null)); break;
            case TypeCode.UInt32: result = DataConverter.Converter.GetBytes(value.ToUInt32(null)); break;
            case TypeCode.UInt64: result = DataConverter.Converter.GetBytes(value.ToUInt64(null)); break;
            case TypeCode.DateTime: result = DataConverter.Converter.GetBytes(((DateTime)value).ToBinary()); break;

            default: throw new MarshallingException("Unhandled form of IConvertible: " + value.GetTypeCode());
            }
            output.Write(result, 0, result.Length);
        }
 private object ExecuteBinaryOperator(IConvertible left, IConvertible right, CodeBinaryOperatorType op)
 {
     TypeCode typeCode = left.GetTypeCode();
     TypeCode code2 = right.GetTypeCode();
     TypeCode[] codeArray = new TypeCode[] { TypeCode.Byte, TypeCode.Char, TypeCode.Int16, TypeCode.UInt16, TypeCode.Int32, TypeCode.UInt32, TypeCode.Int64, TypeCode.UInt64 };
     int num = -1;
     int num2 = -1;
     for (int i = 0; i < codeArray.Length; i++)
     {
         if (typeCode == codeArray[i])
         {
             num = i;
         }
         if (code2 == codeArray[i])
         {
             num2 = i;
         }
         if ((num != -1) && (num2 != -1))
         {
             break;
         }
     }
     if ((num == -1) || (num2 == -1))
     {
         return left;
     }
     int index = Math.Max(num, num2);
     object obj2 = left;
     switch (codeArray[index])
     {
         case TypeCode.Char:
         {
             char ch = left.ToChar(null);
             char ch2 = right.ToChar(null);
             if (op != CodeBinaryOperatorType.BitwiseOr)
             {
                 obj2 = ch & ch2;
                 break;
             }
             obj2 = ch | ch2;
             break;
         }
         case TypeCode.Byte:
         {
             byte num5 = left.ToByte(null);
             byte num6 = right.ToByte(null);
             if (op != CodeBinaryOperatorType.BitwiseOr)
             {
                 obj2 = num5 & num6;
                 break;
             }
             obj2 = num5 | num6;
             break;
         }
         case TypeCode.Int16:
         {
             short num7 = left.ToInt16(null);
             short num8 = right.ToInt16(null);
             if (op != CodeBinaryOperatorType.BitwiseOr)
             {
                 obj2 = num7 & num8;
                 break;
             }
             obj2 = (short) (((ushort) num7) | ((ushort) num8));
             break;
         }
         case TypeCode.UInt16:
         {
             ushort num9 = left.ToUInt16(null);
             ushort num10 = right.ToUInt16(null);
             if (op != CodeBinaryOperatorType.BitwiseOr)
             {
                 obj2 = num9 & num10;
                 break;
             }
             obj2 = num9 | num10;
             break;
         }
         case TypeCode.Int32:
         {
             int num11 = left.ToInt32(null);
             int num12 = right.ToInt32(null);
             if (op != CodeBinaryOperatorType.BitwiseOr)
             {
                 obj2 = num11 & num12;
                 break;
             }
             obj2 = num11 | num12;
             break;
         }
         case TypeCode.UInt32:
         {
             uint num13 = left.ToUInt32(null);
             uint num14 = right.ToUInt32(null);
             if (op != CodeBinaryOperatorType.BitwiseOr)
             {
                 obj2 = num13 & num14;
                 break;
             }
             obj2 = num13 | num14;
             break;
         }
         case TypeCode.Int64:
         {
             long num15 = left.ToInt64(null);
             long num16 = right.ToInt64(null);
             if (op != CodeBinaryOperatorType.BitwiseOr)
             {
                 obj2 = num15 & num16;
                 break;
             }
             obj2 = num15 | num16;
             break;
         }
         case TypeCode.UInt64:
         {
             ulong num17 = left.ToUInt64(null);
             ulong num18 = right.ToUInt64(null);
             if (op != CodeBinaryOperatorType.BitwiseOr)
             {
                 obj2 = num17 & num18;
                 break;
             }
             obj2 = num17 | num18;
             break;
         }
     }
     if ((obj2 != left) && (left is Enum))
     {
         obj2 = Enum.ToObject(left.GetType(), obj2);
     }
     return obj2;
 }
 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
 }
	// 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");
				}
			}
        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;
        }
Exemple #38
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: return (uint)ic.ToSingle(null);
     case TypeCode.Double:
     case TypeCode.Decimal: return (uint)ic.ToDouble(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)Convert.ToNumber(ic.ToString(null));
   }
   return 0; //should never get here
 }
Exemple #39
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
 }
        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));
        }
 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);
 }
		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
			}));
		}
Exemple #43
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));
        }
        internal static bool ToBoolean(object value, IConvertible ic)
        {
            switch (GetTypeCode(value, ic))
            {
                case TypeCode.Empty:
                    return false;

                case TypeCode.Object:
                {
                    if ((value is Microsoft.JScript.Missing) || (value is System.Reflection.Missing))
                    {
                        return false;
                    }
                    Type type = value.GetType();
                    MethodInfo method = type.GetMethod("op_True", BindingFlags.ExactBinding | BindingFlags.Public | BindingFlags.Static, null, new Type[] { type }, null);
                    if (((method != null) && ((method.Attributes & MethodAttributes.SpecialName) != MethodAttributes.PrivateScope)) && (method.ReturnType == typeof(bool)))
                    {
                        method = new JSMethodInfo(method);
                        return (bool) method.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) != '\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) != 0L);

                case TypeCode.UInt64:
                    return (ic.ToUInt64(null) != 0L);

                case TypeCode.Single:
                case TypeCode.Double:
                {
                    double num = ic.ToDouble(null);
                    return ((num == num) && !(num == 0.0));
                }
                case TypeCode.Decimal:
                    return (ic.ToDecimal(null) != 0M);

                case TypeCode.DateTime:
                    return true;

                case TypeCode.String:
                    return (ic.ToString(null).Length != 0);
            }
            return false;
        }
Exemple #45
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;
            }
        }
Exemple #46
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));
        }