public bool PosTest1() { bool retVal = true; // Add your scenario description here TestLibrary.TestFramework.BeginScenario("PosTest1: Convert a random Int16 to UInt16"); try { Int16 i1 = TestLibrary.Generator.GetInt16(-55); IConvertible Icon1 = (IConvertible)i1; if (Icon1.ToUInt16(null) != i1) { TestLibrary.TestFramework.LogError("001", "The result is not the value as expected."); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("002", "Unexpected exception: " + e); retVal = false; } return(retVal); }
public bool NegTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("NegTest1: Test the negative int16 "); try { Int16 i1 = TestLibrary.Generator.GetInt16(-55); IConvertible Icon1 = (IConvertible)(-i1); ushort s1 = Icon1.ToUInt16(null); TestLibrary.TestFramework.LogError("101", "The overflow exception was not thrown as expected: "); retVal = false; } catch (OverflowException) { } catch (Exception e) { TestLibrary.TestFramework.LogError("102", "Unexpected exception: " + e); retVal = false; } return(retVal); }
public bool PosTest3() { bool retVal = true; // Add your scenario description here TestLibrary.TestFramework.BeginScenario("PosTest3: Check the boundary value "); try { Int16 i1 = Int16.MaxValue; IConvertible Icon1 = (IConvertible)i1; if (Icon1.ToUInt16(null) != Int16.MaxValue) { TestLibrary.TestFramework.LogError("005", "The result is not the value as expected."); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("006", "Unexpected exception: " + e); retVal = false; } return(retVal); }
private object PromoteNumericArgument(Type type, object argument) { IConvertible convertible = (IConvertible)argument; switch (Type.GetTypeCode(type)) { case TypeCode.Byte: return(convertible.ToByte(null)); case TypeCode.SByte: return(convertible.ToSByte(null)); case TypeCode.Int16: return(convertible.ToInt16(null)); case TypeCode.Int32: return(convertible.ToInt32(null)); case TypeCode.Int64: return(convertible.ToInt64(null)); case TypeCode.UInt16: return(convertible.ToUInt16(null)); case TypeCode.UInt32: return(convertible.ToUInt32(null)); case TypeCode.UInt64: return(convertible.ToUInt64(null)); case TypeCode.Single: return(convertible.ToSingle(null)); case TypeCode.Double: return(convertible.ToDouble(null)); case TypeCode.Boolean: return(convertible.ToBoolean(null)); case TypeCode.Decimal: return(convertible.ToDecimal(null)); case TypeCode.Char: return(convertible.ToChar(null)); } throw new ArgumentException(); }
public static bool IsIntegralMinusOne(ICompileTimeConstant constExpression) { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.SByte: return(ic.ToSByte(null) == -1); case System.TypeCode.Int16: return(ic.ToInt16(null) == -1); case System.TypeCode.Int32: return(ic.ToInt32(null) == -1); case System.TypeCode.Int64: return(ic.ToInt64(null) == -1); case System.TypeCode.Byte: return(ic.ToByte(null) == byte.MaxValue); case System.TypeCode.UInt16: return(ic.ToUInt16(null) == ushort.MaxValue); case System.TypeCode.UInt32: return(ic.ToUInt32(null) == uint.MaxValue); case System.TypeCode.UInt64: return(ic.ToUInt64(null) == ulong.MaxValue); } return(false); }
public bool PosTest3() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest3: Convert an enum of Uint16.maxvalue to uint16"); try { color c1 = color.white; IConvertible i1 = c1 as IConvertible; UInt16 u1 = i1.ToUInt16(null); if (u1 != UInt16.MaxValue) { TestLibrary.TestFramework.LogError("005", "The result is not the value as expected"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("006", "Unexpected exception: " + e); retVal = false; } return(retVal); }
public bool PosTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("PosTest1: Convert an enum of zero to Uint16"); try { color c1 = color.blue; IConvertible i1 = c1 as IConvertible; UInt16 u1 = i1.ToUInt16(null); if (u1 != 0) { TestLibrary.TestFramework.LogError("001", "The result is not the value as expected"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("002", "Unexpected exception: " + e); retVal = false; } return(retVal); }
public void ValuesAreConvertible() { RedisValue val = 123; object o = val; byte[] blob = (byte[])Convert.ChangeType(o, typeof(byte[])); Assert.AreEqual(3, blob.Length); Assert.AreEqual((byte)'1', blob[0]); Assert.AreEqual((byte)'2', blob[1]); Assert.AreEqual((byte)'3', blob[2]); Assert.AreEqual((double)123, Convert.ToDouble(o)); IConvertible c = (IConvertible)o; Assert.AreEqual((short)123, c.ToInt16(CultureInfo.InvariantCulture)); Assert.AreEqual((int)123, c.ToInt32(CultureInfo.InvariantCulture)); Assert.AreEqual((long)123, c.ToInt64(CultureInfo.InvariantCulture)); Assert.AreEqual((float)123, c.ToSingle(CultureInfo.InvariantCulture)); Assert.AreEqual("123", c.ToString(CultureInfo.InvariantCulture)); Assert.AreEqual((double)123, c.ToDouble(CultureInfo.InvariantCulture)); Assert.AreEqual((decimal)123, c.ToDecimal(CultureInfo.InvariantCulture)); Assert.AreEqual((ushort)123, c.ToUInt16(CultureInfo.InvariantCulture)); Assert.AreEqual((uint)123, c.ToUInt32(CultureInfo.InvariantCulture)); Assert.AreEqual((ulong)123, c.ToUInt64(CultureInfo.InvariantCulture)); blob = (byte[])c.ToType(typeof(byte[]), CultureInfo.InvariantCulture); Assert.AreEqual(3, blob.Length); Assert.AreEqual((byte)'1', blob[0]); Assert.AreEqual((byte)'2', blob[1]); Assert.AreEqual((byte)'3', blob[2]); }
public bool PosTest1() { bool retVal = true; CultureInfo myculture = new CultureInfo("en-us"); IFormatProvider provider = myculture.NumberFormat; TestLibrary.TestFramework.BeginScenario("PosTest1:The sbyte MaxValue IConvertible To UInt16"); try { sbyte sbyteVal = sbyte.MaxValue; IConvertible iConvert = (IConvertible)(sbyteVal); UInt16 UInt16Val = iConvert.ToUInt16(provider); if (UInt16Val != 127) { TestLibrary.TestFramework.LogError("001", "the ActualResult is not the ExpectResult"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("002", "Unexpected exception: " + e); retVal = false; } return(retVal); }
public void Substract(IConvertible value) { if (typeof(T) == typeof(byte)) { Value = (T)(object)(Value.ToByte(null) - value.ToByte(null)); } if (typeof(T) == typeof(sbyte)) { Value = (T)(object)(Value.ToSByte(null) - value.ToSByte(null)); } if (typeof(T) == typeof(short)) { Value = (T)(object)(Value.ToInt16(null) - value.ToInt16(null)); } if (typeof(T) == typeof(ushort)) { Value = (T)(object)(Value.ToUInt16(null) - value.ToUInt16(null)); } if (typeof(T) == typeof(int)) { Value = (T)(object)(Value.ToInt32(null) - value.ToInt32(null)); } if (typeof(T) == typeof(uint)) { Value = (T)(object)(Value.ToUInt32(null) - value.ToUInt32(null)); } if (typeof(T) == typeof(long)) { Value = (T)(object)(Value.ToInt64(null) - value.ToInt64(null)); } if (typeof(T) == typeof(ulong)) { Value = (T)(object)(Value.ToUInt64(null) - value.ToUInt64(null)); } }
public bool NegTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("NegTest1: Test the overflowException "); try { Int32 i1 = 0; while (i1 <= c_UINT16_MAXVALUE) { i1 = TestLibrary.Generator.GetInt32(-55); } IConvertible Icon1 = (IConvertible)i1; ushort s1 = Icon1.ToUInt16(null); TestLibrary.TestFramework.LogError("101", "The overflow exception was not thrown as expected: "); retVal = false; } catch (OverflowException) { } catch (Exception e) { TestLibrary.TestFramework.LogError("102", "Unexpected exception: " + e); retVal = false; } return(retVal); }
public static bool IsNumericZero(ICompileTimeConstant constExpression) { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.SByte: return(ic.ToSByte(null) == 0); case System.TypeCode.Int16: return(ic.ToInt16(null) == 0); case System.TypeCode.Int32: return(ic.ToInt32(null) == 0); case System.TypeCode.Int64: return(ic.ToInt64(null) == 0); case System.TypeCode.Byte: return(ic.ToByte(null) == 0); case System.TypeCode.UInt16: return(ic.ToUInt16(null) == 0); case System.TypeCode.UInt32: return(ic.ToUInt32(null) == 0); case System.TypeCode.UInt64: return(ic.ToUInt64(null) == 0); case System.TypeCode.Single: return(ic.ToSingle(null) == 0); case System.TypeCode.Double: return(ic.ToDouble(null) == 0); case System.TypeCode.Decimal: return(ic.ToDecimal(null) == 0); } return(false); }
public bool NegTest1() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("NegTest1: Convert an enum of negative value to Uint16"); try { e_test e1 = e_test.itemA; IConvertible i1 = e1 as IConvertible; UInt16 u1 = i1.ToUInt16(null); TestLibrary.TestFramework.LogError("101", "The OverflowException was not thrown as expected"); retVal = false; } catch (OverflowException) { } catch (Exception e) { TestLibrary.TestFramework.LogError("102", "Unexpected exception: " + e); retVal = false; } return(retVal); }
/////////////////////////////////////////////////////////////////////////////////////////////// public static bool ToUnsignedNarrowInteger(Number number, ref ushort value) { bool result = false; try { if (number != null) { if (number.IsUnsignedNarrowInteger()) { value = (ushort)number.Value; result = true; } else { IConvertible convertible = number.Value as IConvertible; if (convertible != null) { value = convertible.ToUInt16(null); result = true; } } } } catch { // do nothing. } return(result); }
private static bool TryConvert(object input, IFormatProvider provider, out ushort value) { if (IsNullOrEmptyString(input)) { value = 0; return(false); } if (!(input is string)) { IConvertible ic = input as IConvertible; if (ic != null) { try { value = ic.ToUInt16(provider); return(true); } catch { } } } NumberStyles styles = NumberStyles.Integer; string s = Convert.ToString(input, provider); if (NormalizeHexString(ref s)) { styles |= NumberStyles.AllowHexSpecifier; } return(ushort.TryParse(s, styles, provider, out value)); }
public bool NegTest2() { bool retVal = true; TestLibrary.TestFramework.BeginScenario("NegTest2: Convert an enum of the value which is bigger than uint16.maxvalue to Uint16"); try { e_test e1 = e_test.itemB; IConvertible i1 = e1 as IConvertible; UInt16 u1 = i1.ToUInt16(null); TestLibrary.TestFramework.LogError("103", "The OverflowException was not thrown as expected"); retVal = false; } catch (OverflowException) { } catch (Exception e) { TestLibrary.TestFramework.LogError("104", "Unexpected exception: " + e); TestLibrary.TestFramework.LogInformation(e.StackTrace); retVal = false; } return(retVal); }
public bool PosTest3() { bool retVal = true; CultureInfo myculture = new CultureInfo("en-us"); IFormatProvider provider = myculture.NumberFormat; TestLibrary.TestFramework.BeginScenario("PosTest3:The random sbyte IConvertible To UInt16 2"); try { sbyte sbyteVal = (sbyte)(this.GetInt32(0, 128)); IConvertible iConvert = (IConvertible)(sbyteVal); UInt16 UInt16Val = iConvert.ToUInt16(provider); if (UInt16Val != (UInt16)(sbyteVal)) { TestLibrary.TestFramework.LogError("005", "the ActualResult is not the ExpectResult"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("006", "Unexpected exception: " + e); retVal = false; } return(retVal); }
public static bool IsIntegralZero(ICompileTimeConstant constExpression) { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.SByte: return(ic.ToSByte(null) == 0); case System.TypeCode.Int16: return(ic.ToInt16(null) == 0); case System.TypeCode.Int32: return(ic.ToInt32(null) == 0); case System.TypeCode.Int64: return(ic.ToInt64(null) == 0); case System.TypeCode.Byte: return(ic.ToByte(null) == 0); case System.TypeCode.UInt16: return(ic.ToUInt16(null) == 0); case System.TypeCode.UInt32: return(ic.ToUInt32(null) == 0); case System.TypeCode.UInt64: return(ic.ToUInt64(null) == 0); case System.TypeCode.Boolean: return(!ic.ToBoolean(null)); } return(false); }
public bool PosTest2() { bool retVal = true; CultureInfo myculture = new CultureInfo("el-GR"); IFormatProvider provider = myculture.NumberFormat; TestLibrary.TestFramework.BeginScenario("PosTest2:The Int64 value which in the range of UInt16 IConvertible To UInt16 2"); try { long int64A = this.GetInt32(0, UInt16.MaxValue); IConvertible iConvert = (IConvertible)(int64A); ushort uint16A = iConvert.ToUInt16(provider); if (uint16A != int64A) { TestLibrary.TestFramework.LogError("003", "the ActualResult is not the ExpectResult"); retVal = false; } } catch (Exception e) { TestLibrary.TestFramework.LogError("004", "Unexpected exception: " + e); retVal = false; } return(retVal); }
public void TestToUInt16() { JulianDate julianDate = new JulianDate(100); IConvertible convertible = julianDate; convertible.ToUInt16(null); }
public ushort this [int index] { get { IConvertible value = (IConvertible)data.GetValue(index); return(value.ToUInt16(null)); } }
public void ToUInt16Test() { ushort expected = 3; Assert.Equal(expected, lengthAsIConvertible.ToUInt16(null)); Assert.Equal(expected, Convert.ToUInt16(length)); Assert.Equal(expected, Convert.ChangeType(length, typeof(ushort))); }
/// <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 void ToUInt16Test() { IConvertible target = (Half)33.33; IFormatProvider provider = CultureInfo.InvariantCulture; ushort expected = 33; ushort actual = target.ToUInt16(provider); Assert.Equal(expected, actual); }
public void op_ToUInt16_IFormatProvider() { const ushort expected = 123; IConvertible value = (AlphaDecimal)123; var actual = value.ToUInt16(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)); } }
static void Test_ToUInt16(IConvertible c) { Console.WriteLine("ToUInt16"); try { Console.WriteLine(c.ToUInt16(null)); } catch (Exception exc) { Console.WriteLine(exc.GetType()); } }
private static IEnumerable <byte> GetBytesFromConvertible(Type type, IConvertible value, uint defaultStringLength) { IEnumerable <byte> varValueBytes = null; if (value == null) { return(null); } switch (value.GetTypeCode()) { case TypeCode.Boolean: varValueBytes = BitConverter.GetBytes(value.ToBoolean(null)); break; case TypeCode.Byte: varValueBytes = new byte[] { value.ToByte(null) }; break; case TypeCode.Char: varValueBytes = BitConverter.GetBytes(value.ToChar(null)); break; case TypeCode.Int16: varValueBytes = BitConverter.GetBytes(value.ToInt16(null)); break; case TypeCode.Int32: varValueBytes = BitConverter.GetBytes(value.ToInt32(null)); break; case TypeCode.Int64: varValueBytes = BitConverter.GetBytes(value.ToInt64(null)); break; case TypeCode.UInt16: varValueBytes = BitConverter.GetBytes(value.ToUInt16(null)); break; case TypeCode.UInt32: varValueBytes = BitConverter.GetBytes(value.ToUInt32(null)); break; case TypeCode.UInt64: varValueBytes = BitConverter.GetBytes(value.ToUInt64(null)); break; case TypeCode.Single: varValueBytes = BitConverter.GetBytes(value.ToSingle(null)); break; case TypeCode.Double: varValueBytes = BitConverter.GetBytes(value.ToDouble(null)); break; case TypeCode.DateTime: varValueBytes = BitConverter.GetBytes(value.ToInt32(null)); break; case TypeCode.String: varValueBytes = value.ToString().ToAdsBytes(); break; case TypeCode.Object: if (Type.Equals(typeof(Date), type)) { varValueBytes = BitConverter.GetBytes(value.ToInt32(null)); } if (Type.Equals(typeof(Time), type)) { varValueBytes = BitConverter.GetBytes(value.ToInt32(null)); } break; } return(varValueBytes); }
/// <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()); } }
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); }
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 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 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); }
protected void EncodeConvertible(IConvertible value, Stream output) { output.WriteByte((byte)value.GetTypeCode()); byte[] result; switch (value.GetTypeCode()) { // the following encode directly on the stream case TypeCode.Boolean: output.WriteByte((byte)((bool)value ? 1 : 0)); return; case TypeCode.Byte: output.WriteByte(value.ToByte(null)); return; case TypeCode.SByte: output.WriteByte((byte)(value.ToSByte(null) + 128)); return; case TypeCode.Object: formatter.Serialize(output, value); return; case TypeCode.String: { long lengthPosition = output.Position; output.Write(new byte[4], 0, 4); StreamWriter w = new StreamWriter(output, Encoding.UTF8); w.Write((string)value); w.Flush(); long savedPosition = output.Position; uint payloadLength = (uint)(output.Position - lengthPosition - 4); output.Position = lengthPosition; output.Write(DataConverter.Converter.GetBytes(payloadLength), 0, 4); output.Position = savedPosition; return; } // the following obtain byte arrays which are dumped below case TypeCode.Char: result = DataConverter.Converter.GetBytes(value.ToChar(null)); break; case TypeCode.Single: result = DataConverter.Converter.GetBytes(value.ToSingle(null)); break; case TypeCode.Double: result = DataConverter.Converter.GetBytes(value.ToDouble(null)); break; case TypeCode.Int16: result = DataConverter.Converter.GetBytes(value.ToInt16(null)); break; case TypeCode.Int32: result = DataConverter.Converter.GetBytes(value.ToInt32(null)); break; case TypeCode.Int64: result = DataConverter.Converter.GetBytes(value.ToInt64(null)); break; case TypeCode.UInt16: result = DataConverter.Converter.GetBytes(value.ToUInt16(null)); break; case TypeCode.UInt32: result = DataConverter.Converter.GetBytes(value.ToUInt32(null)); break; case TypeCode.UInt64: result = DataConverter.Converter.GetBytes(value.ToUInt64(null)); break; case TypeCode.DateTime: result = DataConverter.Converter.GetBytes(((DateTime)value).ToBinary()); break; default: throw new MarshallingException("Unhandled form of IConvertible: " + value.GetTypeCode()); } output.Write(result, 0, result.Length); }
internal static bool JScriptStrictEquals(object v1, object v2, IConvertible ic1, IConvertible ic2, TypeCode t1, TypeCode t2, bool checkForDebuggerObjects) { 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; }
private object ExecuteBinaryOperator(IConvertible left, IConvertible right, CodeBinaryOperatorType op) { TypeCode typeCode = left.GetTypeCode(); TypeCode code2 = right.GetTypeCode(); TypeCode[] codeArray = new TypeCode[] { TypeCode.Byte, TypeCode.Char, TypeCode.Int16, TypeCode.UInt16, TypeCode.Int32, TypeCode.UInt32, TypeCode.Int64, TypeCode.UInt64 }; int num = -1; int num2 = -1; for (int i = 0; i < codeArray.Length; i++) { if (typeCode == codeArray[i]) { num = i; } if (code2 == codeArray[i]) { num2 = i; } if ((num != -1) && (num2 != -1)) { break; } } if ((num == -1) || (num2 == -1)) { return left; } int index = Math.Max(num, num2); object obj2 = left; switch (codeArray[index]) { case TypeCode.Char: { char ch = left.ToChar(null); char ch2 = right.ToChar(null); if (op != CodeBinaryOperatorType.BitwiseOr) { obj2 = ch & ch2; break; } obj2 = ch | ch2; break; } case TypeCode.Byte: { byte num5 = left.ToByte(null); byte num6 = right.ToByte(null); if (op != CodeBinaryOperatorType.BitwiseOr) { obj2 = num5 & num6; break; } obj2 = num5 | num6; break; } case TypeCode.Int16: { short num7 = left.ToInt16(null); short num8 = right.ToInt16(null); if (op != CodeBinaryOperatorType.BitwiseOr) { obj2 = num7 & num8; break; } obj2 = (short) (((ushort) num7) | ((ushort) num8)); break; } case TypeCode.UInt16: { ushort num9 = left.ToUInt16(null); ushort num10 = right.ToUInt16(null); if (op != CodeBinaryOperatorType.BitwiseOr) { obj2 = num9 & num10; break; } obj2 = num9 | num10; break; } case TypeCode.Int32: { int num11 = left.ToInt32(null); int num12 = right.ToInt32(null); if (op != CodeBinaryOperatorType.BitwiseOr) { obj2 = num11 & num12; break; } obj2 = num11 | num12; break; } case TypeCode.UInt32: { uint num13 = left.ToUInt32(null); uint num14 = right.ToUInt32(null); if (op != CodeBinaryOperatorType.BitwiseOr) { obj2 = num13 & num14; break; } obj2 = num13 | num14; break; } case TypeCode.Int64: { long num15 = left.ToInt64(null); long num16 = right.ToInt64(null); if (op != CodeBinaryOperatorType.BitwiseOr) { obj2 = num15 & num16; break; } obj2 = num15 | num16; break; } case TypeCode.UInt64: { ulong num17 = left.ToUInt64(null); ulong num18 = right.ToUInt64(null); if (op != CodeBinaryOperatorType.BitwiseOr) { obj2 = num17 & num18; break; } obj2 = num17 | num18; break; } } if ((obj2 != left) && (left is Enum)) { obj2 = Enum.ToObject(left.GetType(), obj2); } return obj2; }
internal static bool JScriptStrictEquals(Object v1, Object v2, IConvertible ic1, IConvertible ic2, TypeCode t1, TypeCode t2, bool checkForDebuggerObjects){ switch (t1){ case TypeCode.Empty: return t2 == TypeCode.Empty; case TypeCode.Object: if (v1 == v2) return true; if (v1 is Missing || v1 is System.Reflection.Missing) v1 = null; if (v1 == v2) return true; if (v2 is Missing || v2 is System.Reflection.Missing) v2 = null; return v1 == v2; case TypeCode.DBNull: return t2 == TypeCode.DBNull; case TypeCode.Boolean: return t2 == TypeCode.Boolean && ic1.ToBoolean(null) == ic2.ToBoolean(null); case TypeCode.Char: Char ch = ic1.ToChar(null); switch(t2){ case TypeCode.Char: return ch == ic2.ToChar(null); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return ch == ic2.ToInt64(null); case TypeCode.UInt64: return ch == ic2.ToUInt64(null); case TypeCode.Single: case TypeCode.Double: return ch == ic2.ToDouble(null); case TypeCode.Decimal: return ((Decimal)(int)ch) == ic2.ToDecimal(null); case TypeCode.String: String str = ic2.ToString(null); return str.Length == 1 && ch == str[0]; } return false; case TypeCode.SByte: SByte sb1 = ic1.ToSByte(null); switch (t2){ case TypeCode.Char: return sb1 == ic2.ToChar(null); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return sb1 == ic2.ToInt64(null); case TypeCode.UInt64: return sb1 >= 0 && ((UInt64)sb1) == ic2.ToUInt64(null); case TypeCode.Single: return sb1 == ic2.ToSingle(null); case TypeCode.Double: return sb1 == ic2.ToDouble(null); case TypeCode.Decimal: return ((Decimal)sb1) == ic2.ToDecimal(null); } return false; case TypeCode.Byte: Byte b1 = ic1.ToByte(null); switch (t2){ case TypeCode.Char: return b1 == ic2.ToChar(null); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return b1 == ic2.ToInt64(null); case TypeCode.UInt64: return b1 == ic2.ToUInt64(null); case TypeCode.Single: return b1 == ic2.ToSingle(null); case TypeCode.Double: return b1 == ic2.ToDouble(null); case TypeCode.Decimal: return ((Decimal)b1) == ic2.ToDecimal(null); } return false; case TypeCode.Int16: Int16 s1 = ic1.ToInt16(null); switch (t2){ case TypeCode.Char: return s1 == ic2.ToChar(null); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return s1 == ic2.ToInt64(null); case TypeCode.UInt64: return s1 >= 0 && ((UInt64)s1) == ic2.ToUInt64(null); case TypeCode.Single: return s1 == ic2.ToSingle(null); case TypeCode.Double: return s1 == ic2.ToDouble(null); case TypeCode.Decimal: return ((Decimal)s1) == ic2.ToDecimal(null); } return false; case TypeCode.UInt16: UInt16 us1 = ic1.ToUInt16(null); switch (t2){ case TypeCode.Char: return us1 == ic2.ToChar(null); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return us1 == ic2.ToInt64(null); case TypeCode.UInt64: return us1 == ic2.ToUInt64(null); case TypeCode.Single: return us1 == ic2.ToSingle(null); case TypeCode.Double: return us1 == ic2.ToDouble(null); case TypeCode.Decimal: return ((Decimal)us1) == ic2.ToDecimal(null); } return false; case TypeCode.Int32: Int32 i1 = ic1.ToInt32(null); switch (t2){ case TypeCode.Char: return i1 == ic2.ToChar(null); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return i1 == ic2.ToInt64(null); case TypeCode.UInt64: return i1 >= 0 && ((UInt64)i1) == ic2.ToUInt64(null); case TypeCode.Single: return i1 == ic2.ToSingle(null); case TypeCode.Double: return i1 == ic2.ToDouble(null); case TypeCode.Decimal: return ((Decimal)i1) == ic2.ToDecimal(null); } return false; case TypeCode.UInt32: UInt32 ui1 = ic1.ToUInt32(null); switch (t2){ case TypeCode.Char: return ui1 == ic2.ToChar(null); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return ui1 == ic2.ToInt64(null); case TypeCode.UInt64: return ui1 == ic2.ToUInt64(null); case TypeCode.Single: return ui1 == ic2.ToSingle(null); case TypeCode.Double: return ui1 == ic2.ToDouble(null); case TypeCode.Decimal: return ((Decimal)ui1) == ic2.ToDecimal(null); } return false; case TypeCode.Int64: Int64 l1 = ic1.ToInt64(null); switch (t2){ case TypeCode.Char: return l1 == ic2.ToChar(null); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return l1 == ic2.ToInt64(null); case TypeCode.UInt64: return l1 >= 0 && ((UInt64)l1) == ic2.ToUInt64(null); case TypeCode.Single: return l1 == ic2.ToSingle(null); case TypeCode.Double: return l1 == ic2.ToDouble(null); case TypeCode.Decimal: return ((Decimal)l1) == ic2.ToDecimal(null); } return false; case TypeCode.UInt64: UInt64 ul1 = ic1.ToUInt64(null); switch (t2){ case TypeCode.Char: return ul1 == ic2.ToChar(null); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: l1 = ic2.ToInt64(null); return l1 >= 0 && ul1 == (UInt64)l1; case TypeCode.UInt64: return ul1 == ic2.ToUInt64(null); case TypeCode.Single: return ul1 == ic2.ToSingle(null); case TypeCode.Double: return ul1 == ic2.ToDouble(null); case TypeCode.Decimal: return ((Decimal)ul1) == ic2.ToDecimal(null); } return false; case TypeCode.Single: Single f1 = ic1.ToSingle(null); switch (t2){ case TypeCode.Char: return f1 == ic2.ToChar(null); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return f1 == ic2.ToInt64(null); case TypeCode.UInt64: return f1 == ic2.ToUInt64(null); case TypeCode.Single: return f1 == ic2.ToSingle(null); case TypeCode.Double: return f1 == ic2.ToSingle(null); case TypeCode.Decimal: return ((Decimal)f1) == ic2.ToDecimal(null); } return false; case TypeCode.Double: Double d1 = ic1.ToDouble(null); switch (t2){ case TypeCode.Char: return d1 == ic2.ToChar(null); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return d1 == ic2.ToInt64(null); case TypeCode.UInt64: return d1 == ic2.ToUInt64(null); case TypeCode.Single: return ((float)d1) == ic2.ToSingle(null); case TypeCode.Double: return d1 == ic2.ToDouble(null); case TypeCode.Decimal: return ((Decimal)d1) == ic2.ToDecimal(null); } return false; case TypeCode.Decimal: Decimal de1 = ic1.ToDecimal(null); switch (t2){ case TypeCode.Char: return de1 == (Decimal)(int)ic2.ToChar(null); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return de1 == ic2.ToInt64(null); case TypeCode.UInt64: return de1 == ic2.ToUInt64(null); case TypeCode.Single: return de1 == (Decimal)ic2.ToSingle(null); case TypeCode.Double: return de1 == (Decimal)ic2.ToDouble(null); case TypeCode.Decimal: return de1 == ic2.ToDecimal(null); } return false; case TypeCode.DateTime: return t2 == TypeCode.DateTime && ic1.ToDateTime(null) == ic2.ToDateTime(null); case TypeCode.String: if (t2 == TypeCode.Char){ String str = ic1.ToString(null); return str.Length == 1 && str[0] == ic2.ToChar(null); } return t2 == TypeCode.String && (v1 == v2 || ic1.ToString(null).Equals(ic2.ToString(null))); } return false; //should never get here }
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)); }
// 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"); } }
public static Literal DoSubOvf(IConvertible ic1, IConvertible ic2, TypeCode code1, TypeCode code2, BinaryExpression binaryExpression){ TypeNode type = SystemTypes.Object; object val = null; checked{switch(code1){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: int i = ic1.ToInt32(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Byte: case TypeCode.Char: case TypeCode.UInt16: val = i - ic2.ToInt32(null); type = SystemTypes.Int32; break; case TypeCode.Int64: case TypeCode.UInt32: case TypeCode.UInt64: val = i - ic2.ToInt64(null); type = SystemTypes.Int64; break; case TypeCode.Single: val = i - ic2.ToSingle(null); type = SystemTypes.Single; break; case TypeCode.Double: val = i - ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = i - ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.Byte: case TypeCode.UInt16: ushort us = ic1.ToUInt16(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: val = us - ic2.ToInt32(null); type = SystemTypes.Int32; break; case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.UInt32: val = us - ic2.ToUInt32(null); type = SystemTypes.UInt32; break; case TypeCode.Int64: val = us - ic2.ToInt64(null); type = SystemTypes.Int64; break; case TypeCode.UInt64: val = us - ic2.ToUInt64(null); type = SystemTypes.UInt64; break; case TypeCode.Single: case TypeCode.Double: val = us - ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = us - ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.Char: char ch = ic1.ToChar(null); if (code2 != TypeCode.Char) goto default; val = ch - ic2.ToChar(null); type = SystemTypes.Int32; break; case TypeCode.UInt32: uint ui = ic1.ToUInt32(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: val = ui - ic2.ToInt64(null); type = SystemTypes.Int64; break; case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.UInt32: val = ui - ic2.ToUInt32(null); type = SystemTypes.UInt32; break; case TypeCode.UInt64: val = ui - ic2.ToUInt64(null); type = SystemTypes.UInt64; break; case TypeCode.Single: val = ui - ic2.ToSingle(null); type = SystemTypes.Single; break; case TypeCode.Double: val = ui - ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = ui - ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.Int64: long l = ic1.ToInt64(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: val = l - ic2.ToInt64(null); type = SystemTypes.Int64; break; case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.UInt32: val = l - ic2.ToUInt32(null); type = SystemTypes.UInt32; break; case TypeCode.UInt64: val = l - (long)ic2.ToUInt64(null); type = SystemTypes.UInt64; break; case TypeCode.Single: val = l - ic2.ToSingle(null); type = SystemTypes.Single; break; case TypeCode.Double: val = l - ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = l - ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.UInt64: ulong ul = ic1.ToUInt64(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.UInt32: val = ul - ic2.ToUInt32(null); type = SystemTypes.UInt64; break; case TypeCode.Int64: case TypeCode.UInt64: val = ul - ic2.ToUInt64(null); type = SystemTypes.UInt64; break; case TypeCode.Single: val = ul - ic2.ToSingle(null); type = SystemTypes.Single; break; case TypeCode.Double: val = ul - ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = ul - ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; case TypeCode.Single: float f = ic1.ToSingle(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: val = f - ic2.ToInt16(null); type = SystemTypes.Single; break; case TypeCode.Int32: case TypeCode.Int64: val = f - (double)ic2.ToInt64(null); type = SystemTypes.Double; break; case TypeCode.Byte: case TypeCode.Char: case TypeCode.UInt16: val = f - ic2.ToUInt16(null); type = SystemTypes.Single; break; case TypeCode.UInt32: case TypeCode.UInt64: val = f - (double)ic2.ToUInt64(null); type = SystemTypes.Double; break; case TypeCode.Single: case TypeCode.Double: val = f - ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = f - (double)ic2.ToDecimal(null); type = SystemTypes.Double; break; default: return null; } break; case TypeCode.Double: double d = ic1.ToDouble(null); switch(code2){ case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: val = d - ic2.ToInt64(null); type = SystemTypes.Double; break; case TypeCode.Byte: case TypeCode.Char: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: val = d - ic2.ToUInt64(null); type = SystemTypes.Double; break; case TypeCode.Single: case TypeCode.Double: val = d - ic2.ToDouble(null); type = SystemTypes.Double; break; case TypeCode.Decimal: val = d - (double)ic2.ToDecimal(null); type = SystemTypes.Double; break; default: return null; } break; case TypeCode.Decimal: decimal dec = ic1.ToDecimal(null); switch(code2){ case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Char: case TypeCode.Int32: val = dec - ic2.ToInt32(null); type = SystemTypes.Decimal; break; case TypeCode.UInt32: case TypeCode.Int64: case TypeCode.UInt64: val = dec - ic2.ToInt64(null); type = SystemTypes.Decimal; break; case TypeCode.Decimal: val = dec - ic2.ToDecimal(null); type = SystemTypes.Decimal; break; default: return null; } break; default: return null; } } return new Literal(val, type, binaryExpression.SourceContext); }
internal static Object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) { Contract.Requires(value != null, "[Convert.DefaultToType]value!=null"); if (targetType==null) { throw new ArgumentNullException("targetType"); } Contract.EndContractBlock(); RuntimeType rtTargetType = targetType as RuntimeType; if (rtTargetType != null) { if (value.GetType() == targetType) { return value; } if (rtTargetType == ConvertTypes[(int)TypeCode.Boolean]) return value.ToBoolean(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Char]) return value.ToChar(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.SByte]) return value.ToSByte(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Byte]) return value.ToByte(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Int16]) return value.ToInt16(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.UInt16]) return value.ToUInt16(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Int32]) return value.ToInt32(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.UInt32]) return value.ToUInt32(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Int64]) return value.ToInt64(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.UInt64]) return value.ToUInt64(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Single]) return value.ToSingle(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Double]) return value.ToDouble(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Decimal]) return value.ToDecimal(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.DateTime]) return value.ToDateTime(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.String]) return value.ToString(provider); if (rtTargetType == ConvertTypes[(int)TypeCode.Object]) return (Object)value; // Need to special case Enum because typecode will be underlying type, e.g. Int32 if (rtTargetType == EnumType) return (Enum)value; if (rtTargetType == ConvertTypes[(int)TypeCode.DBNull]) throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull")); if (rtTargetType == ConvertTypes[(int)TypeCode.Empty]) throw new InvalidCastException(Environment.GetResourceString("InvalidCast_Empty")); } throw new InvalidCastException(Environment.GetResourceString("InvalidCast_FromTo", value.GetType().FullName, targetType.FullName)); }
public static 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); }
/// <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; } }
public void SetAsIConvertible(IConvertible value) { Debug.Assert(IsEmpty); // The setter can only be called once as VariantClear might be needed otherwise TypeCode tc = value.GetTypeCode(); CultureInfo ci = CultureInfo.CurrentCulture; switch (tc) { case TypeCode.Empty: break; case TypeCode.Object: AsUnknown = value; break; case TypeCode.DBNull: SetAsNull(); break; case TypeCode.Boolean: AsBool = value.ToBoolean(ci); break; case TypeCode.Char: AsUi2 = value.ToChar(ci); break; case TypeCode.SByte: AsI1 = value.ToSByte(ci); break; case TypeCode.Byte: AsUi1 = value.ToByte(ci); break; case TypeCode.Int16: AsI2 = value.ToInt16(ci); break; case TypeCode.UInt16: AsUi2 = value.ToUInt16(ci); break; case TypeCode.Int32: AsI4 = value.ToInt32(ci); break; case TypeCode.UInt32: AsUi4 = value.ToUInt32(ci); break; case TypeCode.Int64: AsI8 = value.ToInt64(ci); break; case TypeCode.UInt64: AsI8 = value.ToInt64(ci); break; case TypeCode.Single: AsR4 = value.ToSingle(ci); break; case TypeCode.Double: AsR8 = value.ToDouble(ci); break; case TypeCode.Decimal: AsDecimal = value.ToDecimal(ci); break; case TypeCode.DateTime: AsDate = value.ToDateTime(ci); break; case TypeCode.String: AsBstr = value.ToString(ci); break; default: throw Assert.Unreachable; } }
internal static Object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) { 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)); }