Exemple #1
0
        public JsonWriter Name(object name)
        {
            if (name == null)
            {
                return(this.Name());
            }
            IConvertible convertible = name as IConvertible;

            if (convertible != null)
            {
                if (name is Enum)
                {
                    this.PreName().Append('"');
                    this.AppendInteger(convertible);
                    this.sb.Append2(CharEx.PNameEnd);
                    return(this);
                }
                switch (convertible.GetTypeCode())
                {
                case TypeCode.Empty:
                case TypeCode.DBNull:
                    return(this.Name());

                case TypeCode.Boolean:
                    return(this.Name(convertible.ToBoolean(null), false));

                case TypeCode.Char:
                    return(this.Name(convertible.ToChar(null)));

                case TypeCode.SByte:
                case TypeCode.Int16:
                case TypeCode.Int32:
                    return(this.Name(convertible.ToInt32(null)));

                case TypeCode.Byte:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                    return(this.Name(convertible.ToUInt32(null)));

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

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

                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Decimal:
                    return(this.Name(convertible.ToString(NumberFormatInfo.InvariantInfo)));

                case TypeCode.DateTime:
                    return(this.Name(convertible.ToDateTime(null), false));

                case TypeCode.String:
                    return(this.Name(name.ToString()));
                }
            }
            if (name is Guid)
            {
                return(this.Name((Guid)name, null));
            }
            return(this.Name(name.ToString()));
        }
Exemple #2
0
        // Helper code: on the back propagation path where a VT_BYREF VARIANT*
        // is marshaled to a "ref Object", we use this helper to force the
        // updated object back to the original type.
        internal static void MarshalHelperCastVariant(Object pValue, int vt, ref Variant v)
        {
            IConvertible iv = pValue as IConvertible;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                default:
                    throw new InvalidCastException(SR.InvalidCast_CannotCoerceByRefVariant);
                }
            }
        }
        public static string ToString(object value)
        {
            if (value == null)
            {
                return(JsonConvert.Null);
            }
            IConvertible convertible = value as IConvertible;

            if (convertible == null)
            {
                if (value is DateTimeOffset)
                {
                    return(JsonConvert.ToString((DateTimeOffset)value));
                }
                if (value is Guid)
                {
                    return(JsonConvert.ToString((Guid)value));
                }
                if (value is Uri)
                {
                    return(JsonConvert.ToString((Uri)value));
                }
                if (value is TimeSpan)
                {
                    return(JsonConvert.ToString((TimeSpan)value));
                }
            }
            else
            {
                switch (convertible.GetTypeCode())
                {
                case TypeCode.DBNull:
                {
                    return(JsonConvert.Null);
                }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                case TypeCode.Object | TypeCode.DateTime:
                {
                    break;
                }

                case TypeCode.String:
                {
                    return(JsonConvert.ToString(convertible.ToString(CultureInfo.InvariantCulture)));
                }

                default:
                {
                    goto case TypeCode.Object | TypeCode.DateTime;
                }
                }
            }
            throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, new object[] { value.GetType() }));
        }
Exemple #4
0
        private static int CompareConvertible(IConvertible comparisonValue, IConvertible predicateValue)
        {
//            try
//            {
            TypeCode comparisonType = comparisonValue.GetTypeCode();

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            default:
                return(String.CompareOrdinal(predicateValue.ToString(CultureInfo.CurrentCulture), comparisonValue.ToString(CultureInfo.CurrentCulture)));
            }
//            }
//            catch (FormatException)
//            {
//                return 1;
//            }
        }
Exemple #5
0
 internal static object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
 {
     if (targetType == null)
     {
         throw new ArgumentNullException("targetType");
     }
     if (value.GetType() == targetType)
     {
         return(value);
     }
     if (targetType == ConvertTypes[3])
     {
         return(value.ToBoolean(provider));
     }
     if (targetType == ConvertTypes[3])
     {
         return(value.ToChar(provider));
     }
     if (targetType == ConvertTypes[4])
     {
         return(value.ToSByte(provider));
     }
     if (targetType == ConvertTypes[5])
     {
         return(value.ToByte(provider));
     }
     if (targetType == ConvertTypes[6])
     {
         return(value.ToInt16(provider));
     }
     if (targetType == ConvertTypes[7])
     {
         return(value.ToUInt16(provider));
     }
     if (targetType == ConvertTypes[8])
     {
         return(value.ToInt32(provider));
     }
     if (targetType == ConvertTypes[9])
     {
         return(value.ToUInt32(provider));
     }
     if (targetType == ConvertTypes[10])
     {
         return(value.ToInt64(provider));
     }
     if (targetType == ConvertTypes[11])
     {
         return(value.ToUInt64(provider));
     }
     if (targetType == ConvertTypes[12])
     {
         return(value.ToSingle(provider));
     }
     if (targetType == ConvertTypes[13])
     {
         return(value.ToDouble(provider));
     }
     if (targetType == ConvertTypes[14])
     {
         return(value.ToDecimal(provider));
     }
     if (targetType == ConvertTypes[0xF])
     {
         return(value.ToDateTime(provider));
     }
     if (targetType == ConvertTypes[0x11])
     {
         return(value.ToString(provider));
     }
     if (targetType == ConvertTypes[0])
     {
         return(value);
     }
     if (targetType == ConvertTypes[1])
     {
         throw new InvalidCastException("Invalid Cast to DBDefault");
     }
     throw new InvalidCastException(string.Format(CultureInfo.CurrentCulture, string.Format("Invalid Cast From {0} To {1}", value.GetType().FullName, targetType.FullName)));
 }
        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 #7
0
 internal static String ToString(Object value, PreferredType pref, IConvertible ic, bool explicitOK){
   Enum e = value as Enum;
   if (e != null) return e.ToString("G");
   EnumWrapper ew = value as EnumWrapper;
   if (ew != null) return ew.ToString();
   TypeCode code = Convert.GetTypeCode(value, ic);
   if (pref == PreferredType.LocaleString){
     switch (code){
       case TypeCode.SByte:
       case TypeCode.Byte:
       case TypeCode.Int16:
       case TypeCode.UInt16:
       case TypeCode.Int32:
       case TypeCode.UInt32:
       case TypeCode.Single:
       case TypeCode.Double: {
         double d = ic.ToDouble(null);
         return d.ToString(d <= -1e+15 || d >= 1e+15 ? "g" : "n", NumberFormatInfo.CurrentInfo);
       }
       case TypeCode.Int64: return ic.ToInt64(null).ToString("n", NumberFormatInfo.CurrentInfo);
       case TypeCode.UInt64: return ic.ToUInt64(null).ToString("n", NumberFormatInfo.CurrentInfo);
       case TypeCode.Decimal: return ic.ToDecimal(null).ToString("n", NumberFormatInfo.CurrentInfo);
     }
   }
   switch (code){
     case TypeCode.Empty: return explicitOK ? "undefined" : null;
     case TypeCode.Object: return Convert.ToString(Convert.ToPrimitive(value, pref, ref ic), ic);
     case TypeCode.DBNull: return explicitOK ? "null" : null;
     case TypeCode.Boolean: return ic.ToBoolean(null) ? "true" : "false";
     case TypeCode.Char:
     case TypeCode.SByte:
     case TypeCode.Byte:
     case TypeCode.Int16:
     case TypeCode.UInt16:
     case TypeCode.Int32:
     case TypeCode.UInt32:
     case TypeCode.Int64:
     case TypeCode.UInt64:
     case TypeCode.Decimal:
     case TypeCode.String: return ic.ToString(null);
     case TypeCode.DateTime: return Convert.ToString(DateConstructor.ob.Construct(ic.ToDateTime(null)));
     case TypeCode.Single:
     case TypeCode.Double: return Convert.ToString(ic.ToDouble(null));
   }
   return null; //Should never get here
 }
