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 bool AreValuesEqual(IConvertible x, IConvertible y, float precission = 0) { var xc = x.GetTypeCode(); var yc = y.GetTypeCode(); if (precission > 0) { if (xc == TypeCode.Decimal || yc == TypeCode.Decimal) { var xf = y.ToDecimal(CULTURE.InvariantCulture); var yf = y.ToDecimal(CULTURE.InvariantCulture); return(Math.Abs((double)(xf - yf)) <= precission); } if (xc == TypeCode.Double || yc == TypeCode.Double) { var xf = y.ToDouble(CULTURE.InvariantCulture); var yf = y.ToDouble(CULTURE.InvariantCulture); return(Math.Abs(xf - yf) <= precission); } if (xc == TypeCode.Single || yc == TypeCode.Single) { var xf = y.ToSingle(CULTURE.InvariantCulture); var yf = y.ToSingle(CULTURE.InvariantCulture); return(Math.Abs(xf - yf) <= precission); } } if (xc == yc) { return(Object.Equals(x, y)); } return(false); }
public static object ToFloat(object obj) { if (obj is float) { return(obj); } IConvertible convertible = obj as IConvertible; if (null != convertible) { return(convertible.ToSingle(null)); } return(Db4objects.Db4o.Foundation.No4.Instance); }
/// <summary> /// 使用指定的区域性特定格式设置信息将此实例的值转换为等效的单精度浮点数字。 /// </summary> /// <param name="target">要转化的实例</param> /// <param name="defaultValue">转化失败返回的默认值</param> /// <returns>与此实例的值等效的单精度浮点数字。</returns> public static float ToFloat(object target, float defaultValue) { IConvertible tmp = target as IConvertible; if (tmp == null || tmp == DBNull.Value) { return(0); } try { return(tmp.ToSingle(CultureInfo.CurrentCulture)); } catch (Exception) { return(defaultValue); } }
public bool PosTest3() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest3:Convert a random UInt64 between 10000000 UInt64.MaxValue to Single"); try { UInt64 uintA = this.GetInt64(10000000, UInt64.MaxValue); IConvertible iConvert = (IConvertible)(uintA); Single single = iConvert.ToSingle(null); } catch (Exception e) { TestLibrary.TestFramework.LogError("005", "Unexpected exception: " + e); retVal = false; } return(retVal); }
static float GetFloat(object value) { try { if (value.GetType() == typeof(Jurassic.ConcatenatedString)) { return(float.Parse(((Jurassic.ConcatenatedString)value).ToString())); } IConvertible val = (IConvertible)value; float cast = val.ToSingle(null); return(cast); } catch { return(0); } }
//----------------------------------------------------------------------------------------------------------------------------------------------------- public static void EmitConvertible(ILGenerator il, IConvertible convertible) { var formatProvider = CultureInfo.CurrentCulture; switch (convertible.GetTypeCode()) { case TypeCode.Boolean: case TypeCode.Char: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: il.Emit(OpCodes.Ldc_I4, convertible.ToInt32(formatProvider)); break; case TypeCode.UInt32: il.Emit(OpCodes.Ldc_I4, convertible.ToUInt32(formatProvider)); break; case TypeCode.Int64: il.Emit(OpCodes.Ldc_I8, convertible.ToInt64(formatProvider)); break; case TypeCode.UInt64: il.Emit(OpCodes.Ldc_I8, convertible.ToUInt64(formatProvider)); break; case TypeCode.Single: il.Emit(OpCodes.Ldc_R8, convertible.ToSingle(formatProvider)); break; case TypeCode.Double: il.Emit(OpCodes.Ldc_R8, convertible.ToDouble(formatProvider)); break; case TypeCode.String: il.Emit(OpCodes.Ldstr, convertible.ToString()); break; default: throw CreateConstantNotSupportedException(convertible.GetType()); } }
public static object DefaultConvertToType(IConvertible value, Type destinationType, IFormatProvider provider) { if (value.GetType() == destinationType) { return(value); } switch (Type.GetTypeCode(destinationType)) { case TypeCode.Boolean: return(value.ToBoolean(provider)); case TypeCode.Byte: return(value.ToByte(provider)); case TypeCode.Char: return(value.ToChar(provider)); case TypeCode.DateTime: return(value.ToDateTime(provider)); case TypeCode.Decimal: return(value.ToDecimal(provider)); case TypeCode.Double: return(value.ToDouble(provider)); case TypeCode.Int16: return(value.ToInt16(provider)); case TypeCode.Int32: return(value.ToInt32(provider)); case TypeCode.Int64: return(value.ToInt64(provider)); case TypeCode.SByte: return(value.ToSByte(provider)); case TypeCode.Single: return(value.ToSingle(provider)); case TypeCode.String: return(value.ToString(provider)); case TypeCode.UInt16: return(value.ToUInt16(provider)); case TypeCode.UInt32: return(value.ToUInt32(provider)); case TypeCode.UInt64: return(value.ToUInt64(provider)); default: throw new InvalidCastException("Can't convert from " + value.GetType().FullName + " to " + (destinationType == null ? "NULL" : destinationType.FullName)); } }
public static decimal FromObject(object Value, NumberFormatInfo NumberFormat) { if (Value == null) { return(decimal.Zero); } IConvertible convertible = Value as IConvertible; if (convertible != null) { switch (convertible.GetTypeCode()) { case TypeCode.Boolean: return(FromBoolean(convertible.ToBoolean(null))); case TypeCode.Byte: return(new decimal(convertible.ToByte(null))); case TypeCode.Int16: return(new decimal(convertible.ToInt16(null))); case TypeCode.Int32: return(new decimal(convertible.ToInt32(null))); case TypeCode.Int64: return(new decimal(convertible.ToInt64(null))); case TypeCode.Single: return(new decimal(convertible.ToSingle(null))); case TypeCode.Double: return(new decimal(convertible.ToDouble(null))); case TypeCode.Decimal: return(convertible.ToDecimal(null)); case TypeCode.String: return(FromString(convertible.ToString(null), NumberFormat)); } } throw new InvalidCastException(Utils.GetResourceString("InvalidCast_FromTo", new string[] { Utils.VBFriendlyName(Value), "Decimal" })); }
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { if (value is null) { return(base.ConvertFrom(context, culture, value)); } Type fromType = value.GetType(); if (fromType.IsNullable()) { fromType = fromType.GetGenericArguments()[0]; } TypeCode fromTypeCode = Type.GetTypeCode(fromType); IConvertible convertible = value as IConvertible; switch (fromTypeCode) { case TypeCode.DBNull: return(default); case TypeCode.Boolean: return((Boolean)value ? '1' : '0'); case TypeCode.Char: case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: case TypeCode.Single: case TypeCode.Double: case TypeCode.Decimal: case TypeCode.DateTime: return(convertible.ToSingle(defaultProvider)); default: return(base.ConvertFrom(context, culture, value)); } }
public static object Int(object Number) { 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: return(convertible.ToInt32(null)); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: case TypeCode.UInt64: return(Number); case TypeCode.Single: return(Int(convertible.ToSingle(null))); case TypeCode.Double: return(Int(convertible.ToDouble(null))); case TypeCode.Decimal: return(Int(convertible.ToDecimal(null))); case TypeCode.String: return(Int(Conversions.ToDouble(convertible.ToString(null)))); } } throw ExceptionUtils.VbMakeException(new ArgumentException(Utils.GetResourceString("Argument_NotNumericType2", new string[] { "Number", Number.GetType().FullName })), 13); }
/// <summary> /// Converts the value of this instance to an <see cref="Object"/> of the specified <see cref="Type"/> that has /// an equivalent value, using the specified culture-specific formatting information. /// </summary> /// <param name="conversionType">The <see cref="Type"/> to which the value of this instance is /// converted.</param> /// <param name="provider">An <see cref="IFormatProvider"/> interface implementation that supplies /// culture-specific formatting information.</param> /// <returns>An <see cref="Object"/> instance of type <paramref name="conversionType"/> whose value is /// equivalent to the value of this instance.</returns> object IConvertible.ToType(Type conversionType, IFormatProvider provider) { IConvertible conv = (IConvertible)this; if (conversionType == typeof(bool)) return conv.ToBoolean(provider); if (conversionType == typeof(byte)) return conv.ToByte(provider); if (conversionType == typeof(char)) return conv.ToChar(provider); if (conversionType == typeof(DateTime)) return conv.ToDateTime(provider); if (conversionType == typeof(Decimal)) return conv.ToDecimal(provider); if (conversionType == typeof(Double)) return conv.ToDouble(provider); if (conversionType == typeof(Int16)) return conv.ToInt16(provider); if (conversionType == typeof(Int32)) return conv.ToInt32(provider); if (conversionType == typeof(Int64)) return conv.ToInt64(provider); if (conversionType == typeof(SByte)) return conv.ToSByte(provider); if (conversionType == typeof(Single)) return conv.ToSingle(provider); if (conversionType == typeof(String)) return conv.ToString(provider); if (conversionType == typeof(UInt16)) return conv.ToUInt16(provider); if (conversionType == typeof(UInt32)) return conv.ToUInt32(provider); if (conversionType == typeof(UInt64)) return conv.ToUInt64(provider); if (conversionType == typeof(Int128)) return this; throw new InvalidCastException(); }
public bool PosTest2() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest2:Convert a random Uint64 less than 10000000 to float"); try { UInt64 uintA = this.GetInt64(0, 10000000); IConvertible iConvert = (IConvertible)(uintA); Single single = iConvert.ToSingle(null); if (single != (float)uintA) { TestLibrary.TestFramework.LogError("003", "the ActualResult is not the ExpectResult"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("004", "Unexpected exception: " + e); retVal = false; } return(retVal); }
public bool PosTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest1:UInt64 MinValue to SByte"); try { UInt64 uintA = UInt64.MinValue; IConvertible iConvert = (IConvertible)(uintA); Single single = iConvert.ToSingle(null); if ((ulong)single != uintA) { TestLibrary.TestFramework.LogError("001", "the ActualResult is not the ExpectResult"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("002", "Unexpected exception: " + e); retVal = false; } return(retVal); }
public bool PosTest3() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest3:The random Int64 IConvertible To Single"); try { long int64A = TestLibrary.Generator.GetInt64(-55); IConvertible iConvert = (IConvertible)(int64A); Single singleA = iConvert.ToSingle(null); if (singleA != (Single)int64A) { TestLibrary.TestFramework.LogError("005", "the ActualResult is not the ExpectResult"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("006", "Unexpected exception: " + e); retVal = false; } return(retVal); }
public bool PosTest2() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest2:The Int64 MinValue IConvertible To Single"); try { long int64A = Int64.MinValue; IConvertible iConvert = (IConvertible)(int64A); Single singleA = iConvert.ToSingle(null); if (singleA != (Single)int64A) { TestLibrary.TestFramework.LogError("003", "the ActualResult is not the ExpectResult"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("004", "Unexpected exception: " + e); retVal = false; } return(retVal); }
public bool PosTest2() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest2:UInt32 MaxValue to Single"); try { UInt32 uintA = UInt32.MaxValue; IConvertible iConvert = (IConvertible)(uintA); Single singleA = iConvert.ToSingle(null); if (singleA != uintA) { TestLibrary.TestFramework.LogError("003", "the ActualResult is not the ExpectResult"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("004", "Unexpected exception: " + e); retVal = false; } return(retVal); }
public bool PosTest3() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest4: Convert zero to single "); try { Double i1 = 0; IConvertible Icon1 = (IConvertible)i1; if (Icon1.ToSingle(null) != 0) { TestLibrary.TestFramework.LogError("003.1", "The result is not zero as expected"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("003.2", "Unexpected exception: " + e); retVal = false; } return(retVal); }
public bool PosTest3() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest3:UInt32 between MinValue and MaxValue to Single"); try { UInt32 uintA = (UInt32)(this.GetInt32(1, Int32.MaxValue) + this.GetInt32(0, Int32.MaxValue)); IConvertible iConvert = (IConvertible)(uintA); Single singleA = iConvert.ToSingle(null); if (singleA != uintA) { TestLibrary.TestFramework.LogError("005", "the ActualResult is not the ExpectResult"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("006", "Unexpected exception: " + e); retVal = false; } return(retVal); }
public bool PosTest5() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest5: Convert (double)Single.MinValue to single."); try { Double i1 = (Double)Single.MinValue; IConvertible Icon1 = (IConvertible)i1; if (Icon1.ToSingle(null) != Single.MinValue) { TestLibrary.TestFramework.LogError("005.1", "The result is not expected"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("005.2", "Unexpected exception: " + e); retVal = false; } return(retVal); }
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 TypeCode.DBNull: this.WriteNull(); return; case TypeCode.Boolean: this.WriteValue(convertible.ToBoolean(CultureInfo.InvariantCulture)); return; case TypeCode.Char: this.WriteValue(convertible.ToChar(CultureInfo.InvariantCulture)); return; case TypeCode.SByte: this.WriteValue(convertible.ToSByte(CultureInfo.InvariantCulture)); return; case TypeCode.Byte: this.WriteValue(convertible.ToByte(CultureInfo.InvariantCulture)); return; case TypeCode.Int16: this.WriteValue(convertible.ToInt16(CultureInfo.InvariantCulture)); return; case TypeCode.UInt16: this.WriteValue(convertible.ToUInt16(CultureInfo.InvariantCulture)); return; case TypeCode.Int32: this.WriteValue(convertible.ToInt32(CultureInfo.InvariantCulture)); return; case TypeCode.UInt32: this.WriteValue(convertible.ToUInt32(CultureInfo.InvariantCulture)); return; case TypeCode.Int64: this.WriteValue(convertible.ToInt64(CultureInfo.InvariantCulture)); return; case TypeCode.UInt64: this.WriteValue(convertible.ToUInt64(CultureInfo.InvariantCulture)); return; case TypeCode.Single: this.WriteValue(convertible.ToSingle(CultureInfo.InvariantCulture)); return; case TypeCode.Double: this.WriteValue(convertible.ToDouble(CultureInfo.InvariantCulture)); return; case TypeCode.Decimal: this.WriteValue(convertible.ToDecimal(CultureInfo.InvariantCulture)); return; case TypeCode.DateTime: this.WriteValue(convertible.ToDateTime(CultureInfo.InvariantCulture)); return; case TypeCode.String: this.WriteValue(convertible.ToString(CultureInfo.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.InvariantCulture, new object[] { value.GetType() })); }
public override float ToSingle(IFormatProvider provider) { return(value.ToSingle(provider)); }
// 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"); } }
object IConvertible.ToType(System.Type targetType, IFormatProvider provider) { if (targetType == null) { throw new ArgumentNullException("targetType"); } if (targetType == typeof(mpq)) { return(this); } IConvertible convertible = this; if (targetType == typeof(sbyte)) { return(convertible.ToSByte(provider)); } if (targetType == typeof(byte)) { return(convertible.ToByte(provider)); } if (targetType == typeof(short)) { return(convertible.ToInt16(provider)); } if (targetType == typeof(ushort)) { return(convertible.ToUInt16(provider)); } if (targetType == typeof(int)) { return(convertible.ToInt32(provider)); } if (targetType == typeof(uint)) { return(convertible.ToUInt32(provider)); } if (targetType == typeof(long)) { return(convertible.ToInt64(provider)); } if (targetType == typeof(ulong)) { return(convertible.ToUInt64(provider)); } if (targetType == typeof(float)) { return(convertible.ToSingle(provider)); } if (targetType == typeof(double)) { return(convertible.ToDouble(provider)); } if (targetType == typeof(Decimal)) { return(convertible.ToDecimal(provider)); } if (targetType == typeof(string)) { return(convertible.ToString(provider)); } if (targetType == typeof(object)) { return(convertible); } throw new InvalidCastException(); }
protected override ulong ToRaw(IConvertible value) { float tmp = value.ToSingle(null); return(*(uint *)&tmp); }
protected override ulong ToRaw(IConvertible value) { float tmp = value.ToSingle(null); return *(uint*)&tmp; }
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; // } }
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; }
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)); }
private static object InternalNegObj(object obj, IConvertible conv, TypeCode tc) { switch (tc) { case TypeCode.Empty: return 0; case TypeCode.Boolean: if (obj is bool) { return -((short) -(((bool) obj) > false)); } return -((short) -(conv.ToBoolean(null) > false)); case TypeCode.Byte: if (obj is byte) { return (short) -((byte) obj); } return (short) -conv.ToByte(null); case TypeCode.Int16: int num4; if (obj is short) { num4 = 0 - ((short) obj); } else { num4 = 0 - conv.ToInt16(null); } if ((num4 >= -32768) && (num4 <= 0x7fff)) { return (short) num4; } return num4; case TypeCode.Int32: long num5; if (obj is int) { num5 = 0L - ((int) obj); } else { num5 = 0L - conv.ToInt32(null); } if ((num5 >= -2147483648L) && (num5 <= 0x7fffffffL)) { return (int) num5; } return num5; case TypeCode.Int64: try { if (obj is long) { return (0L - ((long) obj)); } return (0L - conv.ToInt64(null)); } catch (StackOverflowException exception) { throw exception; } catch (OutOfMemoryException exception2) { throw exception2; } catch (ThreadAbortException exception3) { throw exception3; } catch (Exception) { return decimal.Negate(conv.ToDecimal(null)); } break; case TypeCode.Single: goto Label_01B9; case TypeCode.Double: if (obj is double) { return -((double) obj); } return -conv.ToDouble(null); case TypeCode.Decimal: break; case TypeCode.String: { string str = obj as string; if (str == null) { return -DoubleType.FromString(conv.ToString(null)); } return -DoubleType.FromString(str); } default: throw GetNoValidOperatorException(obj); } try { if (obj is decimal) { return decimal.Negate((decimal) obj); } return decimal.Negate(conv.ToDecimal(null)); } catch (StackOverflowException exception4) { throw exception4; } catch (OutOfMemoryException exception5) { throw exception5; } catch (ThreadAbortException exception6) { throw exception6; } catch (Exception) { return -conv.ToDouble(null); } Label_01B9: if (obj is float) { return -((float) obj); } return -conv.ToSingle(null); }
private void EmitConstant(IConvertible ic) { this.StackSize++; TypeCode tc = ic.GetTypeCode(); switch (tc) { case TypeCode.Boolean: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Char: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: long n = ic.ToInt64(null); switch (n) { case -1: this.generator.Emit(OperationCode.Ldc_I4_M1); break; case 0: this.generator.Emit(OperationCode.Ldc_I4_0); break; case 1: this.generator.Emit(OperationCode.Ldc_I4_1); break; case 2: this.generator.Emit(OperationCode.Ldc_I4_2); break; case 3: this.generator.Emit(OperationCode.Ldc_I4_3); break; case 4: this.generator.Emit(OperationCode.Ldc_I4_4); break; case 5: this.generator.Emit(OperationCode.Ldc_I4_5); break; case 6: this.generator.Emit(OperationCode.Ldc_I4_6); break; case 7: this.generator.Emit(OperationCode.Ldc_I4_7); break; case 8: this.generator.Emit(OperationCode.Ldc_I4_8); break; default: if (sbyte.MinValue <= n && n <= sbyte.MaxValue) { this.generator.Emit(OperationCode.Ldc_I4_S, (sbyte)n); } else if (int.MinValue <= n && n <= int.MaxValue || n <= uint.MaxValue && (tc == TypeCode.Char || tc == TypeCode.UInt16 || tc == TypeCode.UInt32)) { if (n == uint.MaxValue) this.generator.Emit(OperationCode.Ldc_I4_M1); else this.generator.Emit(OperationCode.Ldc_I4, (int)n); } else { this.generator.Emit(OperationCode.Ldc_I8, n); tc = TypeCode.Empty; //Suppress conversion to long } break; } if (tc == TypeCode.Int64) this.generator.Emit(OperationCode.Conv_I8); return; case TypeCode.UInt64: this.generator.Emit(OperationCode.Ldc_I8, (long)ic.ToUInt64(null)); return; case TypeCode.Single: this.generator.Emit(OperationCode.Ldc_R4, ic.ToSingle(null)); return; case TypeCode.Double: this.generator.Emit(OperationCode.Ldc_R8, ic.ToDouble(null)); return; case TypeCode.String: this.generator.Emit(OperationCode.Ldstr, ic.ToString(null)); return; case TypeCode.Decimal: var bits = Decimal.GetBits(ic.ToDecimal(null)); this.generator.Emit(OperationCode.Ldc_I4, bits[0]); this.generator.Emit(OperationCode.Ldc_I4, bits[1]); this.StackSize++; this.generator.Emit(OperationCode.Ldc_I4, bits[2]); this.StackSize++; if (bits[3] >= 0) this.generator.Emit(OperationCode.Ldc_I4_0); else this.generator.Emit(OperationCode.Ldc_I4_1); this.StackSize++; int scale = (bits[3]&0x7FFFFF)>>16; if (scale > 28) scale = 28; this.generator.Emit(OperationCode.Ldc_I4_S, scale); this.StackSize++; this.generator.Emit(OperationCode.Newobj, this.DecimalConstructor); this.StackSize -= 4; return; } }
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); }
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 }
public static bool FromObject(object Value) { if (Value == null) { return(false); } IConvertible valueInterface = Value as IConvertible; if (valueInterface != null) { switch (valueInterface.GetTypeCode()) { case TypeCode.Boolean: if (Value is bool) { return((bool)Value); } return(valueInterface.ToBoolean(null)); case TypeCode.Byte: if (Value is byte) { return(((byte)Value) > 0); } return(valueInterface.ToByte(null) > 0); case TypeCode.Int16: if (Value is short) { return(((short)Value) > 0); } return(valueInterface.ToInt16(null) > 0); case TypeCode.Int32: if (Value is int) { return(((int)Value) > 0); } return(valueInterface.ToInt32(null) > 0); case TypeCode.Int64: if (Value is long) { return(((long)Value) > 0L); } return(valueInterface.ToInt64(null) > 0L); case TypeCode.Single: if (Value is float) { return(!(((float)Value) == 0f)); } return(!(valueInterface.ToSingle(null) == 0f)); case TypeCode.Double: if (Value is double) { return(!(((double)Value) == 0.0)); } return(!(valueInterface.ToDouble(null) == 0.0)); case TypeCode.Decimal: return(DecimalToBoolean(valueInterface)); case TypeCode.String: { string str = Value as string; if (str == null) { return(FromString(valueInterface.ToString(null))); } return(FromString(str)); } } } throw new InvalidCastException(Utils.GetResourceString("InvalidCast_FromTo", new string[] { Utils.VBFriendlyName(Value), "Boolean" })); }
internal static uint ToUint32(Object value, IConvertible ic){ switch (Convert.GetTypeCode(value, ic)){ case TypeCode.Empty: return 0; case TypeCode.DBNull: return 0; case TypeCode.Boolean: return ic.ToBoolean(null) ? (uint)1 : (uint)0; case TypeCode.Char: return (uint)ic.ToChar(null); case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.UInt32: return ic.ToUInt32(null); case TypeCode.UInt64: return (uint)ic.ToUInt64(null); case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: return (uint)ic.ToInt64(null); case TypeCode.Single: return (uint)ic.ToSingle(null); case TypeCode.Double: case TypeCode.Decimal: return (uint)ic.ToDouble(null); case TypeCode.Object: case TypeCode.DateTime: Object pval = Convert.ToPrimitive(value, PreferredType.Number, ref ic); if (pval != value) return Convert.ToUint32(pval, ic); else return 0; case TypeCode.String: return (uint)Convert.ToNumber(ic.ToString(null)); } return 0; //should never get here }
internal static Object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) { Contract.Requires(value != null, "[Convert.DefaultToType]value!=null"); if (targetType==null) { throw new ArgumentNullException("targetType"); } Contract.EndContractBlock(); RuntimeType rtTargetType = targetType as RuntimeType; if (rtTargetType != null) { if (value.GetType() == targetType) { return value; } if (rtTargetType == ConvertTypes[(int)TypeCode.Boolean]) return value.ToBoolean(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Char]) return value.ToChar(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.SByte]) return value.ToSByte(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Byte]) return value.ToByte(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Int16]) return value.ToInt16(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.UInt16]) return value.ToUInt16(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Int32]) return value.ToInt32(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.UInt32]) return value.ToUInt32(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Int64]) return value.ToInt64(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.UInt64]) return value.ToUInt64(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Single]) return value.ToSingle(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Double]) return value.ToDouble(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Decimal]) return value.ToDecimal(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.DateTime]) return value.ToDateTime(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.String]) return value.ToString(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Object]) return (Object)value; // Need to special case Enum because typecode will be underlying type, e.g. Int32 if (rtTargetType == EnumType) return (Enum)value; if (rtTargetType == ConvertTypes[(int)TypeCode.DBNull]) throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull")); if (rtTargetType == ConvertTypes[(int)TypeCode.Empty]) throw new InvalidCastException(Environment.GetResourceString("InvalidCast_Empty")); } throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", value.GetType().FullName, targetType.FullName)); }
internal static 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 })); }
public static int FromObject(object Value) { if (Value == null) { return(0); } IConvertible valueInterface = Value as IConvertible; if (valueInterface != null) { switch (valueInterface.GetTypeCode()) { case TypeCode.Boolean: return((int)-(valueInterface.ToBoolean(null) > false)); case TypeCode.Byte: if (Value is byte) { return((byte)Value); } return(valueInterface.ToByte(null)); case TypeCode.Int16: if (Value is short) { return((short)Value); } return(valueInterface.ToInt16(null)); case TypeCode.Int32: if (Value is int) { return((int)Value); } return(valueInterface.ToInt32(null)); case TypeCode.Int64: if (Value is long) { return((int)((long)Value)); } return((int)valueInterface.ToInt64(null)); case TypeCode.Single: if (Value is float) { return((int)Math.Round((double)((float)Value))); } return((int)Math.Round((double)valueInterface.ToSingle(null))); case TypeCode.Double: if (Value is double) { return((int)Math.Round((double)Value)); } return((int)Math.Round(valueInterface.ToDouble(null))); case TypeCode.Decimal: return(DecimalToInteger(valueInterface)); case TypeCode.String: return(FromString(valueInterface.ToString(null))); } } throw new InvalidCastException(Utils.GetResourceString("InvalidCast_FromTo", new string[] { Utils.VBFriendlyName(Value), "Integer" })); }
/// <summary> /// Converts the value of this instance to an object of the specified type. /// </summary> /// <param name="targetType">The type to which the value of this instance is converted.</param> /// <param name="provider">Culture-specific formatting information.</param> public object ToType(Type targetType, IFormatProvider?provider) { if (targetType == typeof(mpq_t)) { return(this); } IConvertible value = this; if (targetType == typeof(sbyte)) { return(value.ToSByte(provider)); } else if (targetType == typeof(byte)) { return(value.ToByte(provider)); } else if (targetType == typeof(short)) { return(value.ToInt16(provider)); } else if (targetType == typeof(ushort)) { return(value.ToUInt16(provider)); } else if (targetType == typeof(int)) { return(value.ToInt32(provider)); } else if (targetType == typeof(uint)) { return(value.ToUInt32(provider)); } else if (targetType == typeof(long)) { return(value.ToInt64(provider)); } else if (targetType == typeof(ulong)) { return(value.ToUInt64(provider)); } else if (targetType == typeof(float)) { return(value.ToSingle(provider)); } else if (targetType == typeof(double)) { return(value.ToDouble(provider)); } else if (targetType == typeof(decimal)) { return(value.ToDecimal(provider)); } else if (targetType == typeof(string)) { return(value.ToString(provider)); } else if (targetType == typeof(object)) { return(value); } else { throw new InvalidCastException(); } }
public static object ChangeType(object value, Type conversionType, IFormatProvider provider) { if (conversionType == null) { throw new ArgumentNullException("conversionType"); } if (value == null) { if (conversionType.IsValueType) { throw new InvalidCastException("InvalidCast_CannotCastNullToValueType"); } return(null); } IConvertible convertible1 = value as IConvertible; if (convertible1 == null) { if (value.GetType() != conversionType) { throw new InvalidCastException("InvalidCast_IConvertible"); } return(value); } if (typeof(System.Enum).IsAssignableFrom(conversionType) == true) { return(convertible1.ToInt32(provider)); } #region - bool - if (conversionType == typeof(bool)) { return(convertible1.ToBoolean(provider)); } #endregion #region - char - if (conversionType == typeof(char)) { return(convertible1.ToChar(provider)); } #endregion #region - sbyte - if (conversionType == typeof(sbyte)) { return(convertible1.ToSByte(provider)); } #endregion #region - byte - if (conversionType == typeof(byte)) { return(convertible1.ToByte(provider)); } #endregion #region - short - if (conversionType == typeof(short)) { return(convertible1.ToInt16(provider)); } #endregion #region - ushort - if (conversionType == typeof(ushort)) { return(convertible1.ToUInt16(provider)); } #endregion #region - int - if (conversionType == typeof(int)) { return(convertible1.ToInt32(provider)); } #endregion #region - uint - if (conversionType == typeof(uint)) { return(convertible1.ToUInt32(provider)); } #endregion #region - long - if (conversionType == typeof(long)) { return(convertible1.ToInt64(provider)); } #endregion #region - ulong - if (conversionType == typeof(ulong)) { return(convertible1.ToUInt64(provider)); } #endregion #region - float - if (conversionType == typeof(float)) { return(convertible1.ToSingle(provider)); } #endregion #region - double - if (conversionType == typeof(double)) { return(convertible1.ToDouble(provider)); } #endregion #region - decimal - if (conversionType == typeof(decimal)) { return(convertible1.ToDecimal(provider)); } #endregion #region - DateTime - if (conversionType == typeof(DateTime)) { try { return(convertible1.ToDateTime(provider)); } catch (Exception ex) { throw new System.FormatException(ex.Message + "\n" + value.ToString()); } } #endregion if (conversionType == typeof(Nullable <DateTime>)) { var _value = value as Nullable <DateTime>; if (_value.HasValue) { return(_value.Value); } return(_value); } #region - string - if (conversionType == typeof(string)) { return(convertible1.ToString(provider)); } #endregion #region - object - if (conversionType == typeof(object)) { return(value); } #endregion #region = Uri = if (conversionType == typeof(Uri)) { string s = value.ToString(); return(new Uri(s)); } #endregion return(convertible1.ToType(conversionType, provider)); }
public static Literal DoDiv(IConvertible ic1, IConvertible ic2, TypeCode code1, TypeCode code2, BinaryExpression binaryExpression){ TypeNode type = SystemTypes.Object; object val = null; switch(code1){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: int i = ic1.ToInt32(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Byte: case TypeCode.Char: case TypeCode.UInt16: val = i / ic2.ToInt32(null); type = SystemTypes.Int32; break; case TypeCode.Int64: case TypeCode.UInt32: case TypeCode.UInt64: val = i / ic2.ToInt64(null); type = SystemTypes.Int64; break; case TypeCode.Single: val = i / ic2.ToSingle(null); type = SystemTypes.Single; break; case TypeCode.Double: val = i / ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = i / ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.Byte: case TypeCode.UInt16: ushort us = ic1.ToUInt16(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: val = us / ic2.ToInt32(null); type = SystemTypes.Int32; break; case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.UInt32: val = us / ic2.ToUInt32(null); type = SystemTypes.UInt32; break; case TypeCode.Int64: val = us / ic2.ToInt64(null); type = SystemTypes.Int64; break; case TypeCode.UInt64: val = us / ic2.ToUInt64(null); type = SystemTypes.UInt64; break; case TypeCode.Single: case TypeCode.Double: val = us / ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = us / ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.UInt32: uint ui = ic1.ToUInt32(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: val = ui / ic2.ToInt64(null); type = SystemTypes.Int64; break; case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.UInt32: val = ui / ic2.ToUInt32(null); type = SystemTypes.UInt32; break; case TypeCode.UInt64: val = ui / ic2.ToUInt64(null); type = SystemTypes.UInt64; break; case TypeCode.Single: val = ui / ic2.ToSingle(null); type = SystemTypes.Single; break; case TypeCode.Double: val = ui / ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = ui / ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.Int64: long l = ic1.ToInt64(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: val = l / ic2.ToInt64(null); type = SystemTypes.Int64; break; case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.UInt32: val = l / ic2.ToUInt32(null); type = SystemTypes.UInt32; break; case TypeCode.UInt64: val = l / (long)ic2.ToUInt64(null); type = SystemTypes.UInt64; break; case TypeCode.Single: val = l / ic2.ToSingle(null); type = SystemTypes.Single; break; case TypeCode.Double: val = l / ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = l / ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.UInt64: ulong ul = ic1.ToUInt64(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.UInt32: val = ul / ic2.ToUInt32(null); type = SystemTypes.UInt32; break; case TypeCode.Int64: case TypeCode.UInt64: val = ul / ic2.ToUInt64(null); type = SystemTypes.UInt64; break; case TypeCode.Single: val = ul / ic2.ToSingle(null); type = SystemTypes.Single; break; case TypeCode.Double: val = ul / ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = ul / ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.Single: float f = ic1.ToSingle(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: val = f / ic2.ToInt16(null); type = SystemTypes.Single; break; case TypeCode.Int32: case TypeCode.Int64: val = f / (double)ic2.ToInt64(null); type = SystemTypes.Double; break; case TypeCode.Byte: case TypeCode.Char: case TypeCode.UInt16: val = f / ic2.ToUInt16(null); type = SystemTypes.Single; break; case TypeCode.UInt32: case TypeCode.UInt64: val = f / (double)ic2.ToUInt64(null); type = SystemTypes.Double; break; case TypeCode.Single: val = f / ic2.ToSingle(null); type = SystemTypes.Single; break; case TypeCode.Double: val = f / ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = f / (double)ic2.ToDecimal(null); type = SystemTypes.Double; break; default: return null; } break; case TypeCode.Double: double d = ic1.ToDouble(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: val = d / ic2.ToInt64(null); type = SystemTypes.Double; break; case TypeCode.Byte: case TypeCode.Char: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: val = d / ic2.ToUInt64(null); type = SystemTypes.Double; break; case TypeCode.Single: case TypeCode.Double: val = d / ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = d / (double)ic2.ToDecimal(null); type = SystemTypes.Double; break; default: return null; } break; case TypeCode.Decimal: decimal dec = ic1.ToDecimal(null); switch(code2){ case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.Int32: val = dec / ic2.ToInt32(null); type = SystemTypes.Decimal; break; case TypeCode.UInt32: case TypeCode.Int64: case TypeCode.UInt64: val = dec / ic2.ToInt64(null); type = SystemTypes.Decimal; break; case TypeCode.Decimal: val = dec / ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; default: return null; } return new Literal(val, type, binaryExpression.SourceContext); }
public static Literal DoSubOvf(IConvertible ic1, IConvertible ic2, TypeCode code1, TypeCode code2, BinaryExpression binaryExpression){ TypeNode type = SystemTypes.Object; object val = null; checked{switch(code1){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: int i = ic1.ToInt32(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Byte: case TypeCode.Char: case TypeCode.UInt16: val = i - ic2.ToInt32(null); type = SystemTypes.Int32; break; case TypeCode.Int64: case TypeCode.UInt32: case TypeCode.UInt64: val = i - ic2.ToInt64(null); type = SystemTypes.Int64; break; case TypeCode.Single: val = i - ic2.ToSingle(null); type = SystemTypes.Single; break; case TypeCode.Double: val = i - ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = i - ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.Byte: case TypeCode.UInt16: ushort us = ic1.ToUInt16(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: val = us - ic2.ToInt32(null); type = SystemTypes.Int32; break; case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.UInt32: val = us - ic2.ToUInt32(null); type = SystemTypes.UInt32; break; case TypeCode.Int64: val = us - ic2.ToInt64(null); type = SystemTypes.Int64; break; case TypeCode.UInt64: val = us - ic2.ToUInt64(null); type = SystemTypes.UInt64; break; case TypeCode.Single: case TypeCode.Double: val = us - ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = us - ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.Char: char ch = ic1.ToChar(null); if (code2 != TypeCode.Char) goto default; val = ch - ic2.ToChar(null); type = SystemTypes.Int32; break; case TypeCode.UInt32: uint ui = ic1.ToUInt32(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: val = ui - ic2.ToInt64(null); type = SystemTypes.Int64; break; case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.UInt32: val = ui - ic2.ToUInt32(null); type = SystemTypes.UInt32; break; case TypeCode.UInt64: val = ui - ic2.ToUInt64(null); type = SystemTypes.UInt64; break; case TypeCode.Single: val = ui - ic2.ToSingle(null); type = SystemTypes.Single; break; case TypeCode.Double: val = ui - ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = ui - ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.Int64: long l = ic1.ToInt64(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: val = l - ic2.ToInt64(null); type = SystemTypes.Int64; break; case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.UInt32: val = l - ic2.ToUInt32(null); type = SystemTypes.UInt32; break; case TypeCode.UInt64: val = l - (long)ic2.ToUInt64(null); type = SystemTypes.UInt64; break; case TypeCode.Single: val = l - ic2.ToSingle(null); type = SystemTypes.Single; break; case TypeCode.Double: val = l - ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = l - ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.UInt64: ulong ul = ic1.ToUInt64(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.UInt32: val = ul - ic2.ToUInt32(null); type = SystemTypes.UInt64; break; case TypeCode.Int64: case TypeCode.UInt64: val = ul - ic2.ToUInt64(null); type = SystemTypes.UInt64; break; case TypeCode.Single: val = ul - ic2.ToSingle(null); type = SystemTypes.Single; break; case TypeCode.Double: val = ul - ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = ul - ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.Single: float f = ic1.ToSingle(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: val = f - ic2.ToInt16(null); type = SystemTypes.Single; break; case TypeCode.Int32: case TypeCode.Int64: val = f - (double)ic2.ToInt64(null); type = SystemTypes.Double; break; case TypeCode.Byte: case TypeCode.Char: case TypeCode.UInt16: val = f - ic2.ToUInt16(null); type = SystemTypes.Single; break; case TypeCode.UInt32: case TypeCode.UInt64: val = f - (double)ic2.ToUInt64(null); type = SystemTypes.Double; break; case TypeCode.Single: case TypeCode.Double: val = f - ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = f - (double)ic2.ToDecimal(null); type = SystemTypes.Double; break; default: return null; } break; case TypeCode.Double: double d = ic1.ToDouble(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: val = d - ic2.ToInt64(null); type = SystemTypes.Double; break; case TypeCode.Byte: case TypeCode.Char: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: val = d - ic2.ToUInt64(null); type = SystemTypes.Double; break; case TypeCode.Single: case TypeCode.Double: val = d - ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = d - (double)ic2.ToDecimal(null); type = SystemTypes.Double; break; default: return null; } break; case TypeCode.Decimal: decimal dec = ic1.ToDecimal(null); switch(code2){ case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.Int32: val = dec - ic2.ToInt32(null); type = SystemTypes.Decimal; break; case TypeCode.UInt32: case TypeCode.Int64: case TypeCode.UInt64: val = dec - ic2.ToInt64(null); type = SystemTypes.Decimal; break; case TypeCode.Decimal: val = dec - ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; default: return null; } } return new Literal(val, type, binaryExpression.SourceContext); }
internal static Object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) { 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)); }
/// <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 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())); }
public static Literal DoAddOvf(IConvertible ic1, IConvertible ic2, TypeCode code1, TypeCode code2, BinaryExpression binaryExpression){ TypeNode type = SystemTypes.Object; object val = null; checked{switch(code1){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: int i = ic1.ToInt32(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Byte: case TypeCode.Char: case TypeCode.UInt16: val = i + ic2.ToInt32(null); type = SystemTypes.Int32; break; case TypeCode.Int64: case TypeCode.UInt32: case TypeCode.UInt64: val = i + ic2.ToInt64(null); type = SystemTypes.Int64; break; case TypeCode.Single: val = i + ic2.ToSingle(null); type = SystemTypes.Single; break; case TypeCode.Double: val = i + ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = i + ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.Byte: case TypeCode.UInt16: ushort us = ic1.ToUInt16(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: val = us + ic2.ToInt32(null); type = SystemTypes.Int32; break; case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.UInt32: val = us + ic2.ToUInt32(null); type = SystemTypes.UInt32; break; case TypeCode.Int64: val = us + ic2.ToInt64(null); type = SystemTypes.Int64; break; case TypeCode.UInt64: val = us + ic2.ToUInt64(null); type = SystemTypes.UInt64; break; case TypeCode.Single: val = us + ic2.ToSingle(null); type = SystemTypes.Single; break; case TypeCode.Double: val = us + ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = us + ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.UInt32: uint ui = ic1.ToUInt32(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: val = ui + ic2.ToInt64(null); type = SystemTypes.Int64; break; case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.UInt32: val = ui + ic2.ToUInt32(null); type = SystemTypes.UInt32; break; case TypeCode.UInt64: val = ui + ic2.ToUInt64(null); type = SystemTypes.UInt64; break; case TypeCode.Single: val = ui + ic2.ToSingle(null); type = SystemTypes.Single; break; case TypeCode.Double: val = ui + ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = ui + ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.Int64: long l = ic1.ToInt64(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: val = l + ic2.ToInt64(null); type = SystemTypes.Int64; break; case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.UInt32: val = l + ic2.ToUInt32(null); type = SystemTypes.UInt32; break; case TypeCode.UInt64: if (l >= 0){ val = ((ulong)l) + ic2.ToUInt64(null); type = SystemTypes.UInt64; }else{ val = l + (long)ic2.ToUInt64(null); type = SystemTypes.Int64; } break; case TypeCode.Single: val = l + ic2.ToSingle(null); type = SystemTypes.Single; break; case TypeCode.Double: val = l + ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = l + ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.UInt64: ulong ul = ic1.ToUInt64(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.UInt32: val = ul + ic2.ToUInt32(null); type = SystemTypes.UInt32; break; case TypeCode.Int64: case TypeCode.UInt64: val = ul + ic2.ToUInt64(null); type = SystemTypes.UInt64; break; case TypeCode.Single: val = ul + ic2.ToSingle(null); type = SystemTypes.Single; break; case TypeCode.Double: val = ul + ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = ul + ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.Single: float f = ic1.ToSingle(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: val = f + ic2.ToInt16(null); type = SystemTypes.Single; break; case TypeCode.Int32: case TypeCode.Int64: val = f + (double)ic2.ToInt64(null); type = SystemTypes.Double; break; case TypeCode.Byte: case TypeCode.Char: case TypeCode.UInt16: val = f + ic2.ToUInt16(null); type = SystemTypes.Single; break; case TypeCode.UInt32: case TypeCode.UInt64: val = f + (double)ic2.ToUInt64(null); type = SystemTypes.Double; break; case TypeCode.Single: case TypeCode.Double: val = f + ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = f + (double)ic2.ToDecimal(null); type = SystemTypes.Double; break; default: return null; } break; case TypeCode.Double: double d = ic1.ToDouble(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: val = d + ic2.ToInt64(null); type = SystemTypes.Double; break; case TypeCode.Byte: case TypeCode.Char: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: val = d + ic2.ToUInt64(null); type = SystemTypes.Double; break; case TypeCode.Single: case TypeCode.Double: val = d + ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = d + (double)ic2.ToDecimal(null); type = SystemTypes.Double; break; default: return null; } break; case TypeCode.Decimal: decimal dec = ic1.ToDecimal(null); switch(code2){ case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.Int32: val = dec + ic2.ToInt32(null); type = SystemTypes.Decimal; break; case TypeCode.UInt32: case TypeCode.Int64: case TypeCode.UInt64: val = dec + ic2.ToInt64(null); type = SystemTypes.Decimal; break; case TypeCode.Decimal: val = dec + ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.String: string str = ic1.ToString(null); switch (code2) { case TypeCode.String: val = str + ic2.ToString(null); type = SystemTypes.String; break; default: return null; } break; default: return null; }} return new Literal(val, type, binaryExpression.SourceContext); }