private static bool PropertyValueEquals(object value1, object value2) { if (object.Equals(value1, value2)) { return(true); } if (value1 is Enum || value2 is Enum) { IConvertible convertible = value1 as IConvertible; IConvertible convertible2 = value2 as IConvertible; if (convertible != null && convertible2 != null) { try { ulong num = convertible.ToUInt64(null); ulong num2 = convertible2.ToUInt64(null); return(num == num2); } catch (OverflowException) { } catch (FormatException) { return(false); } try { long num3 = convertible.ToInt64(null); long num4 = convertible2.ToInt64(null); return(num3 == num4); } catch (OverflowException) { return(false); } catch (FormatException) { return(false); } return(false); } return(false); } IList list = value1 as IList; IList list2 = value2 as IList; if (list != null && list2 != null) { bool flag = list2.Count == list.Count; int num5 = 0; while (flag && num5 < list.Count) { flag = ComparisonFilter.PropertyValueEquals(list[num5], list2[num5]); num5++; } return(flag); } return(false); }
public void ToUInt64Test() { ulong expected = 3; Assert.Equal(expected, lengthAsIConvertible.ToUInt64(null)); Assert.Equal(expected, Convert.ToUInt64(length)); Assert.Equal(expected, Convert.ChangeType(length, typeof(ulong))); }
/// <summary> /// Converts object value to invariant format (understood by JavaScript) /// </summary> /// <param name="value">Object value</param> /// <param name="objTypeCode">Object TypeCode</param> /// <param name="safeConversion">Check and remove unusual unicode characters from the result string.</param> /// <returns>Object value converted to string</returns> internal static string XmlConvertToString(IConvertible value, TypeCode objTypeCode, bool safeConversion = false) { if (value == null) { return("null"); } switch (objTypeCode) { case TypeCode.Boolean: return(XmlConvert.ToString(value.ToBoolean(CultureInfo.InvariantCulture))); // boolean as lowercase case TypeCode.Byte: return(XmlConvert.ToString(value.ToByte(CultureInfo.InvariantCulture))); case TypeCode.SByte: return(XmlConvert.ToString(value.ToSByte(CultureInfo.InvariantCulture))); case TypeCode.Int16: return(XmlConvert.ToString(value.ToInt16(CultureInfo.InvariantCulture))); case TypeCode.Int32: return(XmlConvert.ToString(value.ToInt32(CultureInfo.InvariantCulture))); case TypeCode.Int64: return(XmlConvert.ToString(value.ToInt64(CultureInfo.InvariantCulture))); case TypeCode.UInt16: return(XmlConvert.ToString(value.ToUInt16(CultureInfo.InvariantCulture))); case TypeCode.UInt32: return(XmlConvert.ToString(value.ToUInt32(CultureInfo.InvariantCulture))); case TypeCode.UInt64: return(XmlConvert.ToString(value.ToUInt64(CultureInfo.InvariantCulture))); case TypeCode.Single: return(XmlConvertToString(value.ToSingle(CultureInfo.InvariantCulture))); case TypeCode.Double: return(XmlConvertToString(value.ToDouble(CultureInfo.InvariantCulture))); case TypeCode.Decimal: return(XmlConvertToString(value.ToDecimal(CultureInfo.InvariantCulture))); case TypeCode.DateTime: return(XmlConvert.ToString(value.ToDateTime(CultureInfo.InvariantCulture), XmlDateTimeSerializationMode.Utc)); case TypeCode.Char: return(XmlConvert.ToString(value.ToChar(CultureInfo.InvariantCulture))); case TypeCode.String: return(safeConversion ? RemoveInvalidXmlChars(value.ToString(CultureInfo.InvariantCulture)) : value.ToString(CultureInfo.InvariantCulture)); default: return(XmlConvertToStringInvariant(value, safeConversion)); } }
public object EvaluatePostOrPrefix(ref object v) { int num; double num5; IConvertible iConvertible = Microsoft.JScript.Convert.GetIConvertible(v); switch (Microsoft.JScript.Convert.GetTypeCode(v, iConvertible)) { case TypeCode.Empty: v = (double)1.0 / (double)0.0; return(v); case TypeCode.DBNull: v = 0; return(this.DoOp(0)); case TypeCode.Boolean: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: v = num = iConvertible.ToInt32(null); return(this.DoOp(num)); case TypeCode.Char: num = iConvertible.ToInt32(null); return(((IConvertible)this.DoOp(num)).ToChar(null)); case TypeCode.UInt32: uint num2; v = num2 = iConvertible.ToUInt32(null); return(this.DoOp(num2)); case TypeCode.Int64: long num3; v = num3 = iConvertible.ToInt64(null); return(this.DoOp(num3)); case TypeCode.UInt64: ulong num4; v = num4 = iConvertible.ToUInt64(null); return(this.DoOp(num4)); case TypeCode.Single: case TypeCode.Double: v = num5 = iConvertible.ToDouble(null); return(this.DoOp(num5)); } MethodInfo @operator = this.GetOperator(v.GetType()); if (@operator != null) { return(@operator.Invoke(null, BindingFlags.Default, JSBinder.ob, new object[] { v }, null)); } v = num5 = Microsoft.JScript.Convert.ToNumber(v, iConvertible); return(this.DoOp(num5)); }
public void ToUInt64Test() { IConvertible target = (Half)12345.999f; IFormatProvider provider = CultureInfo.InvariantCulture; ulong expected = 12344; ulong actual = target.ToUInt64(provider); Assert.Equal(expected, actual); }
public override void LoadData(BinaryReader reader, StructInstance instance) { int offset = (int)reader.BaseStream.Position; IConvertible iValue = ReadIntValue(reader, instance); ulong value = iValue.ToUInt64(CultureInfo.CurrentCulture); EnumDef enumDef = GetEnumAttribute("enum"); AddCell(instance, new SetValue((uint)value, enumDef, _size), offset); }
public void op_ToUInt64_IFormatProvider() { const ulong expected = 123; IConvertible value = (AlphaDecimal)123; var actual = value.ToUInt64(CultureInfo.InvariantCulture); Assert.Equal(expected, actual); }
private static bool TryConvertEnum(object value, Type type, out object result) { Type underlyingType = Enum.GetUnderlyingType(type); IConvertible convertiable = value as IConvertible; try { if (underlyingType == typeof(Int32)) { result = Enum.ToObject(type, convertiable.ToInt32(CultureInfo.CurrentCulture)); return(true); } if (underlyingType == typeof(Int64)) { result = Enum.ToObject(type, convertiable.ToInt64(CultureInfo.CurrentCulture)); return(true); } if (underlyingType == typeof(Int16)) { result = Enum.ToObject(type, convertiable.ToInt16(CultureInfo.CurrentCulture)); return(true); } if (underlyingType == typeof(Byte)) { result = Enum.ToObject(type, convertiable.ToByte(CultureInfo.CurrentCulture)); return(true); } if (underlyingType == typeof(UInt32)) { result = Enum.ToObject(type, convertiable.ToUInt32(CultureInfo.CurrentCulture)); return(true); } if (underlyingType == typeof(UInt64)) { result = Enum.ToObject(type, convertiable.ToUInt64(CultureInfo.CurrentCulture)); return(true); } if (underlyingType == typeof(UInt16)) { result = Enum.ToObject(type, convertiable.ToUInt16(CultureInfo.CurrentCulture)); return(true); } if (underlyingType == typeof(SByte)) { result = Enum.ToObject(type, convertiable.ToSByte(CultureInfo.CurrentCulture)); return(true); } } catch (InvalidCastException) { } result = null; return(false); }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="conv"></param> /// <param name="formatProvider"></param> /// <returns></returns> private static T To <T>(IConvertible conv, IFormatProvider formatProvider = null) { formatProvider = formatProvider ?? CultureInfo.CurrentCulture; switch (typeof(T).Name) { case TypeNames.BOOLEAN: return(conv.ToBoolean(formatProvider).To <T>()); case TypeNames.BYTE: return(conv.ToByte(formatProvider).To <T>()); case TypeNames.CHAR: return(conv.ToChar(formatProvider).To <T>()); case TypeNames.DATE_TIME: return(conv.ToDateTime(formatProvider).To <T>()); case TypeNames.DECIMAL: return(conv.ToDecimal(formatProvider).To <T>()); case TypeNames.DOUBLE: return(conv.ToDouble(formatProvider).To <T>()); case TypeNames.INT16: return(conv.ToInt16(formatProvider).To <T>()); case TypeNames.INT32: return(conv.ToInt32(formatProvider).To <T>()); case TypeNames.INT64: return(conv.ToInt64(formatProvider).To <T>()); case TypeNames.S_BYTE: return(conv.ToSByte(formatProvider).To <T>()); case TypeNames.SINGLE: return(conv.ToSingle(formatProvider).To <T>()); case TypeNames.STRING: return(conv.ToString(formatProvider).To <T>()); case TypeNames.U_INT16: return(conv.ToUInt16(formatProvider).To <T>()); case TypeNames.U_INT32: return(conv.ToUInt32(formatProvider).To <T>()); case TypeNames.U_INT64: return(conv.ToUInt64(formatProvider).To <T>()); default: return(default(T)); } }
/// <summary> /// Returns true if bit is set in value (&) /// </summary> /// <param name="value">An enum, int, ...</param> /// <param name="bit">An int</param> /// <returns>A boolean</returns> public static bool HasAnyFlagBit(this IConvertible value, IConvertible bit) { var uBit = bit.ToInt32(null); Contract.Assert(uBit >= 0 && uBit <= 63); var uFlag = 1UL << uBit; var uThis = value.ToUInt64(null); return((uThis & uFlag) != 0); }
static void Test_ToUInt64(IConvertible c) { Console.WriteLine("ToUInt64"); try { Console.WriteLine(c.ToUInt64(null)); } catch (Exception exc) { Console.WriteLine(exc.GetType()); } }
void EmitValue(IConvertible val) { switch (val.GetTypeCode()) { case TypeCode.UInt64: g.EmitI8Helper(unchecked ((long)val.ToUInt64(null)), false); break; case TypeCode.Int64: g.EmitI8Helper(val.ToInt64(null), true); break; case TypeCode.UInt32: g.EmitI4Helper(unchecked ((int)val.ToUInt64(null))); break; default: g.EmitI4Helper(val.ToInt32(null)); break; } }
private static IEnumerable <byte> GetBytesFromConvertible(Type type, IConvertible value, uint defaultStringLength) { IEnumerable <byte> varValueBytes = null; if (value == null) { return(null); } switch (value.GetTypeCode()) { case TypeCode.Boolean: varValueBytes = BitConverter.GetBytes(value.ToBoolean(null)); break; case TypeCode.Byte: varValueBytes = new byte[] { value.ToByte(null) }; break; case TypeCode.Char: varValueBytes = BitConverter.GetBytes(value.ToChar(null)); break; case TypeCode.Int16: varValueBytes = BitConverter.GetBytes(value.ToInt16(null)); break; case TypeCode.Int32: varValueBytes = BitConverter.GetBytes(value.ToInt32(null)); break; case TypeCode.Int64: varValueBytes = BitConverter.GetBytes(value.ToInt64(null)); break; case TypeCode.UInt16: varValueBytes = BitConverter.GetBytes(value.ToUInt16(null)); break; case TypeCode.UInt32: varValueBytes = BitConverter.GetBytes(value.ToUInt32(null)); break; case TypeCode.UInt64: varValueBytes = BitConverter.GetBytes(value.ToUInt64(null)); break; case TypeCode.Single: varValueBytes = BitConverter.GetBytes(value.ToSingle(null)); break; case TypeCode.Double: varValueBytes = BitConverter.GetBytes(value.ToDouble(null)); break; case TypeCode.DateTime: varValueBytes = BitConverter.GetBytes(value.ToInt32(null)); break; case TypeCode.String: varValueBytes = value.ToString().ToAdsBytes(); break; case TypeCode.Object: if (Type.Equals(typeof(Date), type)) { varValueBytes = BitConverter.GetBytes(value.ToInt32(null)); } if (Type.Equals(typeof(Time), type)) { varValueBytes = BitConverter.GetBytes(value.ToInt32(null)); } break; } return(varValueBytes); }
/// <summary> /// Returns the specified value as an array of bytes in the target endian-order. /// </summary> /// <param name="value">The value to convert.</param> /// <returns>An array of bytes with length 1.</returns> /// <typeparam name="T">Native value type to get bytes for.</typeparam> /// <exception cref="ArgumentException"><paramref name="value"/> type is not primitive.</exception> /// <exception cref="InvalidOperationException">Cannot get bytes for <paramref name="value"/> type.</exception> public byte[] GetBytes <T>(T value) where T : struct { if (!typeof(T).IsPrimitive) { throw new ArgumentException("Value type is not primitive", "value"); } IConvertible nativeValue = (IConvertible)value; switch (nativeValue.GetTypeCode()) { case TypeCode.Char: return(GetBytes(nativeValue.ToChar(null))); case TypeCode.Boolean: return(GetBytes(nativeValue.ToBoolean(null))); case TypeCode.SByte: return(GetBytes(nativeValue.ToSByte(null))); case TypeCode.Byte: return(GetBytes(nativeValue.ToByte(null))); case TypeCode.Int16: return(GetBytes(nativeValue.ToInt16(null))); case TypeCode.UInt16: return(GetBytes(nativeValue.ToUInt16(null))); case TypeCode.Int32: return(GetBytes(nativeValue.ToInt32(null))); case TypeCode.UInt32: return(GetBytes(nativeValue.ToUInt32(null))); case TypeCode.Int64: return(GetBytes(nativeValue.ToInt64(null))); case TypeCode.UInt64: return(GetBytes(nativeValue.ToUInt64(null))); case TypeCode.Single: return(GetBytes(nativeValue.ToSingle(null))); case TypeCode.Double: return(GetBytes(nativeValue.ToDouble(null))); default: throw new InvalidOperationException("Cannot get bytes for value type " + nativeValue.GetTypeCode()); } }
/// <summary> /// Append a int type (byte, int) as string /// </summary> internal static void AppendIntegerAsString(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; default: sb.Append(XmlHelper.XmlConvertToString(value, objTypeCode)); break; } }
/// <summary> /// can convert any enum based on signed/unsigned integer to long /// </summary> public static long ToInt64(this IConvertible value) { Type type = value.GetType(); if (type.IsEnum) { type = Enum.GetUnderlyingType(type); } if (type.IsSigned()) { return(value.ToInt64(CultureInfo.InvariantCulture)); } else { return((long)value.ToUInt64(CultureInfo.InvariantCulture)); } }
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 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()); } }
/// <summary> /// /// </summary> /// <param name="reader"></param> /// <param name="objectType"></param> /// <param name="existingValue"></param> /// <param name="serializer"></param> /// <returns></returns> public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) { return(0); } else { IConvertible convertible = reader.Value as IConvertible; if (objectType == typeof(System.Int64)) { return(convertible.ToInt64(CultureInfo.InvariantCulture)); } else if (objectType == typeof(System.UInt64)) { return(convertible.ToUInt64(CultureInfo.InvariantCulture)); } return(0); } }
public override void LoadData(BinaryReader reader, StructInstance instance) { int offset = (int)reader.BaseStream.Position; IConvertible value = ReadIntValue(reader, instance); ulong u = value.ToUInt64(CultureInfo.CurrentCulture); StringBuilder displayValue = new StringBuilder(_size * 8); for (int i = _size * 8 - 1; i >= 0; i--) { if ((u & (1ul << i)) != 0) { displayValue.Append("1"); } else { displayValue.Append("0"); } } AddCell(instance, value, displayValue.ToString(), offset); }
public Object EvaluatePostOrPrefix(ref Object v) { int i; uint ui; long l; ulong ul; double d; IConvertible ic = Convert.GetIConvertible(v); switch (Convert.GetTypeCode(v, ic)) { case TypeCode.Empty: v = Double.NaN; return(v); case TypeCode.DBNull: v = 0; return(this.DoOp(0)); case TypeCode.Boolean: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: v = i = ic.ToInt32(null); return(this.DoOp(i)); case TypeCode.Char: i = ic.ToInt32(null); return(((IConvertible)this.DoOp(i)).ToChar(null)); case TypeCode.UInt32: v = ui = ic.ToUInt32(null); return(this.DoOp(ui)); case TypeCode.Int64: v = l = ic.ToInt64(null); return(this.DoOp(l)); case TypeCode.UInt64: v = ul = ic.ToUInt64(null); return(this.DoOp(ul)); case TypeCode.Single: case TypeCode.Double: v = d = ic.ToDouble(null); return(this.DoOp(d)); } MethodInfo oper = this.GetOperator(v.GetType()); if (oper != null) { return(oper.Invoke(null, (BindingFlags)0, JSBinder.ob, new Object[] { v }, null)); } else { v = d = Convert.ToNumber(v, ic); return(this.DoOp(d)); } }
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.ToUInt64(defaultProvider)); default: return(base.ConvertFrom(context, culture, value)); } }
public bool PosTest1() { bool retVal = true; const string c_TEST_ID = "P001"; const string c_TEST_DESC = "PosTest1: Random character."; string errorDesc; UInt64 expectedValue; UInt64 actualValue; char ch; expectedValue = (UInt64)(TestLibrary.Generator.GetInt32(-55) % (UInt16.MaxValue + 1)); ch = (char)expectedValue; TestLibrary.TestFramework.BeginScenario(c_TEST_DESC); try { NumberFormatInfo numberFormat = new NumberFormatInfo(); IFormatProvider provider = numberFormat; IConvertible converter = ch; actualValue = converter.ToUInt64(numberFormat); if (actualValue != expectedValue) { errorDesc = string.Format("UInt64 value of character \\u{0:x} is not ", (int)ch); errorDesc += expectedValue + " as expected: Actual(" + actualValue + ")"; TestLibrary.TestFramework.LogError("001" + " TestId-" + c_TEST_ID, errorDesc); retVal = false; } } catch (Exception e) { errorDesc = "Unexpected exception: " + e; TestLibrary.TestFramework.LogError("002" + " TestId-" + c_TEST_ID, errorDesc); retVal = false; } return(retVal); }
public bool NegTest2() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("NegTest2: The current Int64 value is out of UInt64 range 2"); try { long int64A = (TestLibrary.Generator.GetInt64(-55) + 1) * (-1); IConvertible iConvert = (IConvertible)(int64A); ulong uint64 = iConvert.ToUInt64(null); retVal = false; TestLibrary.TestFramework.LogError("N003", "The current Int64 value is out of UInt64 range but not throw exception"); } catch (OverflowException) { } catch (Exception e) { TestLibrary.TestFramework.LogError("N004", "Unexpected exception: " + e); retVal = false; } return(retVal); }
/// <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; } }
internal static ulong ToUInt64(this Enum value) { IConvertible convertible = value; switch (value.GetTypeCode()) { case TypeCode.SByte: return((byte)convertible.ToSByte(null)); case TypeCode.Int16: return((ushort)convertible.ToInt16(null)); case TypeCode.Int32: return((uint)convertible.ToInt32(null)); case TypeCode.Int64: return((ulong)convertible.ToInt64(null)); default: return(convertible.ToUInt64(null)); } }
/// <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 PosTest3() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest3: Convert a random Uint64 to UInt64"); try { UInt64 uintA = this.GetInt64(1, UInt64.MaxValue);; IConvertible iConvert = (IConvertible)(uintA); UInt64 uint64A = iConvert.ToUInt64(null); if (uint64A != 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 PosTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest1:UInt64 MinValue to UInt64"); try { UInt64 uintA = UInt64.MinValue; IConvertible iConvert = (IConvertible)(uintA); UInt64 uint64A = iConvert.ToUInt64(null); if (uint64A != 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 NegTest1() { bool retVal = true; CultureInfo myculture = new CultureInfo("en-us"); IFormatProvider provider = myculture.NumberFormat; TestLibrary.TestFramework.BeginScenario("NegTest1: The current Int64 value is out of UInt64 range 1"); try { long int64A = Int64.MinValue; IConvertible iConvert = (IConvertible)(int64A); ulong uint64 = iConvert.ToUInt64(provider); retVal = false; TestLibrary.TestFramework.LogError("N001", "The current Int64 value is out of UInt64 range but not throw exception"); } catch (OverflowException) { } catch (Exception e) { TestLibrary.TestFramework.LogError("N002", "Unexpected exception: " + e); retVal = false; } return(retVal); }
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)); }
private static double JScriptCompare2(object v1, object v2, IConvertible ic1, IConvertible ic2, TypeCode t1, TypeCode t2) { double num7; if (t1 == TypeCode.Object) { v1 = Microsoft.JScript.Convert.ToPrimitive(v1, PreferredType.Number, ref ic1); t1 = Microsoft.JScript.Convert.GetTypeCode(v1, ic1); } if (t2 == TypeCode.Object) { v2 = Microsoft.JScript.Convert.ToPrimitive(v2, PreferredType.Number, ref ic2); t2 = Microsoft.JScript.Convert.GetTypeCode(v2, ic2); } switch (t1) { case TypeCode.Char: if (t2 != TypeCode.String) { break; } return (double) string.CompareOrdinal(Microsoft.JScript.Convert.ToString(v1, ic1), ic2.ToString(null)); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: break; case TypeCode.UInt64: { ulong num3 = ic1.ToUInt64(null); switch (t2) { case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: { long num4 = ic2.ToInt64(null); if (num4 < 0L) { return 1.0; } if (num3 == num4) { return 0.0; } return -1.0; } case TypeCode.UInt64: { ulong num5 = ic2.ToUInt64(null); if (num3 < num5) { return -1.0; } if (num3 == num5) { return 0.0; } return 1.0; } case TypeCode.Single: case TypeCode.Double: return (num3 - ic2.ToDouble(null)); case TypeCode.Decimal: return (double) (new decimal(num3) - ic2.ToDecimal(null)); } object obj3 = Microsoft.JScript.Convert.ToNumber(v2, ic2); return JScriptCompare2(v1, obj3, ic1, Microsoft.JScript.Convert.GetIConvertible(obj3), t1, TypeCode.Double); } case TypeCode.Decimal: { decimal num6 = ic1.ToDecimal(null); switch (t2) { case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return (double) (num6 - new decimal(ic2.ToInt64(null))); case TypeCode.UInt64: return (double) (num6 - new decimal(ic2.ToUInt64(null))); case TypeCode.Single: case TypeCode.Double: return (double) (num6 - new decimal(ic2.ToDouble(null))); case TypeCode.Decimal: return (double) (num6 - ic2.ToDecimal(null)); } return (double) (num6 - new decimal(Microsoft.JScript.Convert.ToNumber(v2, ic2))); } case TypeCode.String: { TypeCode code5 = t2; if (code5 == TypeCode.Char) { return (double) string.CompareOrdinal(ic1.ToString(null), Microsoft.JScript.Convert.ToString(v2, ic2)); } if (code5 != TypeCode.String) { goto Label_0355; } return (double) string.CompareOrdinal(ic1.ToString(null), ic2.ToString(null)); } default: goto Label_0355; } long num = ic1.ToInt64(null); switch (t2) { case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return (double) (num - ic2.ToInt64(null)); case TypeCode.UInt64: if (num >= 0L) { ulong num2 = ic2.ToUInt64(null); if (num < num2) { return -1.0; } if (num == num2) { return 0.0; } return 1.0; } return -1.0; case TypeCode.Single: case TypeCode.Double: return (num - ic2.ToDouble(null)); case TypeCode.Decimal: return (double) (new decimal(num) - ic2.ToDecimal(null)); default: { object obj2 = Microsoft.JScript.Convert.ToNumber(v2, ic2); return JScriptCompare2(v1, obj2, ic1, Microsoft.JScript.Convert.GetIConvertible(obj2), t1, TypeCode.Double); } } Label_0355: num7 = Microsoft.JScript.Convert.ToNumber(v1, ic1); double num8 = Microsoft.JScript.Convert.ToNumber(v2, ic2); if (num7 == num8) { return 0.0; } return (num7 - num8); }
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 bool ToBoolean(Object value, IConvertible ic){ switch (Convert.GetTypeCode(value, ic)){ case TypeCode.Empty: return false; case TypeCode.Object: if (value is Missing || value is System.Reflection.Missing) return false; Type t = value.GetType(); MethodInfo meth = t.GetMethod("op_True", BindingFlags.ExactBinding|BindingFlags.Public|BindingFlags.Static, null, new Type[]{t}, null); if (meth != null && (meth.Attributes & MethodAttributes.SpecialName) != 0 && meth.ReturnType == Typeob.Boolean){ meth = new JSMethodInfo(meth); return (bool)meth.Invoke(null, BindingFlags.SuppressChangeType, null, new Object[]{value}, null); } return true; case TypeCode.DBNull: return false; case TypeCode.Boolean: return ic.ToBoolean(null); case TypeCode.Char: return ic.ToChar(null) != (Char)0; case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: return ic.ToInt32(null) != 0; case TypeCode.UInt32: case TypeCode.Int64: return ic.ToInt64(null) != 0; case TypeCode.UInt64: return ic.ToUInt64(null) != 0; case TypeCode.Single: case TypeCode.Double: double d = ic.ToDouble(null); if (d != d) return false; else return d != 0; case TypeCode.Decimal: return ic.ToDecimal(null) != (Decimal)0; case TypeCode.DateTime: return true; case TypeCode.String: return ic.ToString(null).Length != 0; } return false; //should never get here }
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); }
public static Literal DoOr(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.UInt32: if (i >= 0){ val = ((uint)i) | ic2.ToUInt32(null); type = SystemTypes.UInt32; break; } goto case TypeCode.Int64; case TypeCode.Int64: long lng = i; val = lng | ic2.ToInt64(null); type = SystemTypes.Int64; break; case TypeCode.UInt64: ulong ulng = (ulong) i; val = ulng | ic2.ToUInt64(null); type = SystemTypes.UInt64; break; case TypeCode.Single: case TypeCode.Double: case TypeCode.Decimal: return null; default: return null; } break; case TypeCode.Byte: case TypeCode.Char: case TypeCode.UInt16: ushort us = ic1.ToUInt16(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Char: val = ((int)us) | ic2.ToInt32(null); type = SystemTypes.Int32; break; case TypeCode.UInt32: val = ((uint)us) | ic2.ToUInt32(null); type = SystemTypes.UInt32; break; case TypeCode.Int64: val = ((long)us) | ic2.ToInt64(null); type = SystemTypes.Int64; break; case TypeCode.UInt64: val = ((ulong)us) | ic2.ToUInt64(null); type = SystemTypes.UInt64; break; case TypeCode.Single: case TypeCode.Double: case TypeCode.Decimal: return null; default: return null; } break; case TypeCode.UInt32: uint ui = ic1.ToUInt32(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: i = ic2.ToInt32(null); if (i >= 0){ val = ui | (uint)i; type = SystemTypes.UInt32; break; } goto case TypeCode.Int64; case TypeCode.Int64: val = ((long)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 = ((ulong)ui) | ic2.ToUInt64(null); type = SystemTypes.UInt64; break; case TypeCode.Single: case TypeCode.Double: case TypeCode.Decimal: return null; 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: case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.UInt32: val = l | ic2.ToInt64(null); type = SystemTypes.Int64; break; case TypeCode.UInt64: val = ((ulong)l) | ic2.ToUInt64(null); type = SystemTypes.UInt64; break; case TypeCode.Single: case TypeCode.Double: case TypeCode.Decimal: return null; 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: case TypeCode.Int64: case TypeCode.UInt64: val = ul | ic2.ToUInt64(null); type = SystemTypes.UInt64; break; case TypeCode.Single: case TypeCode.Double: case TypeCode.Decimal: return null; default: return null; } break; case TypeCode.Single: case TypeCode.Double: case TypeCode.Decimal: return null; default: return null; } return new Literal(val, type, binaryExpression.SourceContext); }
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)); }
static int Diff(IConvertible val1, IConvertible val2) { ulong diff; switch (val1.GetTypeCode()) { case TypeCode.UInt64: diff = val2.ToUInt64(null) - val1.ToUInt64(null); break; case TypeCode.Int64: diff = (ulong)(val2.ToInt64(null) - val1.ToInt64(null)); break; case TypeCode.UInt32: diff = val2.ToUInt32(null) - val1.ToUInt32(null); break; default: diff = (ulong)(val2.ToInt32(null) - val1.ToInt32(null)); break; } if (diff >= int.MaxValue) return int.MaxValue; else return (int)diff; }
internal static double ToNumber(object value, IConvertible ic) { switch (GetTypeCode(value, ic)) { case TypeCode.Empty: return double.NaN; case TypeCode.Object: case TypeCode.DateTime: { object obj2 = ToPrimitive(value, PreferredType.Number, ref ic); if (obj2 == value) { return double.NaN; } return ToNumber(obj2, ic); } case TypeCode.DBNull: return 0.0; case TypeCode.Boolean: return (ic.ToBoolean(null) ? ((double) 1) : ((double) 0)); case TypeCode.Char: return (double) ic.ToChar(null); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: return (double) ic.ToInt32(null); case TypeCode.UInt32: case TypeCode.Int64: return (double) ic.ToInt64(null); case TypeCode.UInt64: return (double) ic.ToUInt64(null); case TypeCode.Single: case TypeCode.Double: case TypeCode.Decimal: return ic.ToDouble(null); case TypeCode.String: return ToNumber(ic.ToString(null)); } return 0.0; }
void EmitValue(IConvertible val) { switch (val.GetTypeCode()) { case TypeCode.UInt64: g.EmitI8Helper(unchecked((long)val.ToUInt64(null)), false); break; case TypeCode.Int64: g.EmitI8Helper(val.ToInt64(null), true); break; case TypeCode.UInt32: g.EmitI4Helper(unchecked((int)val.ToUInt64(null))); break; default: g.EmitI4Helper(val.ToInt32(null)); break; } }
private void EmitConstant(IConvertible ic) { this.StackSize++; TypeCode tc = ic.GetTypeCode(); switch (tc) { case TypeCode.Boolean: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Char: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: long n = ic.ToInt64(null); switch (n) { case -1: this.generator.Emit(OperationCode.Ldc_I4_M1); break; case 0: this.generator.Emit(OperationCode.Ldc_I4_0); break; case 1: this.generator.Emit(OperationCode.Ldc_I4_1); break; case 2: this.generator.Emit(OperationCode.Ldc_I4_2); break; case 3: this.generator.Emit(OperationCode.Ldc_I4_3); break; case 4: this.generator.Emit(OperationCode.Ldc_I4_4); break; case 5: this.generator.Emit(OperationCode.Ldc_I4_5); break; case 6: this.generator.Emit(OperationCode.Ldc_I4_6); break; case 7: this.generator.Emit(OperationCode.Ldc_I4_7); break; case 8: this.generator.Emit(OperationCode.Ldc_I4_8); break; default: if (sbyte.MinValue <= n && n <= sbyte.MaxValue) { this.generator.Emit(OperationCode.Ldc_I4_S, (sbyte)n); } else if (int.MinValue <= n && n <= int.MaxValue || n <= uint.MaxValue && (tc == TypeCode.Char || tc == TypeCode.UInt16 || tc == TypeCode.UInt32)) { if (n == uint.MaxValue) this.generator.Emit(OperationCode.Ldc_I4_M1); else this.generator.Emit(OperationCode.Ldc_I4, (int)n); } else { this.generator.Emit(OperationCode.Ldc_I8, n); tc = TypeCode.Empty; //Suppress conversion to long } break; } if (tc == TypeCode.Int64) this.generator.Emit(OperationCode.Conv_I8); return; case TypeCode.UInt64: this.generator.Emit(OperationCode.Ldc_I8, (long)ic.ToUInt64(null)); return; case TypeCode.Single: this.generator.Emit(OperationCode.Ldc_R4, ic.ToSingle(null)); return; case TypeCode.Double: this.generator.Emit(OperationCode.Ldc_R8, ic.ToDouble(null)); return; case TypeCode.String: this.generator.Emit(OperationCode.Ldstr, ic.ToString(null)); return; case TypeCode.Decimal: var bits = Decimal.GetBits(ic.ToDecimal(null)); this.generator.Emit(OperationCode.Ldc_I4, bits[0]); this.generator.Emit(OperationCode.Ldc_I4, bits[1]); this.StackSize++; this.generator.Emit(OperationCode.Ldc_I4, bits[2]); this.StackSize++; if (bits[3] >= 0) this.generator.Emit(OperationCode.Ldc_I4_0); else this.generator.Emit(OperationCode.Ldc_I4_1); this.StackSize++; int scale = (bits[3]&0x7FFFFF)>>16; if (scale > 28) scale = 28; this.generator.Emit(OperationCode.Ldc_I4_S, scale); this.StackSize++; this.generator.Emit(OperationCode.Newobj, this.DecimalConstructor); this.StackSize -= 4; return; } }
internal static 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 Literal DoLe(IConvertible ic1, IConvertible ic2, TypeCode code1, TypeCode code2, BinaryExpression binaryExpression){ TypeNode type = SystemTypes.Boolean; 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); break; case TypeCode.Int64: case TypeCode.UInt32: val = ((long)i) <= ic2.ToInt64(null); break; case TypeCode.UInt64: val = ((ulong)i) <= ic2.ToUInt64(null); break; case TypeCode.Single: case TypeCode.Double: case TypeCode.Decimal: return null; default: return null; } break; case TypeCode.Byte: case TypeCode.UInt16: ushort us = ic1.ToUInt16(null); switch(code2){ case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.Int32: val = ((int)us) <= ic2.ToInt32(null); break; case TypeCode.UInt32: val = ((uint)us) <= ic2.ToUInt32(null); break; case TypeCode.Int64: val = ((long)us) <= ic2.ToInt64(null); break; case TypeCode.UInt64: val = ((ulong)us) <= ic2.ToUInt64(null); break; case TypeCode.Single: case TypeCode.Double: case TypeCode.Decimal: return null; 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 = ((long)ui) <= ic2.ToInt64(null); break; case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.UInt32: val = ui <= ic2.ToUInt32(null); break; case TypeCode.UInt64: val = ((ulong)ui) <= ic2.ToUInt64(null); break; case TypeCode.Single: case TypeCode.Double: case TypeCode.Decimal: return null; 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: case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.UInt32: val = l <= ic2.ToInt64(null); break; case TypeCode.UInt64: val = ((ulong)l) <= ic2.ToUInt64(null); break; case TypeCode.Single: case TypeCode.Double: case TypeCode.Decimal: return null; 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: case TypeCode.Int64: case TypeCode.UInt64: val = ul <= ic2.ToUInt64(null); break; case TypeCode.Single: case TypeCode.Double: case TypeCode.Decimal: return null; default: return null; } break; case TypeCode.Single: case TypeCode.Double: case TypeCode.Decimal: return null; default: return null; } return new Literal(val, type, binaryExpression.SourceContext); }
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 Literal DoRightShift(IConvertible ic1, IConvertible ic2, TypeCode code1, TypeCode code2, BinaryExpression binaryExpression){ TypeNode type = SystemTypes.Object; object val = null; int shift = 0; switch (code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Byte: case TypeCode.UInt16: shift = ic2.ToInt32(null) & 0x3f; break; case TypeCode.Int64: shift = ((int)ic2.ToInt64(null)) & 0x3f; break; case TypeCode.UInt32: case TypeCode.UInt64: shift = ((int)ic2.ToUInt64(null)) & 0x3f; break; default: return null; } switch(code1){ case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.Int32: int i = ic1.ToInt32(null); val = i >> shift; type = SystemTypes.Int32; break; case TypeCode.UInt32: uint ui = ic1.ToUInt32(null); val = ui >> shift; type = SystemTypes.UInt32; break; case TypeCode.Int64: long l = ic1.ToInt64(null); val = l >> shift; type = SystemTypes.Int64; break; case TypeCode.UInt64: ulong ul = ic1.ToUInt64(null); val = ul >> shift; type = SystemTypes.UInt64; break; default: return null; } return new Literal(val, type, binaryExpression.SourceContext); }
private object ExecuteBinaryOperator(IConvertible left, IConvertible right, CodeBinaryOperatorType op) { TypeCode typeCode = left.GetTypeCode(); TypeCode code2 = right.GetTypeCode(); TypeCode[] codeArray = new TypeCode[] { TypeCode.Byte, TypeCode.Char, TypeCode.Int16, TypeCode.UInt16, TypeCode.Int32, TypeCode.UInt32, TypeCode.Int64, TypeCode.UInt64 }; int num = -1; int num2 = -1; for (int i = 0; i < codeArray.Length; i++) { if (typeCode == codeArray[i]) { num = i; } if (code2 == codeArray[i]) { num2 = i; } if ((num != -1) && (num2 != -1)) { break; } } if ((num == -1) || (num2 == -1)) { return left; } int index = Math.Max(num, num2); object obj2 = left; switch (codeArray[index]) { case TypeCode.Char: { char ch = left.ToChar(null); char ch2 = right.ToChar(null); if (op != CodeBinaryOperatorType.BitwiseOr) { obj2 = ch & ch2; break; } obj2 = ch | ch2; break; } case TypeCode.Byte: { byte num5 = left.ToByte(null); byte num6 = right.ToByte(null); if (op != CodeBinaryOperatorType.BitwiseOr) { obj2 = num5 & num6; break; } obj2 = num5 | num6; break; } case TypeCode.Int16: { short num7 = left.ToInt16(null); short num8 = right.ToInt16(null); if (op != CodeBinaryOperatorType.BitwiseOr) { obj2 = num7 & num8; break; } obj2 = (short) (((ushort) num7) | ((ushort) num8)); break; } case TypeCode.UInt16: { ushort num9 = left.ToUInt16(null); ushort num10 = right.ToUInt16(null); if (op != CodeBinaryOperatorType.BitwiseOr) { obj2 = num9 & num10; break; } obj2 = num9 | num10; break; } case TypeCode.Int32: { int num11 = left.ToInt32(null); int num12 = right.ToInt32(null); if (op != CodeBinaryOperatorType.BitwiseOr) { obj2 = num11 & num12; break; } obj2 = num11 | num12; break; } case TypeCode.UInt32: { uint num13 = left.ToUInt32(null); uint num14 = right.ToUInt32(null); if (op != CodeBinaryOperatorType.BitwiseOr) { obj2 = num13 & num14; break; } obj2 = num13 | num14; break; } case TypeCode.Int64: { long num15 = left.ToInt64(null); long num16 = right.ToInt64(null); if (op != CodeBinaryOperatorType.BitwiseOr) { obj2 = num15 & num16; break; } obj2 = num15 | num16; break; } case TypeCode.UInt64: { ulong num17 = left.ToUInt64(null); ulong num18 = right.ToUInt64(null); if (op != CodeBinaryOperatorType.BitwiseOr) { obj2 = num17 & num18; break; } obj2 = num17 | num18; break; } } if ((obj2 != left) && (left is Enum)) { obj2 = Enum.ToObject(left.GetType(), obj2); } return obj2; }
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); }
private static double JScriptCompare2(Object v1, Object v2, IConvertible ic1, IConvertible ic2, TypeCode t1, TypeCode t2){ if (t1 == TypeCode.Object){ v1 = Convert.ToPrimitive(v1, PreferredType.Number, ref ic1); t1 = Convert.GetTypeCode(v1, ic1); } if (t2 == TypeCode.Object){ v2 = Convert.ToPrimitive(v2, PreferredType.Number, ref ic2); t2 = Convert.GetTypeCode(v2, ic2); } switch (t1){ case TypeCode.Char: if (t2 == TypeCode.String) return String.CompareOrdinal(Convert.ToString(v1, ic1), ic2.ToString(null)); goto case TypeCode.UInt16; case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: long l = ic1.ToInt64(null); switch (t2){ case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return l - ic2.ToInt64(null); case TypeCode.UInt64: if (l < 0) return -1; ulong ul2 = ic2.ToUInt64(null); if (((ulong)l) < ul2) return -1; if (((ulong)l) == ul2) return 0; return 1; case TypeCode.Single: case TypeCode.Double: return ((double)l) - ic2.ToDouble(null); case TypeCode.Decimal: return (double)(new Decimal(l) - ic2.ToDecimal(null)); default: Object bd2 = Convert.ToNumber(v2, ic2); return JScriptCompare2(v1, bd2, ic1, Convert.GetIConvertible(bd2), t1, TypeCode.Double); } case TypeCode.UInt64: ulong ul = ic1.ToUInt64(null); switch (t2){ case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: long l2 = ic2.ToInt64(null); if (l2 < 0) return 1; if (ul == (ulong)l2) return 0; return -1; case TypeCode.UInt64: ulong ul2 = ic2.ToUInt64(null); if (ul < ul2) return -1; if (ul == ul2) return 0; return 1; case TypeCode.Single: case TypeCode.Double: return ((double)ul) - ic2.ToDouble(null); case TypeCode.Decimal: return (double)(new Decimal(ul) - ic2.ToDecimal(null)); default: Object bd2 = Convert.ToNumber(v2, ic2); return JScriptCompare2(v1, bd2, ic1, Convert.GetIConvertible(bd2), t1, TypeCode.Double); } case TypeCode.Decimal: Decimal dec1 = ic1.ToDecimal(null); switch (t2){ case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return (double)(dec1 - new Decimal(ic2.ToInt64(null))); case TypeCode.UInt64: return (double)(dec1 - new Decimal(ic2.ToUInt64(null))); case TypeCode.Single: case TypeCode.Double: return (double)(dec1 - new Decimal(ic2.ToDouble(null))); case TypeCode.Decimal: return (double)(dec1 - ic2.ToDecimal(null)); default: return (double)(dec1 - new Decimal(Convert.ToNumber(v2, ic2))); } case TypeCode.String: switch (t2){ case TypeCode.String: return String.CompareOrdinal(ic1.ToString(null), ic2.ToString(null)); case TypeCode.Char : return String.CompareOrdinal(ic1.ToString(null), Convert.ToString(v2, ic2)); } goto default; default: double d1 = Convert.ToNumber(v1, ic1); double d2 = Convert.ToNumber(v2, ic2); if (d1 == d2) return 0; //d1 and d2 could be infinities return d1 - d2; } }
internal static uint ToUint32(object value, IConvertible ic) { switch (GetTypeCode(value, ic)) { case TypeCode.Empty: return 0; case TypeCode.Object: case TypeCode.DateTime: { object obj2 = ToPrimitive(value, PreferredType.Number, ref ic); if (obj2 == value) { return 0; } return ToUint32(obj2, ic); } case TypeCode.DBNull: return 0; case TypeCode.Boolean: if (ic.ToBoolean(null)) { return 1; } return 0; case TypeCode.Char: return ic.ToChar(null); case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: return (uint) ic.ToInt64(null); case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.UInt32: return ic.ToUInt32(null); case TypeCode.UInt64: return (uint) ic.ToUInt64(null); case TypeCode.Single: case TypeCode.Double: return (uint) Runtime.DoubleToInt64(ic.ToDouble(null)); case TypeCode.Decimal: return (uint) Runtime.UncheckedDecimalToInt64(ic.ToDecimal(null)); case TypeCode.String: return (uint) Runtime.DoubleToInt64(ToNumber(ic.ToString(null))); } return 0; }
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 String ToString(Object value, PreferredType pref, IConvertible ic, bool explicitOK){ Enum e = value as Enum; if (e != null) return e.ToString("G"); EnumWrapper ew = value as EnumWrapper; if (ew != null) return ew.ToString(); TypeCode code = Convert.GetTypeCode(value, ic); if (pref == PreferredType.LocaleString){ switch (code){ case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Single: case TypeCode.Double: { double d = ic.ToDouble(null); return d.ToString(d <= -1e+15 || d >= 1e+15 ? "g" : "n", NumberFormatInfo.CurrentInfo); } case TypeCode.Int64: return ic.ToInt64(null).ToString("n", NumberFormatInfo.CurrentInfo); case TypeCode.UInt64: return ic.ToUInt64(null).ToString("n", NumberFormatInfo.CurrentInfo); case TypeCode.Decimal: return ic.ToDecimal(null).ToString("n", NumberFormatInfo.CurrentInfo); } } switch (code){ case TypeCode.Empty: return explicitOK ? "undefined" : null; case TypeCode.Object: return Convert.ToString(Convert.ToPrimitive(value, pref, ref ic), ic); case TypeCode.DBNull: return explicitOK ? "null" : null; case TypeCode.Boolean: return ic.ToBoolean(null) ? "true" : "false"; case TypeCode.Char: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: case TypeCode.UInt64: case TypeCode.Decimal: case TypeCode.String: return ic.ToString(null); case TypeCode.DateTime: return Convert.ToString(DateConstructor.ob.Construct(ic.ToDateTime(null))); case TypeCode.Single: case TypeCode.Double: return Convert.ToString(ic.ToDouble(null)); } return null; //Should never get here }
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 string ToString(object value, PreferredType pref, IConvertible ic, bool explicitOK) { Enum enum2 = value as Enum; if (enum2 != 0) { return enum2.ToString("G"); } EnumWrapper wrapper = value as EnumWrapper; if (wrapper != null) { return wrapper.ToString(); } TypeCode typeCode = GetTypeCode(value, ic); if (pref == PreferredType.LocaleString) { switch (typeCode) { case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Single: case TypeCode.Double: { double num = ic.ToDouble(null); return num.ToString(((num <= -1E+15) || (num >= 1E+15)) ? "g" : "n", NumberFormatInfo.CurrentInfo); } case TypeCode.Int64: return ic.ToInt64(null).ToString("n", NumberFormatInfo.CurrentInfo); case TypeCode.UInt64: return ic.ToUInt64(null).ToString("n", NumberFormatInfo.CurrentInfo); case TypeCode.Decimal: return ic.ToDecimal(null).ToString("n", NumberFormatInfo.CurrentInfo); } } switch (typeCode) { case TypeCode.Empty: if (explicitOK) { return "undefined"; } return null; case TypeCode.Object: return ToString(ToPrimitive(value, pref, ref ic), ic); case TypeCode.DBNull: if (explicitOK) { return "null"; } return null; case TypeCode.Boolean: if (ic.ToBoolean(null)) { return "true"; } return "false"; case TypeCode.Char: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: case TypeCode.UInt64: case TypeCode.Decimal: case TypeCode.String: return ic.ToString(null); case TypeCode.Single: case TypeCode.Double: return ToString(ic.ToDouble(null)); case TypeCode.DateTime: return ToString(DateConstructor.ob.Construct(ic.ToDateTime(null))); } return null; }
internal static Object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) { Contract.Requires(value != null, "[Convert.DefaultToType]value!=null"); if (targetType==null) { throw new ArgumentNullException("targetType"); } Contract.EndContractBlock(); RuntimeType rtTargetType = targetType as RuntimeType; if (rtTargetType != null) { if (value.GetType() == targetType) { return value; } if (rtTargetType == ConvertTypes[(int)TypeCode.Boolean]) return value.ToBoolean(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Char]) return value.ToChar(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.SByte]) return value.ToSByte(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Byte]) return value.ToByte(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Int16]) return value.ToInt16(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.UInt16]) return value.ToUInt16(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Int32]) return value.ToInt32(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.UInt32]) return value.ToUInt32(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Int64]) return value.ToInt64(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.UInt64]) return value.ToUInt64(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Single]) return value.ToSingle(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Double]) return value.ToDouble(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Decimal]) return value.ToDecimal(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.DateTime]) return value.ToDateTime(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.String]) return value.ToString(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Object]) return (Object)value; // Need to special case Enum because typecode will be underlying type, e.g. Int32 if (rtTargetType == EnumType) return (Enum)value; if (rtTargetType == ConvertTypes[(int)TypeCode.DBNull]) throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull")); if (rtTargetType == ConvertTypes[(int)TypeCode.Empty]) throw new InvalidCastException(Environment.GetResourceString("InvalidCast_Empty")); } throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", value.GetType().FullName, targetType.FullName)); }
internal static bool ToBoolean(object value, IConvertible ic) { switch (GetTypeCode(value, ic)) { case TypeCode.Empty: return false; case TypeCode.Object: { if ((value is Microsoft.JScript.Missing) || (value is System.Reflection.Missing)) { return false; } Type type = value.GetType(); MethodInfo method = type.GetMethod("op_True", BindingFlags.ExactBinding | BindingFlags.Public | BindingFlags.Static, null, new Type[] { type }, null); if (((method != null) && ((method.Attributes & MethodAttributes.SpecialName) != MethodAttributes.PrivateScope)) && (method.ReturnType == typeof(bool))) { method = new JSMethodInfo(method); return (bool) method.Invoke(null, BindingFlags.SuppressChangeType, null, new object[] { value }, null); } return true; } case TypeCode.DBNull: return false; case TypeCode.Boolean: return ic.ToBoolean(null); case TypeCode.Char: return (ic.ToChar(null) != '\0'); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: return (ic.ToInt32(null) != 0); case TypeCode.UInt32: case TypeCode.Int64: return (ic.ToInt64(null) != 0L); case TypeCode.UInt64: return (ic.ToUInt64(null) != 0L); case TypeCode.Single: case TypeCode.Double: { double num = ic.ToDouble(null); return ((num == num) && !(num == 0.0)); } case TypeCode.Decimal: return (ic.ToDecimal(null) != 0M); case TypeCode.DateTime: return true; case TypeCode.String: return (ic.ToString(null).Length != 0); } return false; }
// 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"); } }