Exemple #8
0
        object IConvertible.ToType(Type conversionType, IFormatProvider provider)
        {
            if (conversionType == null || conversionType.AssemblyQualifiedName == (typeof(double)).AssemblyQualifiedName)
            {
                return(ToDouble());
            }
            IConvertible c = this;

            if (conversionType.AssemblyQualifiedName == (typeof(float)).AssemblyQualifiedName)
            {
                c.ToSingle(provider);
            }
            if (conversionType.AssemblyQualifiedName == (typeof(int)).AssemblyQualifiedName)
            {
                c.ToInt32(provider);
            }
            if (conversionType.AssemblyQualifiedName == (typeof(string)).AssemblyQualifiedName)
            {
                c.ToString(provider);
            }
            if (conversionType.AssemblyQualifiedName == (typeof(long)).AssemblyQualifiedName)
            {
                c.ToInt64(provider);
            }
            if (conversionType.AssemblyQualifiedName == (typeof(decimal)).AssemblyQualifiedName)
            {
                c.ToDecimal(provider);
            }
            if (conversionType.AssemblyQualifiedName == (typeof(uint)).AssemblyQualifiedName)
            {
                c.ToUInt32(provider);
            }
            if (conversionType.AssemblyQualifiedName == (typeof(ulong)).AssemblyQualifiedName)
            {
                c.ToUInt64(provider);
            }
            if (conversionType.AssemblyQualifiedName == (typeof(short)).AssemblyQualifiedName)
            {
                c.ToInt16(provider);
            }
            if (conversionType.AssemblyQualifiedName == (typeof(ushort)).AssemblyQualifiedName)
            {
                c.ToUInt16(provider);
            }
            if (conversionType.AssemblyQualifiedName == (typeof(sbyte)).AssemblyQualifiedName)
            {
                c.ToSByte(provider);
            }
            if (conversionType.AssemblyQualifiedName == (typeof(byte)).AssemblyQualifiedName)
            {
                c.ToByte(provider);
            }
            if (conversionType.AssemblyQualifiedName == (typeof(DateTime)).AssemblyQualifiedName)
            {
                c.ToDateTime(provider);
            }
            if (conversionType.AssemblyQualifiedName == (typeof(bool)).AssemblyQualifiedName)
            {
                c.ToBoolean(provider);
            }
            if (conversionType.AssemblyQualifiedName == (typeof(char)).AssemblyQualifiedName)
            {
                c.ToChar(provider);
            }
            return(Convert.ChangeType(ToDouble(), conversionType));
        }
Exemple #9
0
        /// <summary>
        /// Tries the convert by IConvertible interface.
        /// </summary>
        /// <param name="value">The value.</param>
        /// <param name="type">The type.</param>
        /// <param name="culture">The culture.</param>
        /// <returns>Converted value.</returns>
        private static object TryConvertByIConvertible(object value, Type type, CultureInfo culture)
        {
            IConvertible convertible = value as IConvertible;

            if (convertible != null)
            {
                if (type == typeof(bool))
                {
                    return(convertible.ToBoolean(culture));
                }

                if (type == typeof(byte))
                {
                    return(convertible.ToByte(culture));
                }

                if (type == typeof(char))
                {
                    return(convertible.ToChar(culture));
                }

                if (type == typeof(DateTime))
                {
                    return(convertible.ToDateTime(culture));
                }

                if (type == typeof(decimal))
                {
                    return(convertible.ToDecimal(culture));
                }

                if (type == typeof(double))
                {
                    return(convertible.ToDouble(culture));
                }

                if (type == typeof(short))
                {
                    return(convertible.ToInt16(culture));
                }

                if (type == typeof(int))
                {
                    return(convertible.ToInt32(culture));
                }

                if (type == typeof(long))
                {
                    return(convertible.ToInt64(culture));
                }

                if (type == typeof(sbyte))
                {
                    return(convertible.ToSByte(culture));
                }

                if (type == typeof(float))
                {
                    return(convertible.ToSingle(culture));
                }

                if (type == typeof(ushort))
                {
                    return(convertible.ToUInt16(culture));
                }

                if (type == typeof(uint))
                {
                    return(convertible.ToUInt32(culture));
                }

                if (type == typeof(ulong))
                {
                    return(convertible.ToUInt64(culture));
                }
            }

            throw GetConversionException(value, type, culture);
        }
Exemple #10
0
        private string GetDateTimeSortKey(IConvertible value)
        {
            long ticks = value.ToDateTime(null).Ticks;

            return(GetNumericSortKey(string.Format(CultureInfo.InvariantCulture, "{0:e20}", ticks), ticks == 0));
        }
Exemple #11
0
        /// <summary>
        /// 转换数据类型
        /// </summary>
        /// <param name="value"></param>
        /// <param name="conversionType"></param>
        /// <param name="provider"></param>
        /// <returns></returns>
        internal static object ChangeValueType(object value, Type conversionType, IFormatProvider provider)
        {
            if (conversionType == null)
            {
                throw new ArgumentNullException("conversionType");
            }

            bool valueCanbeNull = IsValueNullable(conversionType);

            if (valueCanbeNull && (value == null || value.ToString().Length == 0))//如果Nullable<>类型,且值是空,则直接返回空
            {
                return(null);
            }
            if (value == null)
            {
                if (conversionType.IsValueType)
                {
                    throw new InvalidCastException("值为空!");
                }
                return(null);
            }
            IConvertible convertible = value as IConvertible;

            if (convertible == null)
            {
                if (value.GetType() != conversionType)
                {
                    throw new InvalidCastException("值不能被转换!");
                }
                return(value);
            }
            if (conversionType == typeof(System.Boolean) || conversionType == typeof(Nullable <System.Boolean>))
            {
                if (value.ToString() == "1")
                {
                    return(true);
                }
                if (value.ToString() == "0")
                {
                    return(false);
                }
                return(convertible.ToBoolean(provider));
            }
            if (conversionType == typeof(System.Char) || conversionType == typeof(Nullable <System.Char>))
            {
                return(convertible.ToChar(provider));
            }
            if (conversionType == typeof(System.SByte) || conversionType == typeof(Nullable <System.SByte>))
            {
                return(convertible.ToSByte(provider));
            }
            if (conversionType == typeof(System.Byte) || conversionType == typeof(Nullable <System.Byte>))
            {
                return(convertible.ToByte(provider));
            }
            if (conversionType == typeof(System.Int16) || conversionType == typeof(Nullable <System.Int16>))
            {
                return(convertible.ToInt16(provider));
            }
            if (conversionType == typeof(System.UInt16) || conversionType == typeof(Nullable <System.UInt16>))
            {
                return(convertible.ToUInt16(provider));
            }
            if (conversionType == typeof(System.Int32) || conversionType == typeof(Nullable <System.Int32>))
            {
                return(convertible.ToInt32(provider));
            }
            if (conversionType == typeof(System.UInt32) || conversionType == typeof(Nullable <System.UInt32>))
            {
                return(convertible.ToUInt32(provider));
            }
            if (conversionType == typeof(System.Int64) || conversionType == typeof(Nullable <System.Int64>))
            {
                return(convertible.ToInt64(provider));
            }
            if (conversionType == typeof(System.UInt64) || conversionType == typeof(Nullable <System.UInt64>))
            {
                return(convertible.ToUInt64(provider));
            }
            if (conversionType == typeof(System.Single) || conversionType == typeof(Nullable <System.Single>))
            {
                return(convertible.ToSingle(provider));
            }
            if (conversionType == typeof(System.Double) || conversionType == typeof(Nullable <System.Double>))
            {
                return(convertible.ToDouble(provider));
            }
            if (conversionType == typeof(System.Decimal) || conversionType == typeof(Nullable <System.Decimal>))
            {
                return(convertible.ToDecimal(provider));
            }
            if (conversionType == typeof(System.DateTime) || conversionType == typeof(Nullable <System.DateTime>))
            {
                return(convertible.ToDateTime(provider));
            }
            if (conversionType == typeof(System.String))
            {
                return(convertible.ToString(provider));
            }
            if (conversionType == typeof(System.Object))
            {
                return(value);
            }
            return(value);
        }
