// Default implementation of the "ToType" methods in // the primitive classes like Byte, Int32, Boolean, etc. internal static Object DefaultToType(IConvertible obj, Type targetType, IFormatProvider provider, bool recursive) { if(targetType != null) { if(obj.GetType() == targetType) { return obj; } else if(targetType == ConvertTypes[(int)TypeCode.Boolean]) { return (Object)(obj.ToBoolean(provider)); } else if(targetType == ConvertTypes[(int)TypeCode.Char]) { return (Object)(obj.ToChar(provider)); } else if(targetType == ConvertTypes[(int)TypeCode.SByte]) { return (Object)(obj.ToSByte(provider)); } else if(targetType == ConvertTypes[(int)TypeCode.Byte]) { return (Object)(obj.ToByte(provider)); } else if(targetType == ConvertTypes[(int)TypeCode.Int16]) { return (Object)(obj.ToInt16(provider)); } else if(targetType == ConvertTypes[(int)TypeCode.UInt16]) { return (Object)(obj.ToUInt16(provider)); } else if(targetType == ConvertTypes[(int)TypeCode.Int32]) { return (Object)(obj.ToInt32(provider)); } else if(targetType == ConvertTypes[(int)TypeCode.UInt32]) { return (Object)(obj.ToUInt32(provider)); } else if(targetType == ConvertTypes[(int)TypeCode.Int64]) { return (Object)(obj.ToInt64(provider)); } else if(targetType == ConvertTypes[(int)TypeCode.UInt64]) { return (Object)(obj.ToUInt64(provider)); } else if(targetType == ConvertTypes[(int)TypeCode.Single]) { return (Object)(obj.ToSingle(provider)); } else if(targetType == ConvertTypes[(int)TypeCode.Double]) { return (Object)(obj.ToDouble(provider)); } else if(targetType == ConvertTypes[(int)TypeCode.Decimal]) { return (Object)(obj.ToDecimal(provider)); } else if(targetType == ConvertTypes[(int)TypeCode.DateTime]) { return (Object)(obj.ToDateTime(provider)); } else if(targetType == ConvertTypes[(int)TypeCode.String]) { return (Object)(obj.ToString(provider)); } else if(targetType == ConvertTypes[(int)TypeCode.Object]) { return obj; } else if(targetType == ConvertTypes[(int)TypeCode.Empty]) { throw new InvalidCastException (_("InvalidCast_Empty")); } else if(targetType == ConvertTypes[(int)TypeCode.DBNull]) { throw new InvalidCastException (_("InvalidCast_DBNull")); } else if(recursive) { throw new InvalidCastException (String.Format (_("InvalidCast_FromTo"), obj.GetType().FullName, targetType.FullName)); } else { // We weren't called from a "ToType" method, // so we can use it to handle the default case. return obj.ToType(targetType, provider); } } else { throw new ArgumentNullException("targetType"); } }
internal static bool JScriptStrictEquals(object v1, object v2, IConvertible ic1, IConvertible ic2, TypeCode t1, TypeCode t2, bool checkForDebuggerObjects) { long num7; switch (t1) { case TypeCode.Empty: return (t2 == TypeCode.Empty); case TypeCode.Object: if (v1 != v2) { if ((v1 is Microsoft.JScript.Missing) || (v1 is System.Reflection.Missing)) { v1 = null; } if (v1 == v2) { return true; } if ((v2 is Microsoft.JScript.Missing) || (v2 is System.Reflection.Missing)) { v2 = null; } if (checkForDebuggerObjects) { IDebuggerObject obj2 = v1 as IDebuggerObject; if (obj2 != null) { IDebuggerObject o = v2 as IDebuggerObject; if (o != null) { return obj2.IsEqual(o); } } } return (v1 == v2); } return true; case TypeCode.DBNull: return (t2 == TypeCode.DBNull); case TypeCode.Boolean: if (t2 != TypeCode.Boolean) { return false; } return (ic1.ToBoolean(null) == ic2.ToBoolean(null)); case TypeCode.Char: { char ch = ic1.ToChar(null); switch (t2) { case TypeCode.Char: return (ch == ic2.ToChar(null)); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return (ch == ic2.ToInt64(null)); case TypeCode.UInt64: return (ch == ic2.ToUInt64(null)); case TypeCode.Single: case TypeCode.Double: return (((double) ch) == ic2.ToDouble(null)); case TypeCode.Decimal: return (ch == ic2.ToDecimal(null)); case TypeCode.String: { string str = ic2.ToString(null); return ((str.Length == 1) && (ch == str[0])); } } break; } case TypeCode.SByte: { sbyte num = ic1.ToSByte(null); switch (t2) { case TypeCode.Char: return (num == ic2.ToChar(null)); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return (num == ic2.ToInt64(null)); case TypeCode.UInt64: return ((num >= 0) && (num == ic2.ToUInt64(null))); case TypeCode.Single: return (num == ic2.ToSingle(null)); case TypeCode.Double: return (num == ic2.ToDouble(null)); case TypeCode.Decimal: return (num == ic2.ToDecimal(null)); } return false; } case TypeCode.Byte: { byte num2 = ic1.ToByte(null); switch (t2) { case TypeCode.Char: return (num2 == ic2.ToChar(null)); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return (num2 == ic2.ToInt64(null)); case TypeCode.UInt64: return (num2 == ic2.ToUInt64(null)); case TypeCode.Single: return (num2 == ic2.ToSingle(null)); case TypeCode.Double: return (num2 == ic2.ToDouble(null)); case TypeCode.Decimal: return (num2 == ic2.ToDecimal(null)); } return false; } case TypeCode.Int16: { short num3 = ic1.ToInt16(null); switch (t2) { case TypeCode.Char: return (num3 == ic2.ToChar(null)); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return (num3 == ic2.ToInt64(null)); case TypeCode.UInt64: return ((num3 >= 0) && (num3 == ic2.ToUInt64(null))); case TypeCode.Single: return (num3 == ic2.ToSingle(null)); case TypeCode.Double: return (num3 == ic2.ToDouble(null)); case TypeCode.Decimal: return (num3 == ic2.ToDecimal(null)); } return false; } case TypeCode.UInt16: { ushort num4 = ic1.ToUInt16(null); switch (t2) { case TypeCode.Char: return (num4 == ic2.ToChar(null)); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return (num4 == ic2.ToInt64(null)); case TypeCode.UInt64: return (num4 == ic2.ToUInt64(null)); case TypeCode.Single: return (num4 == ic2.ToSingle(null)); case TypeCode.Double: return (num4 == ic2.ToDouble(null)); case TypeCode.Decimal: return (num4 == ic2.ToDecimal(null)); } return false; } case TypeCode.Int32: { int num5 = ic1.ToInt32(null); switch (t2) { case TypeCode.Char: return (num5 == ic2.ToChar(null)); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return (num5 == ic2.ToInt64(null)); case TypeCode.UInt64: return ((num5 >= 0) && (num5 == ic2.ToUInt64(null))); case TypeCode.Single: return (num5 == ic2.ToSingle(null)); case TypeCode.Double: return (num5 == ic2.ToDouble(null)); case TypeCode.Decimal: return (num5 == ic2.ToDecimal(null)); } return false; } case TypeCode.UInt32: { uint num6 = ic1.ToUInt32(null); switch (t2) { case TypeCode.Char: return (num6 == ic2.ToChar(null)); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return (num6 == ic2.ToInt64(null)); case TypeCode.UInt64: return (num6 == ic2.ToUInt64(null)); case TypeCode.Single: return (num6 == ic2.ToSingle(null)); case TypeCode.Double: return (num6 == ic2.ToDouble(null)); case TypeCode.Decimal: return (num6 == ic2.ToDecimal(null)); } return false; } case TypeCode.Int64: num7 = ic1.ToInt64(null); switch (t2) { case TypeCode.Char: return (num7 == ic2.ToChar(null)); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return (num7 == ic2.ToInt64(null)); case TypeCode.UInt64: return ((num7 >= 0L) && (num7 == ic2.ToUInt64(null))); case TypeCode.Single: return (num7 == ic2.ToSingle(null)); case TypeCode.Double: return (num7 == ic2.ToDouble(null)); case TypeCode.Decimal: return (num7 == ic2.ToDecimal(null)); } return false; case TypeCode.UInt64: { ulong num8 = ic1.ToUInt64(null); switch (t2) { case TypeCode.Char: return (num8 == ic2.ToChar(null)); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: num7 = ic2.ToInt64(null); return ((num7 >= 0L) && (num8 == num7)); case TypeCode.UInt64: return (num8 == ic2.ToUInt64(null)); case TypeCode.Single: return (num8 == ic2.ToSingle(null)); case TypeCode.Double: return (num8 == ic2.ToDouble(null)); case TypeCode.Decimal: return (num8 == ic2.ToDecimal(null)); } return false; } case TypeCode.Single: { float num9 = ic1.ToSingle(null); switch (t2) { case TypeCode.Char: return (num9 == ((float) ic2.ToChar(null))); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return (num9 == ic2.ToInt64(null)); case TypeCode.UInt64: return (num9 == ic2.ToUInt64(null)); case TypeCode.Single: return (num9 == ic2.ToSingle(null)); case TypeCode.Double: return (num9 == ic2.ToSingle(null)); case TypeCode.Decimal: return (((decimal) num9) == ic2.ToDecimal(null)); } return false; } case TypeCode.Double: { double num10 = ic1.ToDouble(null); switch (t2) { case TypeCode.Char: return (num10 == ((double) ic2.ToChar(null))); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return (num10 == ic2.ToInt64(null)); case TypeCode.UInt64: return (num10 == ic2.ToUInt64(null)); case TypeCode.Single: return (((float) num10) == ic2.ToSingle(null)); case TypeCode.Double: return (num10 == ic2.ToDouble(null)); case TypeCode.Decimal: return (((decimal) num10) == ic2.ToDecimal(null)); } return false; } case TypeCode.Decimal: { decimal num11 = ic1.ToDecimal(null); switch (t2) { case TypeCode.Char: return (num11 == ic2.ToChar(null)); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return (num11 == ic2.ToInt64(null)); case TypeCode.UInt64: return (num11 == ic2.ToUInt64(null)); case TypeCode.Single: return (num11 == ((decimal) ic2.ToSingle(null))); case TypeCode.Double: return (num11 == ((decimal) ic2.ToDouble(null))); case TypeCode.Decimal: return (num11 == ic2.ToDecimal(null)); } return false; } case TypeCode.DateTime: if (t2 != TypeCode.DateTime) { return false; } return (ic1.ToDateTime(null) == ic2.ToDateTime(null)); case TypeCode.String: { if (t2 != TypeCode.Char) { if (t2 != TypeCode.String) { return false; } if (v1 != v2) { return ic1.ToString(null).Equals(ic2.ToString(null)); } return true; } string str2 = ic1.ToString(null); if (str2.Length != 1) { return false; } return (str2[0] == ic2.ToChar(null)); } default: return false; } return false; }
/// <summary> 将 数字 类型对象转换Json字符串写入Buffer /// </summary> /// <param name="number">数字对象</param> protected virtual void AppendNumber(IConvertible number) { switch (number.GetTypeCode()) { case TypeCode.Decimal: case TypeCode.Double: case TypeCode.Single: if (QuotWrapNumber) { Buffer.Append('"'); Buffer.Append(number.ToString(CultureInfo.InvariantCulture)); Buffer.Append('"'); } else { Buffer.Append(number.ToString(CultureInfo.InvariantCulture)); } break; case TypeCode.Int16: if (QuotWrapNumber) { Buffer.Append('"'); Buffer.Append(number.ToInt16(CultureInfo.InvariantCulture)); Buffer.Append('"'); } else { Buffer.Append(number.ToInt16(CultureInfo.InvariantCulture)); } break; case TypeCode.Int32: case TypeCode.Int64: if (QuotWrapNumber) { Buffer.Append('"'); Buffer.Append(number.ToInt64(CultureInfo.InvariantCulture)); Buffer.Append('"'); } else { Buffer.Append(number.ToInt64(CultureInfo.InvariantCulture)); } break; case TypeCode.SByte: if (QuotWrapNumber) { Buffer.Append('"'); Buffer.Append(number.ToSByte(CultureInfo.InvariantCulture)); Buffer.Append('"'); } else { Buffer.Append(number.ToSByte(CultureInfo.InvariantCulture)); } break; case TypeCode.Byte: if (QuotWrapNumber) { Buffer.Append('"'); Buffer.Append(number.ToByte(CultureInfo.InvariantCulture)); Buffer.Append('"'); } else { Buffer.Append(number.ToByte(CultureInfo.InvariantCulture)); } break; case TypeCode.UInt16: if (QuotWrapNumber) { Buffer.Append('"'); Buffer.Append(number.ToUInt16(CultureInfo.InvariantCulture)); Buffer.Append('"'); } else { Buffer.Append(number.ToUInt16(CultureInfo.InvariantCulture)); } break; case TypeCode.UInt32: case TypeCode.UInt64: if (QuotWrapNumber) { Buffer.Append('"'); Buffer.Append(number.ToUInt64(CultureInfo.InvariantCulture)); Buffer.Append('"'); } else { Buffer.Append(number.ToUInt64(CultureInfo.InvariantCulture)); } break; default: break; } }
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 }
sbyte IConvertible.ToSByte(IFormatProvider provider) { IConvertible conv = (IConvertible)value; return(conv.ToSByte(provider)); }
/// <summary> /// Converts the <see cref="Object"/> to its JSON string representation. /// </summary> /// <param name="value">The value to convert.</param> /// <returns>A JSON string representation of the <see cref="Object"/>.</returns> public static string ToString(object value) { if (value == null) { return(Null); } IConvertible convertible = value as IConvertible; if (convertible != null) { switch (convertible.GetTypeCode()) { case TypeCode.String: return(ToString(convertible.ToString(CultureInfo.InvariantCulture))); case TypeCode.Char: return(ToString(convertible.ToChar(CultureInfo.InvariantCulture))); case TypeCode.Boolean: return(ToString(convertible.ToBoolean(CultureInfo.InvariantCulture))); case TypeCode.SByte: return(ToString(convertible.ToSByte(CultureInfo.InvariantCulture))); case TypeCode.Int16: return(ToString(convertible.ToInt16(CultureInfo.InvariantCulture))); case TypeCode.UInt16: return(ToString(convertible.ToUInt16(CultureInfo.InvariantCulture))); case TypeCode.Int32: return(ToString(convertible.ToInt32(CultureInfo.InvariantCulture))); case TypeCode.Byte: return(ToString(convertible.ToByte(CultureInfo.InvariantCulture))); case TypeCode.UInt32: return(ToString(convertible.ToUInt32(CultureInfo.InvariantCulture))); case TypeCode.Int64: return(ToString(convertible.ToInt64(CultureInfo.InvariantCulture))); case TypeCode.UInt64: return(ToString(convertible.ToUInt64(CultureInfo.InvariantCulture))); case TypeCode.Single: return(ToString(convertible.ToSingle(CultureInfo.InvariantCulture))); case TypeCode.Double: return(ToString(convertible.ToDouble(CultureInfo.InvariantCulture))); case TypeCode.DateTime: return(ToString(convertible.ToDateTime(CultureInfo.InvariantCulture))); case TypeCode.Decimal: return(ToString(convertible.ToDecimal(CultureInfo.InvariantCulture))); case TypeCode.DBNull: return(Null); } } else if (value is DateTimeOffset) { return(ToString((DateTimeOffset)value)); } else if (value is Guid) { return(ToString((Guid)value)); } else if (value is Uri) { return(ToString((Uri)value)); } else if (value is TimeSpan) { return(ToString((TimeSpan)value)); } throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType())); }
// 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 = 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(SR.Format(SR.NotSupported_UnknownTypeCode, ic.GetTypeCode())); } } }
[System.Security.SecuritySafeCritical] // auto-generated internal static void MarshalHelperConvertObjectToVariant(Object o, ref Variant v) { #if FEATURE_REMOTING IConvertible ic = System.Runtime.Remoting.RemotingServices.IsTransparentProxy(o) ? null : o as IConvertible; #else IConvertible ic = o as IConvertible; #endif if (o == null) { v = Empty; } else if (ic == null) { // This path should eventually go away. But until // the work is done to have all of our wrapper types implement // IConvertible, this is a cheapo way to get the work done. v = new Variant(o); } else { IFormatProvider provider = CultureInfo.InvariantCulture; switch (ic.GetTypeCode()) { case TypeCode.Empty: v = Empty; break; case TypeCode.Object: v = new Variant((Object)o); break; case TypeCode.DBNull: v = DBNull; break; case TypeCode.Boolean: v = new Variant(ic.ToBoolean(provider)); break; case TypeCode.Char: v = new Variant(ic.ToChar(provider)); break; case TypeCode.SByte: v = new Variant(ic.ToSByte(provider)); break; case TypeCode.Byte: v = new Variant(ic.ToByte(provider)); break; case TypeCode.Int16: v = new Variant(ic.ToInt16(provider)); break; case TypeCode.UInt16: v = new Variant(ic.ToUInt16(provider)); break; case TypeCode.Int32: v = new Variant(ic.ToInt32(provider)); break; case TypeCode.UInt32: v = new Variant(ic.ToUInt32(provider)); break; case TypeCode.Int64: v = new Variant(ic.ToInt64(provider)); break; case TypeCode.UInt64: v = new Variant(ic.ToUInt64(provider)); break; case TypeCode.Single: v = new Variant(ic.ToSingle(provider)); break; case TypeCode.Double: v = new Variant(ic.ToDouble(provider)); break; case TypeCode.Decimal: v = new Variant(ic.ToDecimal(provider)); break; case TypeCode.DateTime: v = new Variant(ic.ToDateTime(provider)); break; case TypeCode.String: v = new Variant(ic.ToString(provider)); break; default: throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnknownTypeCode", ic.GetTypeCode())); } } }
public override sbyte ToSByte(IFormatProvider provider) { return(m_Value.ToSByte(provider)); }
/// <summary> /// Append a numeric type (byte, int, double, decimal) as string /// </summary> internal static void AppendNumericInvariant(this StringBuilder sb, IConvertible value, TypeCode objTypeCode) { switch (objTypeCode) { case TypeCode.Byte: sb.AppendInvariant(value.ToByte(CultureInfo.InvariantCulture)); break; case TypeCode.SByte: sb.AppendInvariant(value.ToSByte(CultureInfo.InvariantCulture)); break; case TypeCode.Int16: sb.AppendInvariant(value.ToInt16(CultureInfo.InvariantCulture)); break; case TypeCode.Int32: sb.AppendInvariant(value.ToInt32(CultureInfo.InvariantCulture)); break; case TypeCode.Int64: { long int64 = value.ToInt64(CultureInfo.InvariantCulture); if (int64 < int.MaxValue && int64 > int.MinValue) { sb.AppendInvariant((int)int64); } else { sb.Append(int64); } } break; case TypeCode.UInt16: sb.AppendInvariant(value.ToUInt16(CultureInfo.InvariantCulture)); break; case TypeCode.UInt32: sb.AppendInvariant(value.ToUInt32(CultureInfo.InvariantCulture)); break; case TypeCode.UInt64: { ulong uint64 = value.ToUInt64(CultureInfo.InvariantCulture); if (uint64 < uint.MaxValue) { sb.AppendInvariant((uint)uint64); } else { sb.Append(uint64); } } break; case TypeCode.Single: { float floatValue = value.ToSingle(CultureInfo.InvariantCulture); AppendFloatInvariant(sb, floatValue); } break; case TypeCode.Double: { double doubleValue = value.ToDouble(CultureInfo.InvariantCulture); AppendDoubleInvariant(sb, doubleValue); } break; case TypeCode.Decimal: { decimal decimalValue = value.ToDecimal(CultureInfo.InvariantCulture); AppendDecimalInvariant(sb, decimalValue); } break; default: sb.Append(XmlHelper.XmlConvertToString(value, objTypeCode)); break; } }
public static byte[] ToBytes(this IConvertible value, TypeCode typeCode) { if (typeCode == TypeCode.Boolean) { return(BitConverter.GetBytes(value.ToBoolean(null))); } if (typeCode == TypeCode.Char) { var b = Convert.ToByte(value.ToChar(null)); return(new[] { b }); } if (typeCode == TypeCode.SByte) { var b = Convert.ToByte(value.ToSByte(null)); return(new[] { b }); } if (typeCode == TypeCode.Byte) { var b = Convert.ToByte(value.ToByte(null)); return(new[] { b }); } if (typeCode == TypeCode.Int16) { return(BitConverter.GetBytes(value.ToInt16(null))); } if (typeCode == TypeCode.UInt16) { return(BitConverter.GetBytes(value.ToUInt16(null))); } if (typeCode == TypeCode.Int32) { return(BitConverter.GetBytes(value.ToInt32(null))); } if (typeCode == TypeCode.UInt32) { return(BitConverter.GetBytes(value.ToUInt32(null))); } if (typeCode == TypeCode.Int64) { return(BitConverter.GetBytes(value.ToInt64(null))); } if (typeCode == TypeCode.UInt64) { return(BitConverter.GetBytes(value.ToUInt64(null))); } if (typeCode == TypeCode.Single) { return(BitConverter.GetBytes(value.ToSingle(null))); } if (typeCode == TypeCode.Double) { return(BitConverter.GetBytes(value.ToDouble(null))); } if (typeCode == TypeCode.String) { return(((string)value).ToBytes()); } throw new NotSupportedException("TypeCode:" + typeCode); }
public virtual void WriteValue(object value) { if (value == null) { this.WriteNull(); return; } if (value is IConvertible) { IConvertible convertible = value as IConvertible; switch (convertible.GetTypeCode()) { case 2: this.WriteNull(); return; case 3: this.WriteValue(convertible.ToBoolean(CultureInfo.get_InvariantCulture())); return; case 4: this.WriteValue(convertible.ToChar(CultureInfo.get_InvariantCulture())); return; case 5: this.WriteValue(convertible.ToSByte(CultureInfo.get_InvariantCulture())); return; case 6: this.WriteValue(convertible.ToByte(CultureInfo.get_InvariantCulture())); return; case 7: this.WriteValue(convertible.ToInt16(CultureInfo.get_InvariantCulture())); return; case 8: this.WriteValue(convertible.ToUInt16(CultureInfo.get_InvariantCulture())); return; case 9: this.WriteValue(convertible.ToInt32(CultureInfo.get_InvariantCulture())); return; case 10: this.WriteValue(convertible.ToUInt32(CultureInfo.get_InvariantCulture())); return; case 11: this.WriteValue(convertible.ToInt64(CultureInfo.get_InvariantCulture())); return; case 12: this.WriteValue(convertible.ToUInt64(CultureInfo.get_InvariantCulture())); return; case 13: this.WriteValue(convertible.ToSingle(CultureInfo.get_InvariantCulture())); return; case 14: this.WriteValue(convertible.ToDouble(CultureInfo.get_InvariantCulture())); return; case 15: this.WriteValue(convertible.ToDecimal(CultureInfo.get_InvariantCulture())); return; case 16: this.WriteValue(convertible.ToDateTime(CultureInfo.get_InvariantCulture())); return; case 18: this.WriteValue(convertible.ToString(CultureInfo.get_InvariantCulture())); return; } } else { if (value is DateTimeOffset) { this.WriteValue((DateTimeOffset)value); return; } if (value is byte[]) { this.WriteValue((byte[])value); return; } if (value is Guid) { this.WriteValue((Guid)value); return; } if (value is Uri) { this.WriteValue((Uri)value); return; } if (value is TimeSpan) { this.WriteValue((TimeSpan)value); return; } } 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() })); }
/// <summary> /// Converts the value of this instance to an equivalent 8-bit signed integer using the specified culture-specific formatting information. /// </summary> /// <param name="provider">An <see cref="T:System.IFormatProvider"/> interface implementation that supplies culture-specific formatting information.</param> /// <returns> /// An 8-bit signed integer equivalent to the value of this instance. /// </returns> /// <filterPriority>2</filterPriority> sbyte IConvertible.ToSByte(IFormatProvider provider) { return(_convertible.ToSByte(provider)); }
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 })); }
// Function to convert an object to another type and return // it as an object. In place for the core data types to use // when implementing IConvertible. Uses hardcoded indexes in // the conversionTypes array, so if modify carefully. // // The `try_target_to_type' boolean indicates if the code // should try to call the IConvertible.ToType method if everything // else fails. // // This should be true for invocations from Convert.cs, and // false from the mscorlib types that implement IConvertible that // all into this internal function. // // This was added to keep the fix for #481687 working and to avoid // the regression that the simple fix introduced (485377) internal static object ToType(object value, Type conversionType, IFormatProvider provider, bool try_target_to_type) { if (value == null) { if ((conversionType != null) && conversionType.IsValueType) { throw new InvalidCastException("Null object can not be converted to a value type."); } else { return(null); } } if (conversionType == null) { throw new InvalidCastException("Cannot cast to destination type."); } if (value.GetType() == conversionType) { return(value); } IConvertible convertValue = value as IConvertible; if (convertValue != null) { if (conversionType == conversionTable[0]) // 0 Empty { throw new ArgumentNullException(); } if (conversionType == conversionTable[1]) // 1 TypeCode.Object { return(value); } if (conversionType == conversionTable[2]) // 2 TypeCode.DBNull { throw new InvalidCastException( "Cannot cast to DBNull, it's not IConvertible"); } if (conversionType == conversionTable[3]) // 3 TypeCode.Boolean { return(convertValue.ToBoolean(provider)); } if (conversionType == conversionTable[4]) // 4 TypeCode.Char { return(convertValue.ToChar(provider)); } if (conversionType == conversionTable[5]) // 5 TypeCode.SByte { return(convertValue.ToSByte(provider)); } if (conversionType == conversionTable[6]) // 6 TypeCode.Byte { return(convertValue.ToByte(provider)); } if (conversionType == conversionTable[7]) // 7 TypeCode.Int16 { return(convertValue.ToInt16(provider)); } if (conversionType == conversionTable[8]) // 8 TypeCode.UInt16 { return(convertValue.ToUInt16(provider)); } if (conversionType == conversionTable[9]) // 9 TypeCode.Int32 { return(convertValue.ToInt32(provider)); } if (conversionType == conversionTable[10]) // 10 TypeCode.UInt32 { return(convertValue.ToUInt32(provider)); } if (conversionType == conversionTable[11]) // 11 TypeCode.Int64 { return(convertValue.ToInt64(provider)); } if (conversionType == conversionTable[12]) // 12 TypeCode.UInt64 { return(convertValue.ToUInt64(provider)); } if (conversionType == conversionTable[13]) // 13 TypeCode.Single { return(convertValue.ToSingle(provider)); } if (conversionType == conversionTable[14]) // 14 TypeCode.Double { return(convertValue.ToDouble(provider)); } if (conversionType == conversionTable[15]) // 15 TypeCode.Decimal { return(convertValue.ToDecimal(provider)); } if (conversionType == conversionTable[16]) // 16 TypeCode.DateTime { return(convertValue.ToDateTime(provider)); } if (conversionType == conversionTable[18]) // 18 TypeCode.String { return(convertValue.ToString(provider)); } if (conversionType == conversionTable[19] && value is Enum) // System.Enum { return(value); } if (try_target_to_type) { return(convertValue.ToType(conversionType, provider)); } } // Not in the conversion table throw new InvalidCastException( "Value is not a convertible object: " + value.GetType().ToString() + " to " + conversionType.FullName ); }
/// <summary> 数字 类型对象转换Json字符串写入Buffer /// </summary> /// <param name="number">数字对象</param> protected virtual void AppendNumber(IConvertible number) { switch (number.GetTypeCode()) { case TypeCode.Decimal: case TypeCode.Double: case TypeCode.Single: Buffer.Append(number.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)); break; case TypeCode.Int16: Buffer.Append(number.ToInt16(System.Globalization.NumberFormatInfo.InvariantInfo)); break; case TypeCode.Int32: case TypeCode.Int64: Buffer.Append(number.ToInt64(System.Globalization.NumberFormatInfo.InvariantInfo)); break; case TypeCode.SByte: Buffer.Append(number.ToSByte(System.Globalization.NumberFormatInfo.InvariantInfo)); break; case TypeCode.Byte: Buffer.Append(number.ToByte(System.Globalization.NumberFormatInfo.InvariantInfo)); break; case TypeCode.UInt16: Buffer.Append(number.ToUInt16(System.Globalization.NumberFormatInfo.InvariantInfo)); break; case TypeCode.UInt32: case TypeCode.UInt64: Buffer.Append(number.ToUInt64(System.Globalization.NumberFormatInfo.InvariantInfo)); break; default: break; } }
/// <summary> /// Unserializes the specified string value in the specified type. /// </summary> /// <param name="value">The value.</param> /// <param name="type">The type.</param> /// <returns></returns> internal static object Unserialize(string value, Type type) { if (value == Default) { return(null); } if (type.IsEnum) { return(Enum.Parse(type, value)); } IConvertible convertible = value as IConvertible; if (type == typeof(bool) || type == typeof(bool?)) { return(String.IsNullOrWhiteSpace(value) ? (bool?)null : convertible.ToBoolean(CultureInfo.CurrentCulture)); } if (type == typeof(byte) || type == typeof(byte?)) { return(String.IsNullOrWhiteSpace(value) ? (byte?)null : convertible.ToByte(CultureInfo.CurrentCulture)); } if (type == typeof(char) || type == typeof(char?)) { return(String.IsNullOrWhiteSpace(value) ? (char?)null : convertible.ToChar(CultureInfo.CurrentCulture)); } if (type == typeof(DateTime) || type == typeof(DateTime?)) { return(String.IsNullOrWhiteSpace(value) ? (DateTime?)null : convertible.ToDateTime(CultureInfo.CurrentCulture)); } if (type == typeof(decimal) || type == typeof(decimal?)) { return(String.IsNullOrWhiteSpace(value) ? (decimal?)null : convertible.ToDecimal(CultureInfo.CurrentCulture)); } if (type == typeof(double) || type == typeof(double?)) { return(String.IsNullOrWhiteSpace(value) ? (double?)null : convertible.ToDouble(CultureInfo.CurrentCulture)); } if (type == typeof(short) || type == typeof(short?)) { return(String.IsNullOrWhiteSpace(value) ? (short?)null : convertible.ToInt16(CultureInfo.CurrentCulture)); } if (type == typeof(int) || type == typeof(int?)) { return(String.IsNullOrWhiteSpace(value) ? (int?)null : convertible.ToInt32(CultureInfo.CurrentCulture)); } if (type == typeof(long) || type == typeof(long?)) { return(String.IsNullOrWhiteSpace(value) ? (long?)null : convertible.ToInt64(CultureInfo.CurrentCulture)); } if (type == typeof(sbyte) || type == typeof(sbyte?)) { return(String.IsNullOrWhiteSpace(value) ? (sbyte?)null : convertible.ToSByte(CultureInfo.CurrentCulture)); } if (type == typeof(float) || type == typeof(float?)) { return(String.IsNullOrWhiteSpace(value) ? (float?)null : convertible.ToSingle(CultureInfo.CurrentCulture)); } if (type == typeof(string)) { return(value.ToString()); } if (type == typeof(ushort) || type == typeof(ushort?)) { return(String.IsNullOrWhiteSpace(value) ? (ushort?)null : convertible.ToUInt16(CultureInfo.CurrentCulture)); } if (type == typeof(uint) || type == typeof(uint?)) { return(String.IsNullOrWhiteSpace(value) ? (uint?)null : convertible.ToUInt32(CultureInfo.CurrentCulture)); } if (type == typeof(ulong) || type == typeof(ulong?)) { return(String.IsNullOrWhiteSpace(value) ? (ulong?)null : convertible.ToUInt64(CultureInfo.CurrentCulture)); } var c = TypeDescriptor.GetConverter(type); string result = c.ConvertTo(value, typeof(string)) as string; return(result); }
[System.Security.SecurityCritical] // auto-generated internal static void MarshalHelperCastVariant(Object pValue, int vt, ref Variant v) { IConvertible iv = pValue as IConvertible; if (iv == null) { switch (vt) { case 9: /*VT_DISPATCH*/ v = new Variant(new DispatchWrapper(pValue)); break; case 12: /*VT_VARIANT*/ v = new Variant(pValue); break; case 13: /*VT_UNKNOWN*/ v = new Variant(new UnknownWrapper(pValue)); break; case 36: /*VT_RECORD*/ v = new Variant(pValue); break; case 8: /*VT_BSTR*/ if (pValue == null) { v = new Variant(null); v.m_flags = CV_STRING; } else { throw new InvalidCastException(Environment.GetResourceString("InvalidCast_CannotCoerceByRefVariant")); } break; default: throw new InvalidCastException(Environment.GetResourceString("InvalidCast_CannotCoerceByRefVariant")); } } else { IFormatProvider provider = CultureInfo.InvariantCulture; switch (vt) { case 0: /*VT_EMPTY*/ v = Empty; break; case 1: /*VT_NULL*/ v = DBNull; break; case 2: /*VT_I2*/ v = new Variant(iv.ToInt16(provider)); break; case 3: /*VT_I4*/ v = new Variant(iv.ToInt32(provider)); break; case 4: /*VT_R4*/ v = new Variant(iv.ToSingle(provider)); break; case 5: /*VT_R8*/ v = new Variant(iv.ToDouble(provider)); break; case 6: /*VT_CY*/ v = new Variant(new CurrencyWrapper(iv.ToDecimal(provider))); break; case 7: /*VT_DATE*/ v = new Variant(iv.ToDateTime(provider)); break; case 8: /*VT_BSTR*/ v = new Variant(iv.ToString(provider)); break; case 9: /*VT_DISPATCH*/ v = new Variant(new DispatchWrapper((Object)iv)); break; case 10: /*VT_ERROR*/ v = new Variant(new ErrorWrapper(iv.ToInt32(provider))); break; case 11: /*VT_BOOL*/ v = new Variant(iv.ToBoolean(provider)); break; case 12: /*VT_VARIANT*/ v = new Variant((Object)iv); break; case 13: /*VT_UNKNOWN*/ v = new Variant(new UnknownWrapper((Object)iv)); break; case 14: /*VT_DECIMAL*/ v = new Variant(iv.ToDecimal(provider)); break; // case 15: /*unused*/ // NOT SUPPORTED case 16: /*VT_I1*/ v = new Variant(iv.ToSByte(provider)); break; case 17: /*VT_UI1*/ v = new Variant(iv.ToByte(provider)); break; case 18: /*VT_UI2*/ v = new Variant(iv.ToUInt16(provider)); break; case 19: /*VT_UI4*/ v = new Variant(iv.ToUInt32(provider)); break; case 20: /*VT_I8*/ v = new Variant(iv.ToInt64(provider)); break; case 21: /*VT_UI8*/ v = new Variant(iv.ToUInt64(provider)); break; case 22: /*VT_INT*/ v = new Variant(iv.ToInt32(provider)); break; case 23: /*VT_UINT*/ v = new Variant(iv.ToUInt32(provider)); break; default: throw new InvalidCastException(Environment.GetResourceString("InvalidCast_CannotCoerceByRefVariant")); } } }
/// <summary> /// 转换数据类型 /// </summary> /// <param name="value"></param> /// <param name="conversionType"></param> /// <param name="provider"></param> /// <returns></returns> public static object ChangeValueType(object value, Type conversionType, IFormatProvider provider) { if (conversionType == null) { throw new ArgumentNullException("conversionType"); } bool valueCanbeNull = valueNaullable(conversionType); if (valueCanbeNull && (value == null || value.ToString().Length == 0))//如果Nullable<>类型,且值是空,则直接返回空 { return(null); } if (value == null) { if (conversionType.IsValueType) { throw new InvalidCastException(string.Format("值为空。")); } return(null); } IConvertible convertible = value as IConvertible; if (convertible == null) { if (value.GetType() != conversionType) { throw new InvalidCastException(string.Format("值不能被转换。")); } 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); }
public static string ToString(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(ToString(convertible.ToBoolean(null))); case TypeCode.Char: return(ToString(convertible.ToChar(null))); case TypeCode.SByte: return(ToString((int)convertible.ToSByte(null))); case TypeCode.Byte: return(ToString(convertible.ToByte(null))); case TypeCode.Int16: return(ToString((int)convertible.ToInt16(null))); case TypeCode.UInt16: return(ToString((uint)convertible.ToUInt16(null))); case TypeCode.Int32: return(ToString(convertible.ToInt32(null))); case TypeCode.UInt32: return(ToString(convertible.ToUInt32(null))); case TypeCode.Int64: return(ToString(convertible.ToInt64(null))); case TypeCode.UInt64: return(ToString(convertible.ToUInt64(null))); case TypeCode.Single: return(ToString(convertible.ToSingle(null))); case TypeCode.Double: return(ToString(convertible.ToDouble(null))); case TypeCode.Decimal: return(ToString(convertible.ToDecimal(null))); case TypeCode.DateTime: return(ToString(convertible.ToDateTime(null))); case (TypeCode.DateTime | TypeCode.Object): return(null); case TypeCode.String: return(convertible.ToString(null)); } } else { char[] chArray = value as char[]; if (chArray != null) { return(new string(chArray)); } } return(convertible.ToString()); }
protected void EncodeConvertible(IConvertible value, Stream output) { output.WriteByte((byte)value.GetTypeCode()); byte[] result; switch (value.GetTypeCode()) { // the following encode directly on the stream case TypeCode.Boolean: output.WriteByte((byte)((bool)value ? 1 : 0)); return; case TypeCode.Byte: output.WriteByte(value.ToByte(null)); return; case TypeCode.SByte: output.WriteByte((byte)(value.ToSByte(null) + 128)); return; case TypeCode.Object: formatter.Serialize(output, value); return; case TypeCode.String: { long lengthPosition = output.Position; output.Write(new byte[4], 0, 4); StreamWriter w = new StreamWriter(output, Encoding.UTF8); w.Write((string)value); w.Flush(); long savedPosition = output.Position; uint payloadLength = (uint)(output.Position - lengthPosition - 4); output.Position = lengthPosition; output.Write(DataConverter.Converter.GetBytes(payloadLength), 0, 4); output.Position = savedPosition; return; } // the following obtain byte arrays which are dumped below case TypeCode.Char: result = DataConverter.Converter.GetBytes(value.ToChar(null)); break; case TypeCode.Single: result = DataConverter.Converter.GetBytes(value.ToSingle(null)); break; case TypeCode.Double: result = DataConverter.Converter.GetBytes(value.ToDouble(null)); break; case TypeCode.Int16: result = DataConverter.Converter.GetBytes(value.ToInt16(null)); break; case TypeCode.Int32: result = DataConverter.Converter.GetBytes(value.ToInt32(null)); break; case TypeCode.Int64: result = DataConverter.Converter.GetBytes(value.ToInt64(null)); break; case TypeCode.UInt16: result = DataConverter.Converter.GetBytes(value.ToUInt16(null)); break; case TypeCode.UInt32: result = DataConverter.Converter.GetBytes(value.ToUInt32(null)); break; case TypeCode.UInt64: result = DataConverter.Converter.GetBytes(value.ToUInt64(null)); break; case TypeCode.DateTime: result = DataConverter.Converter.GetBytes(((DateTime)value).ToBinary()); break; default: throw new MarshallingException("Unhandled form of IConvertible: " + value.GetTypeCode()); } output.Write(result, 0, result.Length); }
/// <summary> /// Converts object value to invariant format (understood by JavaScript) /// </summary> /// <param name="value">Object value</param> /// <param name="objTypeCode">Object TypeCode</param> /// <param name="safeConversion">Check and remove unusual unicode characters from the result string.</param> /// <returns>Object value converted to string</returns> internal static string XmlConvertToString(IConvertible value, TypeCode objTypeCode, bool safeConversion = false) { if (value == null) { return("null"); } switch (objTypeCode) { case TypeCode.Boolean: return(XmlConvert.ToString(value.ToBoolean(CultureInfo.InvariantCulture))); // boolean as lowercase case TypeCode.Byte: return(XmlConvert.ToString(value.ToByte(CultureInfo.InvariantCulture))); case TypeCode.SByte: return(XmlConvert.ToString(value.ToSByte(CultureInfo.InvariantCulture))); case TypeCode.Int16: return(XmlConvert.ToString(value.ToInt16(CultureInfo.InvariantCulture))); case TypeCode.Int32: return(XmlConvert.ToString(value.ToInt32(CultureInfo.InvariantCulture))); case TypeCode.Int64: return(XmlConvert.ToString(value.ToInt64(CultureInfo.InvariantCulture))); case TypeCode.UInt16: return(XmlConvert.ToString(value.ToUInt16(CultureInfo.InvariantCulture))); case TypeCode.UInt32: return(XmlConvert.ToString(value.ToUInt32(CultureInfo.InvariantCulture))); case TypeCode.UInt64: return(XmlConvert.ToString(value.ToUInt64(CultureInfo.InvariantCulture))); case TypeCode.Single: { 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)); } }
public virtual void WriteValue(object value) { if (value == null) { WriteNull(); return; } 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; } } else { if (value is DateTimeOffset) { WriteValue((DateTimeOffset)value); return; } if (value is byte[]) { WriteValue((byte[])value); return; } if (value is Guid) { WriteValue((Guid)value); return; } if (value is Uri) { WriteValue((Uri)value); return; } if (value is TimeSpan) { WriteValue((TimeSpan)value); return; } } throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType())); }
private void AppendConvertible(IConvertible obj) { var @enum = obj as Enum; if (@enum != null) { AppendEnum(@enum); return; } if (obj is IToJson) { AppendCheckLoopRef(obj); return; } switch (obj.GetTypeCode()) { case TypeCode.Boolean: AppendBoolean(obj.ToBoolean(CultureInfo.InvariantCulture)); break; case TypeCode.Byte: AppendByte(obj.ToByte(CultureInfo.InvariantCulture)); break; case TypeCode.Char: AppendChar(obj.ToChar(CultureInfo.InvariantCulture)); break; case TypeCode.DateTime: AppendDateTime(obj.ToDateTime(CultureInfo.InvariantCulture)); break; case TypeCode.Decimal: AppendDecimal(obj.ToDecimal(CultureInfo.InvariantCulture)); break; case TypeCode.Double: AppendDouble(obj.ToDouble(CultureInfo.InvariantCulture)); break; case TypeCode.Int16: AppendInt16(obj.ToInt16(CultureInfo.InvariantCulture)); break; case TypeCode.Int32: AppendInt32(obj.ToInt32(CultureInfo.InvariantCulture)); break; case TypeCode.Int64: AppendInt64(obj.ToInt64(CultureInfo.InvariantCulture)); break; case TypeCode.SByte: AppendSByte(obj.ToSByte(CultureInfo.InvariantCulture)); break; case TypeCode.Single: AppendSingle(obj.ToSingle(CultureInfo.InvariantCulture)); break; case TypeCode.UInt16: AppendUInt16(obj.ToUInt16(CultureInfo.InvariantCulture)); break; case TypeCode.UInt32: AppendUInt32(obj.ToUInt32(CultureInfo.InvariantCulture)); break; case TypeCode.UInt64: AppendUInt64(obj.ToUInt64(CultureInfo.InvariantCulture)); break; default: AppendCheckLoopRef(obj); break; } }
// This is a custom version of Convert.ChangeType() which works // with the TypeBuilder defined types when compiling corlib. public static object ChangeType(object value, TypeSpec targetType, out bool error) { IConvertible convert_value = value as IConvertible; if (convert_value == null) { error = true; return(null); } // // We cannot rely on build-in type conversions as they are // more limited than what C# supports. // See char -> float/decimal/double conversion // error = false; try { if (targetType == TypeManager.bool_type) { return(convert_value.ToBoolean(nf_provider)); } if (targetType == TypeManager.byte_type) { return(convert_value.ToByte(nf_provider)); } if (targetType == TypeManager.char_type) { return(convert_value.ToChar(nf_provider)); } if (targetType == TypeManager.short_type) { return(convert_value.ToInt16(nf_provider)); } if (targetType == TypeManager.int32_type) { return(convert_value.ToInt32(nf_provider)); } if (targetType == TypeManager.int64_type) { return(convert_value.ToInt64(nf_provider)); } if (targetType == TypeManager.sbyte_type) { return(convert_value.ToSByte(nf_provider)); } if (targetType == TypeManager.decimal_type) { if (convert_value.GetType() == typeof(char)) { return((decimal)convert_value.ToInt32(nf_provider)); } return(convert_value.ToDecimal(nf_provider)); } if (targetType == TypeManager.double_type) { if (convert_value.GetType() == typeof(char)) { return((double)convert_value.ToInt32(nf_provider)); } return(convert_value.ToDouble(nf_provider)); } if (targetType == TypeManager.float_type) { if (convert_value.GetType() == typeof(char)) { return((float)convert_value.ToInt32(nf_provider)); } return(convert_value.ToSingle(nf_provider)); } if (targetType == TypeManager.string_type) { return(convert_value.ToString(nf_provider)); } if (targetType == TypeManager.ushort_type) { return(convert_value.ToUInt16(nf_provider)); } if (targetType == TypeManager.uint32_type) { return(convert_value.ToUInt32(nf_provider)); } if (targetType == TypeManager.uint64_type) { return(convert_value.ToUInt64(nf_provider)); } if (targetType == TypeManager.object_type) { return(value); } error = true; } catch { error = true; } return(null); }
internal static Object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) { BCLDebug.Assert(value!=null, "[Convert.DefaultToType]value!=null"); if (targetType==null) { throw new ArgumentNullException("targetType"); } if (value.GetType()==targetType) { return value; } if (targetType==ConvertTypes[(int)TypeCode.Boolean]) return value.ToBoolean(provider); if (targetType==ConvertTypes[(int)TypeCode.Char]) return value.ToChar(provider); if (targetType==ConvertTypes[(int)TypeCode.SByte]) return value.ToSByte(provider); if (targetType==ConvertTypes[(int)TypeCode.Byte]) return value.ToByte(provider); if (targetType==ConvertTypes[(int)TypeCode.Int16]) return value.ToInt16(provider); if (targetType==ConvertTypes[(int)TypeCode.UInt16]) return value.ToUInt16(provider); if (targetType==ConvertTypes[(int)TypeCode.Int32]) return value.ToInt32(provider); if (targetType==ConvertTypes[(int)TypeCode.UInt32]) return value.ToUInt32(provider); if (targetType==ConvertTypes[(int)TypeCode.Int64]) return value.ToInt64(provider); if (targetType==ConvertTypes[(int)TypeCode.UInt64]) return value.ToUInt64(provider); if (targetType==ConvertTypes[(int)TypeCode.Single]) return value.ToSingle(provider); if (targetType==ConvertTypes[(int)TypeCode.Double]) return value.ToDouble(provider); if (targetType==ConvertTypes[(int)TypeCode.Decimal]) return value.ToDecimal(provider); if (targetType==ConvertTypes[(int)TypeCode.DateTime]) return value.ToDateTime(provider); if (targetType==ConvertTypes[(int)TypeCode.String]) { return value.ToString(provider); } if (targetType==ConvertTypes[(int)TypeCode.Object]) return (Object)value; if (targetType==ConvertTypes[(int)TypeCode.DBNull]) throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull")); if (targetType==ConvertTypes[(int)TypeCode.Empty]) throw new InvalidCastException(Environment.GetResourceString("InvalidCast_Empty")); throw new InvalidCastException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidCast_FromTo"), value.GetType().FullName, targetType.FullName)); }
public static string Hex(object Number) { long num; if (Number == null) { throw new ArgumentNullException(Utils.GetResourceString("Argument_InvalidNullValue1", new string[] { "Number" })); } IConvertible convertible = Number as IConvertible; if (convertible != null) { switch (convertible.GetTypeCode()) { case TypeCode.Boolean: case TypeCode.Char: case TypeCode.DateTime: case (TypeCode.DateTime | TypeCode.Object): goto Label_013D; case TypeCode.SByte: return(Hex(convertible.ToSByte(null))); case TypeCode.Byte: return(Hex(convertible.ToByte(null))); case TypeCode.Int16: return(Hex(convertible.ToInt16(null))); case TypeCode.UInt16: return(Hex(convertible.ToUInt16(null))); case TypeCode.Int32: return(Hex(convertible.ToInt32(null))); case TypeCode.UInt32: return(Hex(convertible.ToUInt32(null))); case TypeCode.Int64: case TypeCode.Single: case TypeCode.Double: case TypeCode.Decimal: num = convertible.ToInt64(null); goto Label_010E; case TypeCode.UInt64: return(Hex(convertible.ToUInt64(null))); case TypeCode.String: try { num = Conversions.ToLong(convertible.ToString(null)); } catch (OverflowException) { return(Hex(Conversions.ToULong(convertible.ToString(null)))); } goto Label_010E; } } goto Label_013D; Label_010E: if (num == 0L) { return("0"); } if ((num <= 0L) && (num >= -2147483648L)) { return(Hex((int)num)); } return(Hex(num)); Label_013D :; throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValueType2", new string[] { "Number", Utils.VBFriendlyName(Number) })); }
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)); }
public static SByte ToSByte(this IConvertible @this) { return(@this.ToSByte(CultureInfo.CurrentCulture)); }
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)); }
public static SByte ToSByteInv(this IConvertible @this) { return(@this.ToSByte(CultureInfo.InvariantCulture)); }
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; } }
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)); }