Exemple #12
0
        public static string ToString(object value)
        {
            if (value == null)
            {
                return(JsonConvert.Null);
            }
            IConvertible convertible = ConvertUtils.ToConvertible(value);

            if (convertible != null)
            {
                switch (convertible.GetTypeCode())
                {
                case TypeCode.DBNull:
                    return(JsonConvert.Null);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                case TypeCode.String:
                    return(JsonConvert.ToString(convertible.ToString((IFormatProvider)CultureInfo.InvariantCulture)));
                }
            }
            else
            {
                if (value is Guid)
                {
                    return(JsonConvert.ToString((Guid)value));
                }
                if (value is Uri)
                {
                    return(JsonConvert.ToString((Uri)value));
                }
                if (value is TimeSpan)
                {
                    return(JsonConvert.ToString((TimeSpan)value));
                }
            }
            throw new ArgumentException(StringUtils.FormatWith("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.", (IFormatProvider)CultureInfo.InvariantCulture, (object)value.GetType()));
        }
Exemple #13
0
        internal static void MarshalHelperCastVariant(object pValue, int vt, ref Variant v)
        {
            IConvertible convertible = pValue as IConvertible;

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

                case 9:
                    v = new Variant((object)new DispatchWrapper(pValue));
                    break;

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

                case 13:
                    v = new Variant((object)new UnknownWrapper(pValue));
                    break;

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

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

                case 1:
                    v = Variant.DBNull;
                    break;

                case 2:
                    v = new Variant(convertible.ToInt16(provider));
                    break;

                case 3:
                    v = new Variant(convertible.ToInt32(provider));
                    break;

                case 4:
                    v = new Variant(convertible.ToSingle(provider));
                    break;

                case 5:
                    v = new Variant(convertible.ToDouble(provider));
                    break;

                case 6:
                    v = new Variant((object)new CurrencyWrapper(convertible.ToDecimal(provider)));
                    break;

                case 7:
                    v = new Variant(convertible.ToDateTime(provider));
                    break;

                case 8:
                    v = new Variant((object)convertible.ToString(provider));
                    break;

                case 9:
                    v = new Variant((object)new DispatchWrapper((object)convertible));
                    break;

                case 10:
                    v = new Variant((object)new ErrorWrapper(convertible.ToInt32(provider)));
                    break;

                case 11:
                    v = new Variant(convertible.ToBoolean(provider));
                    break;

                case 12:
                    v = new Variant((object)convertible);
                    break;

                case 13:
                    v = new Variant((object)new UnknownWrapper((object)convertible));
                    break;

                case 14:
                    v = new Variant(convertible.ToDecimal(provider));
                    break;

                case 16:
                    v = new Variant(convertible.ToSByte(provider));
                    break;

                case 17:
                    v = new Variant(convertible.ToByte(provider));
                    break;

                case 18:
                    v = new Variant(convertible.ToUInt16(provider));
                    break;

                case 19:
                    v = new Variant(convertible.ToUInt32(provider));
                    break;

                case 20:
                    v = new Variant(convertible.ToInt64(provider));
                    break;

                case 21:
                    v = new Variant(convertible.ToUInt64(provider));
                    break;

                case 22:
                    v = new Variant(convertible.ToInt32(provider));
                    break;

                case 23:
                    v = new Variant(convertible.ToUInt32(provider));
                    break;

                default:
                    throw new InvalidCastException(Environment.GetResourceString("InvalidCast_CannotCoerceByRefVariant"));
                }
            }
        }
 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
 }
Exemple #15
0
    private static bool FromConvertible <TTarget>(IConvertible converter, out TTarget result2)
    {
        object result    = null;
        bool   hasChange = false;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        case TypeCode.String:
            result    = converter.ToString(CultureInfo.CurrentCulture);
            hasChange = true;
            break;
        }
        result2 = result == null ? default(TTarget) : (TTarget)result;
        return(hasChange);
    }
        DateTime GetDateTime(int i)
        {
            IConvertible c = (IConvertible)GetValue(i);

            return(c.ToDateTime(CultureInfo.CurrentCulture));
        }
Exemple #17
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));
        }
Exemple #18
0
        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
        }
		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
			}));
		}
        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));
        }
        internal static string ToString(object value, PreferredType pref, IConvertible ic, bool explicitOK)
        {
            Enum enum2 = value as Enum;
            if (enum2 != 0)
            {
                return enum2.ToString("G");
            }
            EnumWrapper wrapper = value as EnumWrapper;
            if (wrapper != null)
            {
                return wrapper.ToString();
            }
            TypeCode typeCode = GetTypeCode(value, ic);
            if (pref == PreferredType.LocaleString)
            {
                switch (typeCode)
                {
                    case TypeCode.SByte:
                    case TypeCode.Byte:
                    case TypeCode.Int16:
                    case TypeCode.UInt16:
                    case TypeCode.Int32:
                    case TypeCode.UInt32:
                    case TypeCode.Single:
                    case TypeCode.Double:
                    {
                        double num = ic.ToDouble(null);
                        return num.ToString(((num <= -1E+15) || (num >= 1E+15)) ? "g" : "n", NumberFormatInfo.CurrentInfo);
                    }
                    case TypeCode.Int64:
                        return ic.ToInt64(null).ToString("n", NumberFormatInfo.CurrentInfo);

                    case TypeCode.UInt64:
                        return ic.ToUInt64(null).ToString("n", NumberFormatInfo.CurrentInfo);

                    case TypeCode.Decimal:
                        return ic.ToDecimal(null).ToString("n", NumberFormatInfo.CurrentInfo);
                }
            }
            switch (typeCode)
            {
                case TypeCode.Empty:
                    if (explicitOK)
                    {
                        return "undefined";
                    }
                    return null;

                case TypeCode.Object:
                    return ToString(ToPrimitive(value, pref, ref ic), ic);

                case TypeCode.DBNull:
                    if (explicitOK)
                    {
                        return "null";
                    }
                    return null;

                case TypeCode.Boolean:
                    if (ic.ToBoolean(null))
                    {
                        return "true";
                    }
                    return "false";

                case TypeCode.Char:
                case TypeCode.SByte:
                case TypeCode.Byte:
                case TypeCode.Int16:
                case TypeCode.UInt16:
                case TypeCode.Int32:
                case TypeCode.UInt32:
                case TypeCode.Int64:
                case TypeCode.UInt64:
                case TypeCode.Decimal:
                case TypeCode.String:
                    return ic.ToString(null);

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

                case TypeCode.DateTime:
                    return ToString(DateConstructor.ob.Construct(ic.ToDateTime(null)));
            }
            return null;
        }
	// 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");
				}
			}
Exemple #23
0
    private Boolean CallInterfaceMethods(Enum en)
    {
        Boolean      pass  = true;
        IConvertible icon1 = (IConvertible)en;

        if (!icon1.ToBoolean(null))
        {
            pass = false;
        }
        if (icon1.ToByte(null) != 1)
        {
            pass = false;
        }
        if (icon1.ToChar(null) != (Char)1)
        {
            pass = false;
        }
        try{
            icon1.ToDateTime(null);
            pass = false;
        }catch (InvalidCastException) {
        }catch (Exception) {
            pass = false;
        }
        if (icon1.ToDecimal(null) != 1)
        {
            pass = false;
        }
        if (icon1.ToDouble(null) != 1)
        {
            pass = false;
        }
        if (icon1.ToInt16(null) != 1)
        {
            pass = false;
        }
        if (icon1.ToInt32(null) != 1)
        {
            pass = false;
        }
        if (icon1.ToInt64(null) != 1)
        {
            pass = false;
        }
        if (icon1.ToSByte(null) != 1)
        {
            pass = false;
        }
        if (icon1.ToSingle(null) != 1f)
        {
            pass = false;
        }
        if (icon1.ToString(null) != "ONE")
        {
            pass = false;
        }
        if (icon1.ToUInt16(null) != 1)
        {
            pass = false;
        }
        if (icon1.ToUInt32(null) != 1)
        {
            pass = false;
        }
        if (icon1.ToUInt64(null) != 1)
        {
            pass = false;
        }
        return(pass);
    }
Exemple #24
0
        public DateTime ToDateTime(IFormatProvider provider)
        {
            IConvertible o = GetConvertible();

            return(o.ToDateTime(provider));
        }
Exemple #25
0
        /// <summary>
        /// Converts the <see cref="Object"/> to its JSON string representation.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <returns>A JSON string representation of the <see cref="Object"/>.</returns>
        public static string ToString(object value)
        {
            if (value == null)
            {
                return(Null);
            }

            IConvertible convertible = value as IConvertible;

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

                case TypeCode.Char:
                    return(ToString(convertible.ToChar(ObjectUtil.SysCulture)));

                case TypeCode.Boolean:
                    return(ToString(convertible.ToBoolean(ObjectUtil.SysCulture)));

                case TypeCode.SByte:
                    return(ToString(convertible.ToSByte(ObjectUtil.SysCulture)));

                case TypeCode.Int16:
                    return(ToString(convertible.ToInt16(ObjectUtil.SysCulture)));

                case TypeCode.UInt16:
                    return(ToString(convertible.ToUInt16(ObjectUtil.SysCulture)));

                case TypeCode.Int32:
                    return(ToString(convertible.ToInt32(ObjectUtil.SysCulture)));

                case TypeCode.Byte:
                    return(ToString(convertible.ToByte(ObjectUtil.SysCulture)));

                case TypeCode.UInt32:
                    return(ToString(convertible.ToUInt32(ObjectUtil.SysCulture)));

                case TypeCode.Int64:
                    return(ToString(convertible.ToInt64(ObjectUtil.SysCulture)));

                case TypeCode.UInt64:
                    return(ToString(convertible.ToUInt64(ObjectUtil.SysCulture)));

                case TypeCode.Single:
                    return(ToString(convertible.ToSingle(ObjectUtil.SysCulture)));

                case TypeCode.Double:
                    return(ToString(convertible.ToDouble(ObjectUtil.SysCulture)));

                case TypeCode.DateTime:
                    return(ToString(convertible.ToDateTime(ObjectUtil.SysCulture)));

                case TypeCode.Decimal:
                    return(ToString(convertible.ToDecimal(ObjectUtil.SysCulture)));
                    //case TypeCode.DBNull:
                    //    return Null;
                }
            }
            else if (value is DateTimeOffset)
            {
                return(ToString((DateTimeOffset)value));
            }

            TkDebug.ThrowToolkitException(string.Format(ObjectUtil.SysCulture,
                                                        "不支持的类型: {0}", value.GetType()), null);
            return(null);
        }
        public static string FromObject(object Value)
        {
            if (Value == null)
            {
                return(null);
            }
            string str2 = Value as string;

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

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

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

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

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

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

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

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

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

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

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

                case TypeCode.String:
                    return(convertible.ToString(null));
                }
            }
            else
            {
                char[] chArray = Value as char[];
                if ((chArray != null) && (chArray.Rank == 1))
                {
                    return(new string(CharArrayType.FromObject(Value)));
                }
            }
            throw new InvalidCastException(Utils.GetResourceString("InvalidCast_FromTo", new string[] { Utils.VBFriendlyName(Value), "String" }));
        }
        /// <summary>
        /// Changes the type of the value.
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        /// <param name="value"></param>
        /// <param name="provider"></param>
        /// <returns></returns>
        public object Convert(Type from, Type to, object value, IFormatProvider provider)
        {
            var          val         = GetTrueValue(value);
            IConvertible convertible = val as IConvertible;

            if (convertible == null)
            {
                throw new InvalidCastException(String.Format("Invalid cast. {0} does not implement the IConvertible interface.", value));
            }

            var cto = GetTrueType(to);

            if (cto == ConvertTypes[cBoolIndex])
            {
                return(convertible.ToBoolean(provider));
            }
            if (cto == ConvertTypes[cCharIndex])
            {
                return(convertible.ToChar(provider));
            }
            if (cto == ConvertTypes[cSbyteIndex])
            {
                return(convertible.ToSByte(provider));
            }
            if (cto == ConvertTypes[cByteIndex])
            {
                return(convertible.ToByte(provider));
            }
            if (cto == ConvertTypes[cShortIndex])
            {
                return(convertible.ToInt16(provider));
            }
            if (cto == ConvertTypes[cUshortIndex])
            {
                return(convertible.ToUInt16(provider));
            }
            if (cto == ConvertTypes[cIntIndex])
            {
                return(convertible.ToInt32(provider));
            }
            if (cto == ConvertTypes[cUintIndex])
            {
                return(convertible.ToUInt32(provider));
            }
            if (cto == ConvertTypes[cLongIndex])
            {
                return(convertible.ToInt64(provider));
            }
            if (cto == ConvertTypes[cUlongIndex])
            {
                return(convertible.ToUInt64(provider));
            }
            if (cto == ConvertTypes[cFloatIndex])
            {
                return(convertible.ToSingle(provider));
            }
            if (cto == ConvertTypes[cDoubleIndex])
            {
                return(convertible.ToDouble(provider));
            }
            if (cto == ConvertTypes[cDecimalIndex])
            {
                return(convertible.ToDecimal(provider));
            }
            if (cto == ConvertTypes[cDateTimeIndex])
            {
                return(convertible.ToDateTime(provider));
            }
            if (cto == ConvertTypes[cStringIndex])
            {
                return(convertible.ToString(provider));
            }
            if (cto == ConvertTypes[cObjectIndex])
            {
                return(value);
            }
            return(convertible.ToType(cto, provider));
        }
        public static string ToString(object value)
        {
            if (value == null)
            {
                return(JsonConvert.Null);
            }
            IConvertible convertible = value as IConvertible;

            if (convertible != null)
            {
                switch (convertible.GetTypeCode())
                {
                case 2:
                    return(JsonConvert.Null);

                case 3:
                    return(JsonConvert.ToString(convertible.ToBoolean(CultureInfo.get_InvariantCulture())));

                case 4:
                    return(JsonConvert.ToString(convertible.ToChar(CultureInfo.get_InvariantCulture())));

                case 5:
                    return(JsonConvert.ToString(convertible.ToSByte(CultureInfo.get_InvariantCulture())));

                case 6:
                    return(JsonConvert.ToString(convertible.ToByte(CultureInfo.get_InvariantCulture())));

                case 7:
                    return(JsonConvert.ToString(convertible.ToInt16(CultureInfo.get_InvariantCulture())));

                case 8:
                    return(JsonConvert.ToString(convertible.ToUInt16(CultureInfo.get_InvariantCulture())));

                case 9:
                    return(JsonConvert.ToString(convertible.ToInt32(CultureInfo.get_InvariantCulture())));

                case 10:
                    return(JsonConvert.ToString(convertible.ToUInt32(CultureInfo.get_InvariantCulture())));

                case 11:
                    return(JsonConvert.ToString(convertible.ToInt64(CultureInfo.get_InvariantCulture())));

                case 12:
                    return(JsonConvert.ToString(convertible.ToUInt64(CultureInfo.get_InvariantCulture())));

                case 13:
                    return(JsonConvert.ToString(convertible.ToSingle(CultureInfo.get_InvariantCulture())));

                case 14:
                    return(JsonConvert.ToString(convertible.ToDouble(CultureInfo.get_InvariantCulture())));

                case 15:
                    return(JsonConvert.ToString(convertible.ToDecimal(CultureInfo.get_InvariantCulture())));

                case 16:
                    return(JsonConvert.ToString(convertible.ToDateTime(CultureInfo.get_InvariantCulture())));

                case 18:
                    return(JsonConvert.ToString(convertible.ToString(CultureInfo.get_InvariantCulture())));
                }
            }
            else
            {
                if (value is DateTimeOffset)
                {
                    return(JsonConvert.ToString((DateTimeOffset)value));
                }
                if (value is Guid)
                {
                    return(JsonConvert.ToString((Guid)value));
                }
                if (value is Uri)
                {
                    return(JsonConvert.ToString((Uri)value));
                }
                if (value is TimeSpan)
                {
                    return(JsonConvert.ToString((TimeSpan)value));
                }
            }
            throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.get_InvariantCulture(), new object[]
            {
                value.GetType()
            }));
        }
        private static object TryConvertByIConvertible(object value, Type type, CultureInfo culture)
        {
            IConvertible convertible = value as IConvertible;

            if (convertible != null)
            {
                if (type == typeof(Boolean))
                {
                    return(convertible.ToBoolean(culture));
                }
                if (type == typeof(Byte))
                {
                    return(convertible.ToByte(culture));
                }
                if (type == typeof(Char))
                {
                    return(convertible.ToChar(culture));
                }
                if (type == typeof(DateTime))
                {
                    return(convertible.ToDateTime(culture));
                }
                if (type == typeof(Decimal))
                {
                    return(convertible.ToDecimal(culture));
                }
                if (type == typeof(Double))
                {
                    return(convertible.ToDouble(culture));
                }
                if (type == typeof(Int16))
                {
                    return(convertible.ToInt16(culture));
                }
                if (type == typeof(Int32))
                {
                    return(convertible.ToInt32(culture));
                }
                if (type == typeof(Int64))
                {
                    return(convertible.ToInt64(culture));
                }
                if (type == typeof(SByte))
                {
                    return(convertible.ToSByte(culture));
                }
                if (type == typeof(Single))
                {
                    return(convertible.ToSingle(culture));
                }
                if (type == typeof(UInt16))
                {
                    return(convertible.ToUInt16(culture));
                }
                if (type == typeof(UInt32))
                {
                    return(convertible.ToUInt32(culture));
                }
                if (type == typeof(UInt64))
                {
                    return(convertible.ToUInt64(culture));
                }
            }

            throw GetConversionException(value, type, culture);
        }
Exemple #30
0
        private void TranslateToIL(ILGenerator il, object val, Type rtype)
        {
            long         ticks;
            IConvertible iConvertible = Microsoft.JScript.Convert.GetIConvertible(val);

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

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

            case TypeCode.Boolean:
                TranslateToILInt(il, iConvertible.ToInt32(null));
                Microsoft.JScript.Convert.Emit(this, il, Typeob.Boolean, rtype);
                return;

            case TypeCode.Char:
            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Int32:
                TranslateToILInt(il, iConvertible.ToInt32(null));
                if (!rtype.IsEnum)
                {
                    if (val is EnumWrapper)
                    {
                        Microsoft.JScript.Convert.Emit(this, il, ((EnumWrapper)val).type, rtype);
                        return;
                    }
                    Microsoft.JScript.Convert.Emit(this, il, Globals.TypeRefs.ToReferenceContext(val.GetType()), rtype);
                    return;
                }
                return;

            case TypeCode.UInt32:
                TranslateToILInt(il, (int)iConvertible.ToUInt32(null));
                if (!rtype.IsEnum)
                {
                    if (val is EnumWrapper)
                    {
                        Microsoft.JScript.Convert.Emit(this, il, ((EnumWrapper)val).type, rtype);
                        return;
                    }
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.UInt32, rtype);
                    return;
                }
                return;

            case TypeCode.Int64:
                ticks = iConvertible.ToInt64(null);
                if ((-2147483648L > ticks) || (ticks > 0x7fffffffL))
                {
                    il.Emit(OpCodes.Ldc_I8, ticks);
                    break;
                }
                TranslateToILInt(il, (int)ticks);
                il.Emit(OpCodes.Conv_I8);
                break;

            case TypeCode.UInt64:
            {
                ulong num2 = iConvertible.ToUInt64(null);
                if (num2 > 0x7fffffffL)
                {
                    il.Emit(OpCodes.Ldc_I8, (long)num2);
                }
                else
                {
                    TranslateToILInt(il, (int)num2);
                    il.Emit(OpCodes.Conv_I8);
                }
                if (!rtype.IsEnum)
                {
                    if (val is EnumWrapper)
                    {
                        Microsoft.JScript.Convert.Emit(this, il, ((EnumWrapper)val).type, rtype);
                        return;
                    }
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.UInt64, rtype);
                }
                return;
            }

            case TypeCode.Single:
            {
                float arg = iConvertible.ToSingle(null);
                if ((arg != arg) || ((arg == 0f) && float.IsNegativeInfinity(1f / arg)))
                {
                    il.Emit(OpCodes.Ldc_R4, arg);
                }
                else
                {
                    int i = (int)Runtime.DoubleToInt64((double)arg);
                    if (((-128 > i) || (i > 0x7f)) || (arg != i))
                    {
                        il.Emit(OpCodes.Ldc_R4, arg);
                    }
                    else
                    {
                        TranslateToILInt(il, i);
                        il.Emit(OpCodes.Conv_R4);
                    }
                }
                Microsoft.JScript.Convert.Emit(this, il, Typeob.Single, rtype);
                return;
            }

            case TypeCode.Double:
            {
                double num5 = iConvertible.ToDouble(null);
                if ((num5 != num5) || ((num5 == 0.0) && double.IsNegativeInfinity(1.0 / num5)))
                {
                    il.Emit(OpCodes.Ldc_R8, num5);
                }
                else
                {
                    int num6 = (int)Runtime.DoubleToInt64(num5);
                    if (((-128 > num6) || (num6 > 0x7f)) || (num5 != num6))
                    {
                        il.Emit(OpCodes.Ldc_R8, num5);
                    }
                    else
                    {
                        TranslateToILInt(il, num6);
                        il.Emit(OpCodes.Conv_R8);
                    }
                }
                Microsoft.JScript.Convert.Emit(this, il, Typeob.Double, rtype);
                return;
            }

            case TypeCode.Decimal:
            {
                int[] bits = decimal.GetBits(iConvertible.ToDecimal(null));
                TranslateToILInt(il, bits[0]);
                TranslateToILInt(il, bits[1]);
                TranslateToILInt(il, bits[2]);
                il.Emit((bits[3] < 0) ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
                TranslateToILInt(il, (bits[3] & 0x7fffffff) >> 0x10);
                il.Emit(OpCodes.Newobj, CompilerGlobals.decimalConstructor);
                Microsoft.JScript.Convert.Emit(this, il, Typeob.Decimal, rtype);
                return;
            }

            case TypeCode.DateTime:
                ticks = iConvertible.ToDateTime(null).Ticks;
                il.Emit(OpCodes.Ldc_I8, ticks);
                Microsoft.JScript.Convert.Emit(this, il, Typeob.Int64, rtype);
                return;

            case TypeCode.String:
            {
                string str = iConvertible.ToString(null);
                if (!(rtype == Typeob.Char) || (str.Length != 1))
                {
                    il.Emit(OpCodes.Ldstr, str);
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.String, rtype);
                    return;
                }
                TranslateToILInt(il, str[0]);
                return;
            }

            default:
                if (val is Enum)
                {
                    if (rtype == Typeob.String)
                    {
                        this.TranslateToIL(il, val.ToString(), rtype);
                        return;
                    }
                    if (rtype.IsPrimitive)
                    {
                        this.TranslateToIL(il, System.Convert.ChangeType(val, Enum.GetUnderlyingType(Globals.TypeRefs.ToReferenceContext(val.GetType())), CultureInfo.InvariantCulture), rtype);
                        return;
                    }
                    Type enumType       = Globals.TypeRefs.ToReferenceContext(val.GetType());
                    Type underlyingType = Enum.GetUnderlyingType(enumType);
                    this.TranslateToIL(il, System.Convert.ChangeType(val, underlyingType, CultureInfo.InvariantCulture), underlyingType);
                    il.Emit(OpCodes.Box, enumType);
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.Object, rtype);
                }
                else if (val is EnumWrapper)
                {
                    if (rtype == Typeob.String)
                    {
                        this.TranslateToIL(il, val.ToString(), rtype);
                        return;
                    }
                    if (rtype.IsPrimitive)
                    {
                        this.TranslateToIL(il, ((EnumWrapper)val).ToNumericValue(), rtype);
                    }
                    else
                    {
                        Type type  = ((EnumWrapper)val).type;
                        Type type4 = Globals.TypeRefs.ToReferenceContext(((EnumWrapper)val).value.GetType());
                        this.TranslateToIL(il, ((EnumWrapper)val).value, type4);
                        il.Emit(OpCodes.Box, type);
                        Microsoft.JScript.Convert.Emit(this, il, Typeob.Object, rtype);
                    }
                }
                else if (val is Type)
                {
                    il.Emit(OpCodes.Ldtoken, (Type)val);
                    il.Emit(OpCodes.Call, CompilerGlobals.getTypeFromHandleMethod);
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.Type, rtype);
                }
                else if (val is Namespace)
                {
                    il.Emit(OpCodes.Ldstr, ((Namespace)val).Name);
                    base.EmitILToLoadEngine(il);
                    il.Emit(OpCodes.Call, CompilerGlobals.getNamespaceMethod);
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.Namespace, rtype);
                }
                else if (val is ClassScope)
                {
                    il.Emit(OpCodes.Ldtoken, ((ClassScope)val).GetTypeBuilderOrEnumBuilder());
                    il.Emit(OpCodes.Call, CompilerGlobals.getTypeFromHandleMethod);
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.Type, rtype);
                }
                else if (val is TypedArray)
                {
                    il.Emit(OpCodes.Ldtoken, Microsoft.JScript.Convert.ToType((TypedArray)val));
                    il.Emit(OpCodes.Call, CompilerGlobals.getTypeFromHandleMethod);
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.Type, rtype);
                }
                else if (val is NumberObject)
                {
                    this.TranslateToIL(il, ((NumberObject)val).value, Typeob.Object);
                    base.EmitILToLoadEngine(il);
                    il.Emit(OpCodes.Call, CompilerGlobals.toObjectMethod);
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.NumberObject, rtype);
                }
                else if (val is StringObject)
                {
                    il.Emit(OpCodes.Ldstr, ((StringObject)val).value);
                    base.EmitILToLoadEngine(il);
                    il.Emit(OpCodes.Call, CompilerGlobals.toObjectMethod);
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.StringObject, rtype);
                }
                else if (val is BooleanObject)
                {
                    il.Emit(((BooleanObject)val).value ? OpCodes.Ldc_I4_1 : OpCodes.Ldc_I4_0);
                    il.Emit(OpCodes.Box, Typeob.Boolean);
                    base.EmitILToLoadEngine(il);
                    il.Emit(OpCodes.Call, CompilerGlobals.toObjectMethod);
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.BooleanObject, rtype);
                }
                else if (val is ActiveXObjectConstructor)
                {
                    il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("ActiveXObject").GetGetMethod());
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
                }
                else if (val is ArrayConstructor)
                {
                    il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("Array").GetGetMethod());
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
                }
                else if (val is BooleanConstructor)
                {
                    il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("Boolean").GetGetMethod());
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
                }
                else if (val is DateConstructor)
                {
                    il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("Date").GetGetMethod());
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
                }
                else if (val is EnumeratorConstructor)
                {
                    il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("Enumerator").GetGetMethod());
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
                }
                else if (val is ErrorConstructor)
                {
                    ErrorConstructor constructor = (ErrorConstructor)val;
                    if (constructor == ErrorConstructor.evalOb)
                    {
                        il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("EvalError").GetGetMethod());
                    }
                    else if (constructor == ErrorConstructor.rangeOb)
                    {
                        il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("RangeError").GetGetMethod());
                    }
                    else if (constructor == ErrorConstructor.referenceOb)
                    {
                        il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("ReferenceError").GetGetMethod());
                    }
                    else if (constructor == ErrorConstructor.syntaxOb)
                    {
                        il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("SyntaxError").GetGetMethod());
                    }
                    else if (constructor == ErrorConstructor.typeOb)
                    {
                        il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("TypeError").GetGetMethod());
                    }
                    else if (constructor == ErrorConstructor.uriOb)
                    {
                        il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("URIError").GetGetMethod());
                    }
                    else
                    {
                        il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("Error").GetGetMethod());
                    }
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
                }
                else if (val is FunctionConstructor)
                {
                    il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("Function").GetGetMethod());
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
                }
                else if (val is MathObject)
                {
                    il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("Math").GetGetMethod());
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.JSObject, rtype);
                }
                else if (val is NumberConstructor)
                {
                    il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("Number").GetGetMethod());
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
                }
                else if (val is ObjectConstructor)
                {
                    il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("Object").GetGetMethod());
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
                }
                else if (val is RegExpConstructor)
                {
                    il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("RegExp").GetGetMethod());
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
                }
                else if (val is StringConstructor)
                {
                    il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("String").GetGetMethod());
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
                }
                else if (val is VBArrayConstructor)
                {
                    il.Emit(OpCodes.Call, Typeob.GlobalObject.GetProperty("VBArray").GetGetMethod());
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.ScriptFunction, rtype);
                }
                else if (val is IntPtr)
                {
                    il.Emit(OpCodes.Ldc_I8, (long)((IntPtr)val));
                    il.Emit(OpCodes.Conv_I);
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.IntPtr, rtype);
                }
                else if (val is UIntPtr)
                {
                    il.Emit(OpCodes.Ldc_I8, (long)((ulong)((UIntPtr)val)));
                    il.Emit(OpCodes.Conv_U);
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.UIntPtr, rtype);
                }
                else if (val is Microsoft.JScript.Missing)
                {
                    il.Emit(OpCodes.Ldsfld, CompilerGlobals.missingField);
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.Object, rtype);
                }
                else if (val is System.Reflection.Missing)
                {
                    if (rtype.IsPrimitive)
                    {
                        this.TranslateToIL(il, (double)1.0 / (double)0.0, rtype);
                    }
                    else if ((rtype != Typeob.Object) && !rtype.IsValueType)
                    {
                        il.Emit(OpCodes.Ldnull);
                    }
                    else
                    {
                        il.Emit(OpCodes.Ldsfld, CompilerGlobals.systemReflectionMissingField);
                        Microsoft.JScript.Convert.Emit(this, il, Typeob.Object, rtype);
                    }
                }
                else
                {
                    if (val == this.value)
                    {
                        throw new JScriptException(JSError.InternalError, base.context);
                    }
                    this.TranslateToIL(il, this.value, rtype);
                }
                return;
            }
            if (!rtype.IsEnum)
            {
                if (val is EnumWrapper)
                {
                    Microsoft.JScript.Convert.Emit(this, il, ((EnumWrapper)val).type, rtype);
                }
                else
                {
                    Microsoft.JScript.Convert.Emit(this, il, Typeob.Int64, rtype);
                }
            }
        }
Exemple #31
0
        public JsonWriter Value(object value)
        {
            if (value == null)
            {
                return(this.Value());
            }
            IConvertible convertible = value as IConvertible;

            if (convertible != null)
            {
                if (!this._EnumToNumber && value is Enum)
                {
                    this.PreValue().Append('"').Append(value.ToString()).Append('"');
                    return(this);
                }
                switch (convertible.GetTypeCode())
                {
                case TypeCode.Empty:
                case TypeCode.DBNull:
                    return(this.Value());

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

                case TypeCode.Char:
                    return(this.Value(convertible.ToChar(null)));

                case TypeCode.SByte:
                case TypeCode.Int16:
                case TypeCode.Int32:
                    this.PreValue().Append(convertible.ToInt32(null));
                    return(this);

                case TypeCode.Byte:
                case TypeCode.UInt16:
                case TypeCode.UInt32:
                    this.PreValue().Append(convertible.ToUInt32(null));
                    return(this);

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

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

                case TypeCode.Single:
                case TypeCode.Double:
                case TypeCode.Decimal:
                    this.PreValue().Append(convertible.ToString(NumberFormatInfo.InvariantInfo));
                    return(this);

                case TypeCode.DateTime:
                    return(this.Value(convertible.ToDateTime(null)));

                case TypeCode.String:
                    return(this.Value(value.ToString()));
                }
            }
            IToJson toJson = value as IToJson;

            if (toJson != null)
            {
                toJson.WriteTo(this);
                return(this);
            }
            JsonWriterEx ex = JsonWriterEx.GetEx(value.GetType());

            if (ex.IsSimpleType)
            {
                ex.Invoke(this, value);
                return(this);
            }
            if (!this._CheckLoopReference)
            {
                if (this._Depth > 32)
                {
                    Throw.NotSupported("对象过于复杂或存在循环引用", false);
                }
                this._Depth++;
                if (ex.IsCustomType)
                {
                    ex.WriteTo(this, value);
                }
                else
                {
                    ex.Invoke(this, value);
                }
                this._Depth--;
                return(this);
            }
            if (this._Loop.Contains(value))
            {
                this.PreValue().Append9(CharEx.PUndefined);
            }
            else
            {
                int index = this._Loop.Add(value);
                if (ex.IsCustomType)
                {
                    ex.WriteTo(this, value);
                }
                else
                {
                    ex.Invoke(this, value);
                }
                this._Loop.RemoveAt(index);
            }
            return(this);
        }
Exemple #32
0
        internal static void MarshalHelperConvertObjectToVariant(object o, ref System.Variant v)
        {
            IConvertible convertible = RemotingServices.IsTransparentProxy(o) ? null : (o as IConvertible);

            if (o == null)
            {
                v = Empty;
            }
            else if (convertible == null)
            {
                v = new System.Variant(o);
            }
            else
            {
                IFormatProvider invariantCulture = CultureInfo.InvariantCulture;
                switch (convertible.GetTypeCode())
                {
                case TypeCode.Empty:
                    v = Empty;
                    return;

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

                case TypeCode.DBNull:
                    v = DBNull;
                    return;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                case TypeCode.String:
                    v = new System.Variant(convertible.ToString(invariantCulture));
                    return;
                }
                throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnknownTypeCode", new object[] { convertible.GetTypeCode() }));
            }
        }
        /// <summary>
        /// Writes a <see cref="Object"/> value.
        /// An error will raised if the value cannot be written as a single JSON token.
        /// </summary>
        /// <param name="value">The <see cref="Object"/> value to write.</param>
        public virtual void WriteValue(object value)
        {
            if (value == null)
            {
                WriteNull();
                return;
            }
            else if (value is IConvertible)
            {
                IConvertible convertible = value as IConvertible;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
        }
Exemple #34
0
        internal static void MarshalHelperCastVariant(object pValue, int vt, ref System.Variant v)
        {
            IConvertible convertible = pValue as IConvertible;

            if (convertible != null)
            {
                IFormatProvider invariantCulture = CultureInfo.InvariantCulture;
                switch (vt)
                {
                case 0:
                    v = Empty;
                    return;

                case 1:
                    v = DBNull;
                    return;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            case 0x24:
                v = new System.Variant(pValue);
                return;
            }
            throw new InvalidCastException(Environment.GetResourceString("InvalidCast_CannotCoerceByRefVariant"));
        }
Exemple #35
0
        // Helper code for marshaling managed objects to VARIANT's (we use
        // managed variants as an intermediate type.
        internal static void MarshalHelperConvertObjectToVariant(Object o, ref Variant v)
        {
            IConvertible ic = System.Runtime.Remoting.RemotingServices.IsTransparentProxy(o) ? null : o as IConvertible;

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

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

                case TypeCode.DBNull:
                    v = DBNull;
                    break;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                default:
                    throw new NotSupportedException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("NotSupported_UnknownTypeCode"), ic.GetTypeCode()));
                }
            }
        }
Exemple #36
0
        internal static object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider)
        {
            Debug.Assert(value != null, "[Convert.DefaultToType]value!=null");
            if (targetType == null)
            {
                throw new ArgumentNullException(nameof(targetType));
            }

            if (ReferenceEquals(value.GetType(), targetType))
            {
                return(value);
            }

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

            throw new InvalidCastException(string.Format("Invalid cast from '{0}' to '{1}'.", value.GetType().FullName, targetType.FullName));
            // TODO: SR
            //throw new InvalidCastException(string.Format(SR.InvalidCast_FromTo, value.GetType().FullName, targetType.FullName));
        }
Exemple #37
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));
        }
Exemple #38
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 (objTypeCode == TypeCode.Empty || 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:
            {
                float singleValue = value.ToSingle(CultureInfo.InvariantCulture);
                return(float.IsInfinity(singleValue) ?
                       Convert.ToString(singleValue, CultureInfo.InvariantCulture) :
                       XmlConvert.ToString(singleValue));
            }

            case TypeCode.Double:
            {
                double doubleValue = value.ToDouble(CultureInfo.InvariantCulture);
                return(double.IsInfinity(doubleValue) ?
                       Convert.ToString(doubleValue, CultureInfo.InvariantCulture) :
                       XmlConvert.ToString(doubleValue));
            }

            case TypeCode.Decimal:
                return(XmlConvert.ToString(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 #39
0
      private static Object ToPrimitive(Object value, PreferredType preferredType, IConvertible ic, TypeCode tcode){
        switch (tcode){
          case TypeCode.Object:
            System.Array arr = value as System.Array;
            if (arr != null && arr.Rank == 1) value = new ArrayWrapper(ArrayPrototype.ob, arr, true);
            if (value is ScriptObject){
              Object result = ((ScriptObject)value).GetDefaultValue(preferredType);
              if (Convert.GetTypeCode(result) != TypeCode.Object)
                return result;
              else if (value == result && preferredType == PreferredType.String || preferredType == PreferredType.LocaleString){
                if (value is JSObject){
                  ScriptObject proto = ((JSObject)value).GetParent();
                  if (proto is ClassScope)
                    return ((ClassScope)proto).GetFullName();
                  return "[object Object]";
                }
                return value.ToString();
              }else
                throw new JScriptException(JSError.TypeMismatch);
            }else if (value is Missing || value is System.Reflection.Missing)
              return null;
            else{
              IReflect ir;
              if (value is IReflect && !(value is Type))
                ir = (IReflect)value;
              else
                ir = value.GetType();

              //Look for an op_Explicit conversion to String or Double (this always fails for IDispatch/Ex objects
              MethodInfo meth = null;
              if (preferredType == PreferredType.String || preferredType == PreferredType.LocaleString)
                meth = Convert.GetToXXXXMethod(ir, Typeob.String, true);
              else{
                meth = Convert.GetToXXXXMethod(ir, Typeob.Double, true);
                if (meth == null)
                  meth = Convert.GetToXXXXMethod(ir, Typeob.Int64, true);
                if (meth == null)
                  meth = Convert.GetToXXXXMethod(ir, Typeob.UInt64, true);
              }
              if (meth != null){
                meth = new JSMethodInfo(meth);
                return meth.Invoke(null, BindingFlags.SuppressChangeType, null, new Object[]{value}, null);
              }

              //Invoke the default method/property or get the value of the default field. If an exception is thrown
              //because the target doesn't have a non-paramterized default member, mask it and execute the
              //default handling.
              try{
                try{
                  MemberInfo member = LateBinding.SelectMember(JSBinder.GetDefaultMembers(ir));
                  if (member != null){
                    switch(member.MemberType){
                      case MemberTypes.Field: return ((FieldInfo)member).GetValue(value);
                      case MemberTypes.Method: return ((MethodInfo)member).Invoke(value, new Object[0]);
                      case MemberTypes.Property: return JSProperty.GetValue((PropertyInfo)member, value, null);
                      case MemberTypes.Event: return null;
                      case MemberTypes.NestedType: return member;
                    }
                  }
                  
                  
                  return ir.InvokeMember(String.Empty, BindingFlags.ExactBinding|BindingFlags.SuppressChangeType|
                                                       BindingFlags.InvokeMethod|BindingFlags.GetProperty|BindingFlags.GetField,
                                         null, value, new Object[0], null, null, new String[0]);
                }catch(TargetInvocationException e){
                  throw e.InnerException;
                }
              }catch(ArgumentException){
              }catch(IndexOutOfRangeException){
              }catch(MissingMemberException){
              }catch(TargetParameterCountException){
              }

              if (preferredType != PreferredType.Number)
                if (value is Char[])
                  return new System.String((Char[])value);
                else
                  return value.ToString();
              return value;
            }
          case TypeCode.DateTime:
            return DateConstructor.ob.Construct(ic.ToDateTime(null)).GetDefaultValue(preferredType);
        }
        return value;
      }
Exemple #40
0
        /// <summary>
        /// Converts the <see cref="Object"/> to its JSON string representation.
        /// </summary>
        /// <param name="value">The value to convert.</param>
        /// <returns>A JSON string representation of the <see cref="Object"/>.</returns>
        public static string ToString(object value)
        {
            if (value == null)
            {
                return(Null);
            }

            IConvertible convertible = value as IConvertible;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

                case TypeCode.DBNull:
                    return(Null);
                }
            }
#if !PocketPC && !NET20
            else if (value is DateTimeOffset)
            {
                return(ToString((DateTimeOffset)value));
            }
#endif

            throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType()));
        }
Exemple #41
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;
            }
        }
        private static object ToPrimitive(object value, PreferredType preferredType, IConvertible ic, TypeCode tcode)
        {
            IReflect reflect;
            TypeCode code = tcode;
            if (code != TypeCode.Object)
            {
                if (code == TypeCode.DateTime)
                {
                    return DateConstructor.ob.Construct(ic.ToDateTime(null)).GetDefaultValue(preferredType);
                }
                return value;
            }
            Array array = value as Array;
            if ((array != null) && (array.Rank == 1))
            {
                value = new ArrayWrapper(ArrayPrototype.ob, array, true);
            }
            if (value is ScriptObject)
            {
                object defaultValue = ((ScriptObject) value).GetDefaultValue(preferredType);
                if (GetTypeCode(defaultValue) != TypeCode.Object)
                {
                    return defaultValue;
                }
                if (((value != defaultValue) || (preferredType != PreferredType.String)) && (preferredType != PreferredType.LocaleString))
                {
                    throw new JScriptException(JSError.TypeMismatch);
                }
                if (!(value is JSObject))
                {
                    return value.ToString();
                }
                ScriptObject parent = ((JSObject) value).GetParent();
                if (parent is ClassScope)
                {
                    return ((ClassScope) parent).GetFullName();
                }
                return "[object Object]";
            }
            if ((value is Microsoft.JScript.Missing) || (value is System.Reflection.Missing))
            {
                return null;
            }
            if ((value is IReflect) && !(value is Type))
            {
                reflect = (IReflect) value;
            }
            else
            {
                reflect = value.GetType();
            }
            MethodInfo method = null;
            if ((preferredType == PreferredType.String) || (preferredType == PreferredType.LocaleString))
            {
                method = GetToXXXXMethod(reflect, typeof(string), true);
            }
            else
            {
                method = GetToXXXXMethod(reflect, typeof(double), true);
                if (method == null)
                {
                    method = GetToXXXXMethod(reflect, typeof(long), true);
                }
                if (method == null)
                {
                    method = GetToXXXXMethod(reflect, typeof(ulong), true);
                }
            }
            if (method != null)
            {
                method = new JSMethodInfo(method);
                return method.Invoke(null, BindingFlags.SuppressChangeType, null, new object[] { value }, null);
            }
            try
            {
                try
                {
                    MemberInfo info2 = LateBinding.SelectMember(JSBinder.GetDefaultMembers(Runtime.TypeRefs, reflect));
                    if (info2 != null)
                    {
                        switch (info2.MemberType)
                        {
                            case MemberTypes.Property:
                                return JSProperty.GetValue((PropertyInfo) info2, value, null);

                            case MemberTypes.NestedType:
                                return info2;

                            case MemberTypes.Event:
                                return null;

                            case MemberTypes.Field:
                                return ((FieldInfo) info2).GetValue(value);

                            case MemberTypes.Method:
                                return ((MethodInfo) info2).Invoke(value, new object[0]);
                        }
                    }
                    if (value == reflect)
                    {
                        Type type = value.GetType();
                        if (TypeReflector.GetTypeReflectorFor(type).Is__ComObject() && (!VsaEngine.executeForJSEE || !(value is IDebuggerObject)))
                        {
                            reflect = type;
                        }
                    }
                    if (VsaEngine.executeForJSEE)
                    {
                        IDebuggerObject obj4 = reflect as IDebuggerObject;
                        if (obj4 != null)
                        {
                            if (!obj4.IsScriptObject())
                            {
                                throw new JScriptException(JSError.NonSupportedInDebugger);
                            }
                            return reflect.InvokeMember("< JScript-" + preferredType.ToString() + " >", BindingFlags.SuppressChangeType | BindingFlags.ExactBinding | BindingFlags.GetProperty | BindingFlags.GetField | BindingFlags.InvokeMethod, null, value, new object[0], null, null, new string[0]);
                        }
                    }
                    return reflect.InvokeMember(string.Empty, BindingFlags.SuppressChangeType | BindingFlags.ExactBinding | BindingFlags.GetProperty | BindingFlags.GetField | BindingFlags.InvokeMethod, null, value, new object[0], null, null, new string[0]);
                }
                catch (TargetInvocationException exception)
                {
                    throw exception.InnerException;
                }
            }
            catch (ArgumentException)
            {
            }
            catch (IndexOutOfRangeException)
            {
            }
            catch (MissingMemberException)
            {
            }
            catch (SecurityException)
            {
            }
            catch (TargetParameterCountException)
            {
            }
            catch (COMException exception2)
            {
                if (exception2.ErrorCode != -2147352573)
                {
                    throw exception2;
                }
            }
            if (preferredType == PreferredType.Number)
            {
                return value;
            }
            if (value.GetType().IsCOMObject)
            {
                return "ActiveXObject";
            }
            if (value is char[])
            {
                return new string((char[]) value);
            }
            return value.ToString();
        }