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; }
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 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 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)); }
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 })); }
/// <summary> /// Returns true if the constant is an integral value that falls in the range of the target type. /// The target type does have to be an integral type. If it is not, this method always returns false. /// </summary> public static bool IsIntegerInRangeOf(ICompileTimeConstant constExpression, ITypeReference targetType) { switch (targetType.TypeCode) { case PrimitiveTypeCode.UInt8: { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.Byte: return(true); case System.TypeCode.SByte: return(byte.MinValue <= ic.ToSByte(null)); case System.TypeCode.Int16: short s = ic.ToInt16(null); return(byte.MinValue <= s && s <= byte.MaxValue); case System.TypeCode.Int32: int i = ic.ToInt32(null); return(byte.MinValue <= i && i <= byte.MaxValue); case System.TypeCode.Int64: long lng = ic.ToInt64(null); return(byte.MinValue <= lng && lng <= byte.MaxValue); case System.TypeCode.UInt16: return(ic.ToUInt16(null) <= byte.MaxValue); case System.TypeCode.UInt32: return(ic.ToUInt32(null) <= byte.MaxValue); case System.TypeCode.UInt64: return(ic.ToUInt64(null) <= byte.MaxValue); case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return(byte.MinValue <= d && d <= byte.MaxValue); } return(false); } case PrimitiveTypeCode.UInt16: { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.Byte: case System.TypeCode.UInt16: return(true); case System.TypeCode.SByte: return(ushort.MinValue <= ic.ToSByte(null)); case System.TypeCode.Int16: return(ushort.MinValue <= ic.ToInt16(null)); case System.TypeCode.Int32: int i = ic.ToInt32(null); return(ushort.MinValue <= i && i <= ushort.MaxValue); case System.TypeCode.Int64: long lng = ic.ToInt64(null); return(ushort.MinValue <= lng && lng <= ushort.MaxValue); case System.TypeCode.UInt32: return(ic.ToUInt32(null) <= ushort.MaxValue); case System.TypeCode.UInt64: return(ic.ToUInt64(null) <= ushort.MaxValue); case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return(ushort.MinValue <= d && d <= ushort.MaxValue); } return(false); } case PrimitiveTypeCode.UInt32: { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.Byte: case System.TypeCode.UInt16: case System.TypeCode.UInt32: return(true); case System.TypeCode.SByte: return(uint.MinValue <= ic.ToSByte(null)); case System.TypeCode.Int16: return(uint.MinValue <= ic.ToInt16(null)); case System.TypeCode.Int32: return(uint.MinValue <= ic.ToInt32(null)); case System.TypeCode.Int64: long lng = ic.ToInt64(null); return(uint.MinValue <= lng && lng <= uint.MaxValue); case System.TypeCode.UInt64: return(ic.ToUInt64(null) <= uint.MaxValue); case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return(uint.MinValue <= d && d <= uint.MaxValue); } return(false); } case PrimitiveTypeCode.UInt64: { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.Byte: case System.TypeCode.UInt16: case System.TypeCode.UInt32: case System.TypeCode.UInt64: return(true); case System.TypeCode.SByte: return(0 <= ic.ToSByte(null)); case System.TypeCode.Int16: return(0 <= ic.ToInt16(null)); case System.TypeCode.Int32: return(0 <= ic.ToInt32(null)); case System.TypeCode.Int64: return(0 <= ic.ToInt64(null)); case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return(0 <= d && d <= ulong.MaxValue); } return(false); } case PrimitiveTypeCode.Int8: { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.SByte: return(true); case System.TypeCode.Int16: short s = ic.ToInt16(null); return(sbyte.MinValue <= s && s <= sbyte.MaxValue); case System.TypeCode.Int32: int i = ic.ToInt32(null); return(sbyte.MinValue <= i && i <= sbyte.MaxValue); case System.TypeCode.Int64: long lng = ic.ToInt64(null); return(sbyte.MinValue <= lng && lng <= sbyte.MaxValue); case System.TypeCode.Byte: return(ic.ToByte(null) <= sbyte.MaxValue); case System.TypeCode.UInt16: return(ic.ToUInt16(null) <= sbyte.MaxValue); case System.TypeCode.UInt32: return(ic.ToUInt32(null) <= sbyte.MaxValue); case System.TypeCode.UInt64: return(ic.ToUInt64(null) <= (ulong)sbyte.MaxValue); case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return(sbyte.MinValue <= d && d <= sbyte.MaxValue); } return(false); } case PrimitiveTypeCode.Int16: { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.SByte: case System.TypeCode.Byte: case System.TypeCode.Int16: return(true); case System.TypeCode.Int32: int i = ic.ToInt32(null); return(short.MinValue <= i && i <= short.MaxValue); case System.TypeCode.Int64: long lng = ic.ToInt64(null); return(short.MinValue <= lng && lng <= short.MaxValue); case System.TypeCode.UInt16: return(ic.ToUInt16(null) <= short.MaxValue); case System.TypeCode.UInt32: return(ic.ToUInt32(null) <= short.MaxValue); case System.TypeCode.UInt64: return(ic.ToUInt64(null) <= (ulong)short.MaxValue); case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return(short.MinValue <= d && d <= short.MaxValue); } return(false); } case PrimitiveTypeCode.Int32: { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.SByte: case System.TypeCode.Byte: case System.TypeCode.Int16: case System.TypeCode.UInt16: case System.TypeCode.Int32: return(true); case System.TypeCode.Int64: long lng = ic.ToInt64(null); return(int.MinValue <= lng && lng <= int.MaxValue); case System.TypeCode.UInt32: return(ic.ToUInt32(null) <= int.MaxValue); case System.TypeCode.UInt64: return(ic.ToUInt64(null) <= int.MaxValue); case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return(int.MinValue <= d && d <= int.MaxValue); } return(false); } case PrimitiveTypeCode.Int64: { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.SByte: case System.TypeCode.Byte: case System.TypeCode.Int16: case System.TypeCode.UInt16: case System.TypeCode.Int32: case System.TypeCode.UInt32: case System.TypeCode.Int64: return(true); case System.TypeCode.UInt64: return(ic.ToUInt64(null) <= int.MaxValue); case System.TypeCode.Decimal: decimal d = ic.ToDecimal(null); return(long.MinValue <= d && d <= long.MaxValue); } return(false); } } return(false); }
/// <summary> /// Writes a <see cref="Object"/> value. /// An error will raised if the value cannot be written as a single JSON token. /// </summary> /// <param name="value">The <see cref="Object"/> value to write.</param> public virtual void WriteValue(object value) { if (value == null) { WriteNull(); return; } else if (value is IConvertible) { IConvertible convertible = value as IConvertible; switch (convertible.GetTypeCode()) { case TypeCode.String: WriteValue(convertible.ToString(CultureInfo.InvariantCulture)); return; case TypeCode.Char: WriteValue(convertible.ToChar(CultureInfo.InvariantCulture)); return; case TypeCode.Boolean: WriteValue(convertible.ToBoolean(CultureInfo.InvariantCulture)); return; case TypeCode.SByte: WriteValue(convertible.ToSByte(CultureInfo.InvariantCulture)); return; case TypeCode.Int16: WriteValue(convertible.ToInt16(CultureInfo.InvariantCulture)); return; case TypeCode.UInt16: WriteValue(convertible.ToUInt16(CultureInfo.InvariantCulture)); return; case TypeCode.Int32: WriteValue(convertible.ToInt32(CultureInfo.InvariantCulture)); return; case TypeCode.Byte: WriteValue(convertible.ToByte(CultureInfo.InvariantCulture)); return; case TypeCode.UInt32: WriteValue(convertible.ToUInt32(CultureInfo.InvariantCulture)); return; case TypeCode.Int64: WriteValue(convertible.ToInt64(CultureInfo.InvariantCulture)); return; case TypeCode.UInt64: WriteValue(convertible.ToUInt64(CultureInfo.InvariantCulture)); return; case TypeCode.Single: WriteValue(convertible.ToSingle(CultureInfo.InvariantCulture)); return; case TypeCode.Double: WriteValue(convertible.ToDouble(CultureInfo.InvariantCulture)); return; case TypeCode.DateTime: WriteValue(convertible.ToDateTime(CultureInfo.InvariantCulture)); return; case TypeCode.Decimal: WriteValue(convertible.ToDecimal(CultureInfo.InvariantCulture)); return; case TypeCode.DBNull: WriteNull(); return; } } #if !PocketPC && !NET20 else if (value is DateTimeOffset) { WriteValue((DateTimeOffset)value); return; } #endif else if (value is byte[]) { WriteValue((byte[])value); return; } throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType())); }
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); }
private Object EvaluatePlus2(Object v1, Object v2) { IConvertible ic1 = Convert.GetIConvertible(v1); IConvertible ic2 = Convert.GetIConvertible(v2); TypeCode t1 = Convert.GetTypeCode(v1, ic1); TypeCode t2 = Convert.GetTypeCode(v2, ic2); switch (t1) { case TypeCode.Empty: return(Plus.DoOp(v1, v2)); case TypeCode.DBNull: switch (t2) { case TypeCode.Empty: return(Double.NaN); case TypeCode.DBNull: return(0); case TypeCode.Boolean: case TypeCode.Char: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: return(ic2.ToInt32(null)); case TypeCode.UInt32: return(ic2.ToUInt32(null)); case TypeCode.Int64: return(ic2.ToInt64(null)); case TypeCode.UInt64: return(ic2.ToUInt64(null)); case TypeCode.Single: case TypeCode.Double: return(ic2.ToDouble(null)); case TypeCode.Object: case TypeCode.Decimal: case TypeCode.DateTime: break; case TypeCode.String: return("null" + ic2.ToString(null)); } break; case TypeCode.Char: { int val = ic1.ToInt32(null); switch (t2) { case TypeCode.Empty: return(Double.NaN); case TypeCode.DBNull: return(val); case TypeCode.Boolean: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: return(((IConvertible)Plus.DoOp(val, ic2.ToInt32(null))).ToChar(null)); case TypeCode.UInt32: case TypeCode.Int64: return(((IConvertible)Plus.DoOp((long)val, ic2.ToInt64(null))).ToChar(null)); case TypeCode.UInt64: return(((IConvertible)Plus.DoOp((ulong)val, ic2.ToUInt64(null))).ToChar(null)); case TypeCode.Single: case TypeCode.Double: checked { return((char)(int)(Convert.CheckIfDoubleIsInteger((double)Plus.DoOp((double)val, ic2.ToDouble(null))))); } case TypeCode.Object: case TypeCode.Decimal: case TypeCode.DateTime: return(Plus.DoOp(v1, v2)); case TypeCode.Char: case TypeCode.String: return(ic1.ToString(null) + ic2.ToString(null)); } break; } case TypeCode.Boolean: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: { int val = ic1.ToInt32(null); switch (t2) { case TypeCode.Empty: return(Double.NaN); case TypeCode.DBNull: return(val); case TypeCode.Char: return(((IConvertible)Plus.DoOp(val, ic2.ToInt32(null))).ToChar(null)); case TypeCode.Boolean: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: return(Plus.DoOp(val, ic2.ToInt32(null))); case TypeCode.UInt32: case TypeCode.Int64: return(Plus.DoOp((long)val, ic2.ToInt64(null))); case TypeCode.UInt64: if (val >= 0) { return(Plus.DoOp((ulong)val, ic2.ToUInt64(null))); } else { return(Plus.DoOp((double)val, ic2.ToDouble(null))); } case TypeCode.Single: case TypeCode.Double: return(Plus.DoOp((double)val, ic2.ToDouble(null))); case TypeCode.Object: case TypeCode.Decimal: case TypeCode.DateTime: break; case TypeCode.String: return(Convert.ToString(v1) + ic2.ToString(null)); } break; } case TypeCode.UInt32: { uint val = ic1.ToUInt32(null); switch (t2) { case TypeCode.Empty: return(Double.NaN); case TypeCode.DBNull: return(val); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.Int32: int val2 = ic2.ToInt32(null); if (val2 >= 0) { return(Plus.DoOp(val, (uint)val2)); } else { return(Plus.DoOp((long)val, (long)val2)); } case TypeCode.Int64: return(Plus.DoOp((long)val, ic2.ToInt64(null))); case TypeCode.Char: return(((IConvertible)Plus.DoOp(val, ic2.ToUInt32(null))).ToChar(null)); case TypeCode.Boolean: case TypeCode.UInt16: case TypeCode.UInt32: return(Plus.DoOp(val, ic2.ToUInt32(null))); case TypeCode.UInt64: return(Plus.DoOp((ulong)val, ic2.ToUInt64(null))); case TypeCode.Single: case TypeCode.Double: return(Plus.DoOp((double)val, ic2.ToDouble(null))); case TypeCode.Object: case TypeCode.Decimal: case TypeCode.DateTime: break; case TypeCode.String: return(Convert.ToString(v1) + ic2.ToString(null)); } break; } case TypeCode.Int64: { long val = ic1.ToInt64(null); switch (t2) { case TypeCode.Empty: return(Double.NaN); case TypeCode.DBNull: return(val); case TypeCode.Char: return(((IConvertible)Plus.DoOp(val, ic2.ToInt64(null))).ToChar(null)); case TypeCode.Boolean: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return(Plus.DoOp(val, ic2.ToInt64(null))); case TypeCode.UInt64: if (val >= 0) { return(Plus.DoOp((ulong)val, ic2.ToUInt64(null))); } else { return(Plus.DoOp((double)val, ic2.ToDouble(null))); } case TypeCode.Single: case TypeCode.Double: return(Plus.DoOp((double)val, ic2.ToDouble(null))); case TypeCode.Object: case TypeCode.Decimal: case TypeCode.DateTime: break; case TypeCode.String: return(Convert.ToString(v1) + ic2.ToString(null)); } break; } case TypeCode.UInt64: { ulong val = ic1.ToUInt64(null); switch (t2) { case TypeCode.Empty: return(Double.NaN); case TypeCode.DBNull: return(val); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: long val2 = ic2.ToInt64(null); if (val2 >= 0) { return(Plus.DoOp(val, (ulong)val2)); } else { return(Plus.DoOp((double)val, (double)val2)); } case TypeCode.Char: return(((IConvertible)Plus.DoOp(val, ic2.ToUInt64(null))).ToChar(null)); case TypeCode.UInt16: case TypeCode.Boolean: case TypeCode.UInt32: case TypeCode.UInt64: return(Plus.DoOp(val, ic2.ToUInt64(null))); case TypeCode.Single: case TypeCode.Double: return(Plus.DoOp((double)val, ic2.ToDouble(null))); case TypeCode.Object: case TypeCode.Decimal: case TypeCode.DateTime: break; case TypeCode.String: return(Convert.ToString(v1) + ic2.ToString(null)); } break; } case TypeCode.Single: case TypeCode.Double: { double d = ic1.ToDouble(null); switch (t2) { case TypeCode.Empty: return(Double.NaN); case TypeCode.DBNull: return(ic1.ToDouble(null)); case TypeCode.Char: return(System.Convert.ToChar(System.Convert.ToInt32((d + (double)ic2.ToInt32(null))))); case TypeCode.Boolean: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: return(d + (double)ic2.ToInt32(null)); case TypeCode.UInt32: case TypeCode.Int64: case TypeCode.UInt64: case TypeCode.Single: case TypeCode.Double: return(d + ic2.ToDouble(null)); case TypeCode.Object: case TypeCode.Decimal: case TypeCode.DateTime: break; case TypeCode.String: return(new ConcatString(Convert.ToString(d), ic2.ToString(null))); } break; } case TypeCode.Object: case TypeCode.Decimal: case TypeCode.DateTime: break; case TypeCode.String: switch (t2) { case TypeCode.Object: break; case TypeCode.String: if (v1 is ConcatString) { return(new ConcatString((ConcatString)v1, ic2.ToString(null))); } else { return(new ConcatString(ic1.ToString(null), ic2.ToString(null))); } default: if (v1 is ConcatString) { return(new ConcatString((ConcatString)v1, Convert.ToString(v2))); } else { return(new ConcatString(ic1.ToString(null), Convert.ToString(v2))); } } break; } MethodInfo oper = this.GetOperator(v1 == null ? Typeob.Empty : v1.GetType(), v2 == null ? Typeob.Empty : v2.GetType()); if (oper != null) { return(oper.Invoke(null, (BindingFlags)0, JSBinder.ob, new Object[] { v1, v2 }, null)); } else { return(Plus.DoOp(v1, v2)); } }
private NumberFormat GetNumberFormat(IConvertible value) { NumberFormat format = this.numberFormat; if (format == NumberFormat.Auto) { long number = (value is ulong) ? (long)(ulong)value : value.ToInt64(CultureInfo.InvariantCulture); if (number < 16) { return NumberFormat.Decimal; } if (((number % 10) == 0) && (number < 1000)) { return NumberFormat.Decimal; } return NumberFormat.Hexadecimal; } return format; }
/// <summary> /// 转换数据类型 /// </summary> /// <param name="value"></param> /// <param name="conversionType"></param> /// <param name="provider"></param> /// <returns></returns> public static object ChangeValueType(object value, Type conversionType, IFormatProvider provider) { if (conversionType == null) { throw new ArgumentNullException("conversionType"); } bool valueCanbeNull = valueNaullable(conversionType); if (valueCanbeNull && (value == null || value.ToString().Length == 0))//如果Nullable<>类型,且值是空,则直接返回空 { return(null); } if (value == null) { if (conversionType.IsValueType) { throw new InvalidCastException(string.Format("值为空。")); } return(null); } IConvertible convertible = value as IConvertible; if (convertible == null) { if (value.GetType() != conversionType) { throw new InvalidCastException(string.Format("值不能被转换。")); } return(value); } if (conversionType == typeof(System.Boolean) || conversionType == typeof(Nullable <System.Boolean>)) { if (value.ToString() == "1") { return(true); } if (value.ToString() == "0") { return(false); } return(convertible.ToBoolean(provider)); } if (conversionType == typeof(System.Char) || conversionType == typeof(Nullable <System.Char>)) { return(convertible.ToChar(provider)); } if (conversionType == typeof(System.SByte) || conversionType == typeof(Nullable <System.SByte>)) { return(convertible.ToSByte(provider)); } if (conversionType == typeof(System.Byte) || conversionType == typeof(Nullable <System.Byte>)) { return(convertible.ToByte(provider)); } if (conversionType == typeof(System.Int16) || conversionType == typeof(Nullable <System.Int16>)) { return(convertible.ToInt16(provider)); } if (conversionType == typeof(System.UInt16) || conversionType == typeof(Nullable <System.UInt16>)) { return(convertible.ToUInt16(provider)); } if (conversionType == typeof(System.Int32) || conversionType == typeof(Nullable <System.Int32>)) { return(convertible.ToInt32(provider)); } if (conversionType == typeof(System.UInt32) || conversionType == typeof(Nullable <System.UInt32>)) { return(convertible.ToUInt32(provider)); } if (conversionType == typeof(System.Int64) || conversionType == typeof(Nullable <System.Int64>)) { return(convertible.ToInt64(provider)); } if (conversionType == typeof(System.UInt64) || conversionType == typeof(Nullable <System.UInt64>)) { return(convertible.ToUInt64(provider)); } if (conversionType == typeof(System.Single) || conversionType == typeof(Nullable <System.Single>)) { return(convertible.ToSingle(provider)); } if (conversionType == typeof(System.Double) || conversionType == typeof(Nullable <System.Double>)) { return(convertible.ToDouble(provider)); } if (conversionType == typeof(System.Decimal) || conversionType == typeof(Nullable <System.Decimal>)) { return(convertible.ToDecimal(provider)); } if (conversionType == typeof(System.DateTime) || conversionType == typeof(Nullable <System.DateTime>)) { return(convertible.ToDateTime(provider)); } if (conversionType == typeof(System.String)) { return(convertible.ToString(provider)); } if (conversionType == typeof(System.Object)) { return(value); } return(value); }
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; }
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 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; }
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; }
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); } }
private object SmallestNumeric(object constant) { if (null == constant) { return((int)0); } else { string sval = (constant as string); if (null != sval) { int i4; if (Int32.TryParse(sval, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out i4)) { return(i4); } long i8; if (Int64.TryParse(sval, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out i8)) { return(i8); } double r8; if (Double.TryParse(sval, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.InvariantInfo, out r8)) { return(r8); } } else { IConvertible convertible = (constant as IConvertible); if (null != convertible) { try { return(convertible.ToInt32(NumberFormatInfo.InvariantInfo)); } catch (System.ArgumentException e) { ExceptionBuilder.TraceExceptionWithoutRethrow(e); } catch (System.FormatException e) { ExceptionBuilder.TraceExceptionWithoutRethrow(e); } catch (System.InvalidCastException e) { ExceptionBuilder.TraceExceptionWithoutRethrow(e); } catch (System.OverflowException e) { ExceptionBuilder.TraceExceptionWithoutRethrow(e); } try { return(convertible.ToInt64(NumberFormatInfo.InvariantInfo)); } catch (System.ArgumentException e) { ExceptionBuilder.TraceExceptionWithoutRethrow(e); } catch (System.FormatException e) { ExceptionBuilder.TraceExceptionWithoutRethrow(e); } catch (System.InvalidCastException e) { ExceptionBuilder.TraceExceptionWithoutRethrow(e); } catch (System.OverflowException e) { ExceptionBuilder.TraceExceptionWithoutRethrow(e); } try { return(convertible.ToDouble(NumberFormatInfo.InvariantInfo)); } catch (System.ArgumentException e) { ExceptionBuilder.TraceExceptionWithoutRethrow(e); } catch (System.FormatException e) { ExceptionBuilder.TraceExceptionWithoutRethrow(e); } catch (System.InvalidCastException e) { ExceptionBuilder.TraceExceptionWithoutRethrow(e); } catch (System.OverflowException e) { ExceptionBuilder.TraceExceptionWithoutRethrow(e); } } } } return(constant); }
// This is a custom version of Convert.ChangeType() which works // with the TypeBuilder defined types when compiling corlib. public static object ChangeType(object value, TypeSpec targetType, out bool error) { IConvertible convert_value = value as IConvertible; if (convert_value == null) { error = true; return(null); } // // We cannot rely on build-in type conversions as they are // more limited than what C# supports. // See char -> float/decimal/double conversion // error = false; try { if (targetType == TypeManager.bool_type) { return(convert_value.ToBoolean(nf_provider)); } if (targetType == TypeManager.byte_type) { return(convert_value.ToByte(nf_provider)); } if (targetType == TypeManager.char_type) { return(convert_value.ToChar(nf_provider)); } if (targetType == TypeManager.short_type) { return(convert_value.ToInt16(nf_provider)); } if (targetType == TypeManager.int32_type) { return(convert_value.ToInt32(nf_provider)); } if (targetType == TypeManager.int64_type) { return(convert_value.ToInt64(nf_provider)); } if (targetType == TypeManager.sbyte_type) { return(convert_value.ToSByte(nf_provider)); } if (targetType == TypeManager.decimal_type) { if (convert_value.GetType() == typeof(char)) { return((decimal)convert_value.ToInt32(nf_provider)); } return(convert_value.ToDecimal(nf_provider)); } if (targetType == TypeManager.double_type) { if (convert_value.GetType() == typeof(char)) { return((double)convert_value.ToInt32(nf_provider)); } return(convert_value.ToDouble(nf_provider)); } if (targetType == TypeManager.float_type) { if (convert_value.GetType() == typeof(char)) { return((float)convert_value.ToInt32(nf_provider)); } return(convert_value.ToSingle(nf_provider)); } if (targetType == TypeManager.string_type) { return(convert_value.ToString(nf_provider)); } if (targetType == TypeManager.ushort_type) { return(convert_value.ToUInt16(nf_provider)); } if (targetType == TypeManager.uint32_type) { return(convert_value.ToUInt32(nf_provider)); } if (targetType == TypeManager.uint64_type) { return(convert_value.ToUInt64(nf_provider)); } if (targetType == TypeManager.object_type) { return(value); } error = true; } catch { error = true; } return(null); }
public bool EvaluateEquality(object v1, object v2) { IConvertible ic1 = v1 as IConvertible; IConvertible ic2 = v2 as IConvertible; TypeCode tc1 = Convert.GetTypeCode(v1, ic1); TypeCode tc2 = Convert.GetTypeCode(v2, ic2); bool both_numbers = Convert.IsNumberTypeCode(tc1) && Convert.IsNumberTypeCode(tc2); if ((tc1 == tc2) || both_numbers) { switch (tc1) { case TypeCode.DBNull: case TypeCode.Empty: return(true); case TypeCode.Boolean: return(ic1.ToBoolean(null) == ic2.ToBoolean(null)); case TypeCode.String: return(ic1.ToString(null) == ic2.ToString(null)); case TypeCode.Object: if (v1 is ScriptFunction && v2 is ScriptFunction) { return(v1 == v2 || v1.Equals(v2)); } else { return(v1 == v2); } default: if (both_numbers) { double num1; if (Convert.IsFloatTypeCode(tc1)) { num1 = ic1.ToDouble(null); } else { num1 = (double)ic1.ToInt64(null); } double num2; if (Convert.IsFloatTypeCode(tc2)) { num2 = ic2.ToDouble(null); } else { num2 = (double)ic2.ToInt64(null); } return(num1 == num2); } else { return(false); } } } else { bool swapped = false; redo: switch (tc1) { case TypeCode.DBNull: if (tc2 == TypeCode.Empty) { return(true); } break; case TypeCode.String: if (Convert.IsNumberTypeCode(tc2)) { return(EvaluateEquality(Convert.ToNumber(v1), v2)); } break; case TypeCode.Boolean: return(EvaluateEquality(Convert.ToNumber(v1), v2)); case TypeCode.Object: if (tc2 == TypeCode.String || Convert.IsNumberTypeCode(tc2)) { return(EvaluateEquality(Convert.ToPrimitive(v1, null), v2)); } break; } if (!swapped) { swapped = true; object vt = v1; v1 = v2; v2 = vt; ic1 = v1 as IConvertible; ic2 = v2 as IConvertible; tc1 = Convert.GetTypeCode(v1, ic1); tc2 = Convert.GetTypeCode(v2, ic2); goto redo; } else { return(false); } } }
public static string Hex(object Number) { long num; if (Number == null) { throw new ArgumentNullException(Utils.GetResourceString("Argument_InvalidNullValue1", new string[] { "Number" })); } IConvertible convertible = Number as IConvertible; if (convertible != null) { switch (convertible.GetTypeCode()) { case TypeCode.Boolean: case TypeCode.Char: case TypeCode.DateTime: case (TypeCode.DateTime | TypeCode.Object): goto Label_013D; case TypeCode.SByte: return(Hex(convertible.ToSByte(null))); case TypeCode.Byte: return(Hex(convertible.ToByte(null))); case TypeCode.Int16: return(Hex(convertible.ToInt16(null))); case TypeCode.UInt16: return(Hex(convertible.ToUInt16(null))); case TypeCode.Int32: return(Hex(convertible.ToInt32(null))); case TypeCode.UInt32: return(Hex(convertible.ToUInt32(null))); case TypeCode.Int64: case TypeCode.Single: case TypeCode.Double: case TypeCode.Decimal: num = convertible.ToInt64(null); goto Label_010E; case TypeCode.UInt64: return(Hex(convertible.ToUInt64(null))); case TypeCode.String: try { num = Conversions.ToLong(convertible.ToString(null)); } catch (OverflowException) { return(Hex(Conversions.ToULong(convertible.ToString(null)))); } goto Label_010E; } } goto Label_013D; Label_010E: if (num == 0L) { return("0"); } if ((num <= 0L) && (num >= -2147483648L)) { return(Hex((int)num)); } return(Hex(num)); Label_013D :; throw new ArgumentException(Utils.GetResourceString("Argument_InvalidValueType2", new string[] { "Number", Utils.VBFriendlyName(Number) })); }
internal static Object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) { BCLDebug.Assert(value!=null, "[Convert.DefaultToType]value!=null"); if (targetType==null) { throw new ArgumentNullException("targetType"); } if (value.GetType()==targetType) { return value; } if (targetType==ConvertTypes[(int)TypeCode.Boolean]) return value.ToBoolean(provider); if (targetType==ConvertTypes[(int)TypeCode.Char]) return value.ToChar(provider); if (targetType==ConvertTypes[(int)TypeCode.SByte]) return value.ToSByte(provider); if (targetType==ConvertTypes[(int)TypeCode.Byte]) return value.ToByte(provider); if (targetType==ConvertTypes[(int)TypeCode.Int16]) return value.ToInt16(provider); if (targetType==ConvertTypes[(int)TypeCode.UInt16]) return value.ToUInt16(provider); if (targetType==ConvertTypes[(int)TypeCode.Int32]) return value.ToInt32(provider); if (targetType==ConvertTypes[(int)TypeCode.UInt32]) return value.ToUInt32(provider); if (targetType==ConvertTypes[(int)TypeCode.Int64]) return value.ToInt64(provider); if (targetType==ConvertTypes[(int)TypeCode.UInt64]) return value.ToUInt64(provider); if (targetType==ConvertTypes[(int)TypeCode.Single]) return value.ToSingle(provider); if (targetType==ConvertTypes[(int)TypeCode.Double]) return value.ToDouble(provider); if (targetType==ConvertTypes[(int)TypeCode.Decimal]) return value.ToDecimal(provider); if (targetType==ConvertTypes[(int)TypeCode.DateTime]) return value.ToDateTime(provider); if (targetType==ConvertTypes[(int)TypeCode.String]) { return value.ToString(provider); } if (targetType==ConvertTypes[(int)TypeCode.Object]) return (Object)value; if (targetType==ConvertTypes[(int)TypeCode.DBNull]) throw new InvalidCastException(Environment.GetResourceString("InvalidCast_DBNull")); if (targetType==ConvertTypes[(int)TypeCode.Empty]) throw new InvalidCastException(Environment.GetResourceString("InvalidCast_Empty")); throw new InvalidCastException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("InvalidCast_FromTo"), value.GetType().FullName, targetType.FullName)); }
public static Int64 ToInt64(this IConvertible @this) { return(@this.ToInt64(CultureInfo.CurrentCulture)); }
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; }
public static Int64 ToInt64Inv(this IConvertible @this) { return(@this.ToInt64(CultureInfo.InvariantCulture)); }
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 QuayRoadsPanelField(NetInfo netInfo, int sectionIndex, FieldInfo fieldInfo, IConvertible flag, UIPanel parent, RoadEditorPanel parentPanel) { netInfo_ = netInfo; sectionIndex_ = sectionIndex; fieldInfo_ = fieldInfo; flag_ = flag; parentPanel_ = parentPanel; if (flag_ is not null) { Log.Debug(flag.ToString()); Log.Debug(flag.ToUInt64().ToString()); var propertyCheckbox = parent.AddUIComponent <UICheckBoxExt>(); propertyCheckbox.Label = ""; propertyCheckbox.width = 20f; propertyCheckbox.isChecked = (AssetValue as IConvertible).IsFlagSet(flag); propertyCheckbox.eventCheckChanged += (_, _isChecked) => { //TODO: find a better / more robust way to do this. if (_isChecked) { if (Enum.GetUnderlyingType(fieldInfo_.FieldType) == typeof(Int32)) { AssetValue = (AssetValue as IConvertible).ToInt32(CultureInfo.InvariantCulture) | flag.ToInt32(CultureInfo.InvariantCulture); } else { AssetValue = (AssetValue as IConvertible).ToInt64() | flag.ToInt64(); } } else { if (Enum.GetUnderlyingType(fieldInfo_.FieldType) == typeof(Int32)) { AssetValue = (AssetValue as IConvertible).ToInt32(CultureInfo.InvariantCulture) & ~flag.ToInt32(CultureInfo.InvariantCulture); } else { AssetValue = (AssetValue as IConvertible).ToInt64() & ~flag.ToInt64(); } } }; } else { // TODO: right now, this assumes everything that is not a flags enum is a float // TODO: find a better way to create a text field - maybe something similar to UICheckBoxExt from KianCommons? var propertyHelper = new UIHelper(parent); var propertyTextField = propertyHelper.AddTextfield("wawa", (AssetValue as float?).ToString(), (_) => { }, (_) => { }) as UITextField; var labelObject = parent.Find <UILabel>("Label"); labelObject.parent.RemoveUIComponent(labelObject); Destroy(labelObject.gameObject); (propertyTextField.parent as UIPanel).autoFitChildrenHorizontally = true; (propertyTextField.parent as UIPanel).autoFitChildrenVertically = true; propertyTextField.numericalOnly = true; propertyTextField.allowFloats = true; propertyTextField.allowNegative = true; propertyTextField.eventTextSubmitted += (_, value) => { float newValue = (float)LenientStringToDouble(value, (double)(float)AssetValue); propertyTextField.text = newValue.ToString(); if (newValue != (float)AssetValue) { AssetValue = newValue; } }; } }
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); }
[System.Security.SecuritySafeCritical] // auto-generated internal static void MarshalHelperConvertObjectToVariant(Object o, ref Variant v) { #if FEATURE_REMOTING IConvertible ic = System.Runtime.Remoting.RemotingServices.IsTransparentProxy(o) ? null : o as IConvertible; #else IConvertible ic = o as IConvertible; #endif if (o == null) { v = Empty; } else if (ic == null) { // This path should eventually go away. But until // the work is done to have all of our wrapper types implement // IConvertible, this is a cheapo way to get the work done. v = new Variant(o); } else { IFormatProvider provider = CultureInfo.InvariantCulture; switch (ic.GetTypeCode()) { case TypeCode.Empty: v = Empty; break; case TypeCode.Object: v = new Variant((Object)o); break; case TypeCode.DBNull: v = DBNull; break; case TypeCode.Boolean: v = new Variant(ic.ToBoolean(provider)); break; case TypeCode.Char: v = new Variant(ic.ToChar(provider)); break; case TypeCode.SByte: v = new Variant(ic.ToSByte(provider)); break; case TypeCode.Byte: v = new Variant(ic.ToByte(provider)); break; case TypeCode.Int16: v = new Variant(ic.ToInt16(provider)); break; case TypeCode.UInt16: v = new Variant(ic.ToUInt16(provider)); break; case TypeCode.Int32: v = new Variant(ic.ToInt32(provider)); break; case TypeCode.UInt32: v = new Variant(ic.ToUInt32(provider)); break; case TypeCode.Int64: v = new Variant(ic.ToInt64(provider)); break; case TypeCode.UInt64: v = new Variant(ic.ToUInt64(provider)); break; case TypeCode.Single: v = new Variant(ic.ToSingle(provider)); break; case TypeCode.Double: v = new Variant(ic.ToDouble(provider)); break; case TypeCode.Decimal: v = new Variant(ic.ToDecimal(provider)); break; case TypeCode.DateTime: v = new Variant(ic.ToDateTime(provider)); break; case TypeCode.String: v = new Variant(ic.ToString(provider)); break; default: throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnknownTypeCode", ic.GetTypeCode())); } } }
public 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; } }
[System.Security.SecurityCritical] // auto-generated internal static void MarshalHelperCastVariant(Object pValue, int vt, ref Variant v) { IConvertible iv = pValue as IConvertible; if (iv == null) { switch (vt) { case 9: /*VT_DISPATCH*/ v = new Variant(new DispatchWrapper(pValue)); break; case 12: /*VT_VARIANT*/ v = new Variant(pValue); break; case 13: /*VT_UNKNOWN*/ v = new Variant(new UnknownWrapper(pValue)); break; case 36: /*VT_RECORD*/ v = new Variant(pValue); break; case 8: /*VT_BSTR*/ if (pValue == null) { v = new Variant(null); v.m_flags = CV_STRING; } else { throw new InvalidCastException(Environment.GetResourceString("InvalidCast_CannotCoerceByRefVariant")); } break; default: throw new InvalidCastException(Environment.GetResourceString("InvalidCast_CannotCoerceByRefVariant")); } } else { IFormatProvider provider = CultureInfo.InvariantCulture; switch (vt) { case 0: /*VT_EMPTY*/ v = Empty; break; case 1: /*VT_NULL*/ v = DBNull; break; case 2: /*VT_I2*/ v = new Variant(iv.ToInt16(provider)); break; case 3: /*VT_I4*/ v = new Variant(iv.ToInt32(provider)); break; case 4: /*VT_R4*/ v = new Variant(iv.ToSingle(provider)); break; case 5: /*VT_R8*/ v = new Variant(iv.ToDouble(provider)); break; case 6: /*VT_CY*/ v = new Variant(new CurrencyWrapper(iv.ToDecimal(provider))); break; case 7: /*VT_DATE*/ v = new Variant(iv.ToDateTime(provider)); break; case 8: /*VT_BSTR*/ v = new Variant(iv.ToString(provider)); break; case 9: /*VT_DISPATCH*/ v = new Variant(new DispatchWrapper((Object)iv)); break; case 10: /*VT_ERROR*/ v = new Variant(new ErrorWrapper(iv.ToInt32(provider))); break; case 11: /*VT_BOOL*/ v = new Variant(iv.ToBoolean(provider)); break; case 12: /*VT_VARIANT*/ v = new Variant((Object)iv); break; case 13: /*VT_UNKNOWN*/ v = new Variant(new UnknownWrapper((Object)iv)); break; case 14: /*VT_DECIMAL*/ v = new Variant(iv.ToDecimal(provider)); break; // case 15: /*unused*/ // NOT SUPPORTED case 16: /*VT_I1*/ v = new Variant(iv.ToSByte(provider)); break; case 17: /*VT_UI1*/ v = new Variant(iv.ToByte(provider)); break; case 18: /*VT_UI2*/ v = new Variant(iv.ToUInt16(provider)); break; case 19: /*VT_UI4*/ v = new Variant(iv.ToUInt32(provider)); break; case 20: /*VT_I8*/ v = new Variant(iv.ToInt64(provider)); break; case 21: /*VT_UI8*/ v = new Variant(iv.ToUInt64(provider)); break; case 22: /*VT_INT*/ v = new Variant(iv.ToInt32(provider)); break; case 23: /*VT_UINT*/ v = new Variant(iv.ToUInt32(provider)); break; default: throw new InvalidCastException(Environment.GetResourceString("InvalidCast_CannotCoerceByRefVariant")); } } }
private bool TryConvert(IConvertible convertible, Type target, out object result) { if (target == typeof(bool)) { result = convertible.ToBoolean(CultureInfo.CurrentCulture); return(true); } if (target == typeof(byte)) { result = convertible.ToByte(CultureInfo.CurrentCulture); return(true); } if (target == typeof(char)) { result = convertible.ToChar(CultureInfo.CurrentCulture); return(true); } if (target == typeof(DateTime)) { result = convertible.ToDateTime(CultureInfo.CurrentCulture); return(true); } if (target == typeof(decimal)) { result = convertible.ToDecimal(CultureInfo.CurrentCulture); return(true); } if (target == typeof(double)) { result = convertible.ToDouble(CultureInfo.CurrentCulture); return(true); } if (target == typeof(short)) { result = convertible.ToInt16(CultureInfo.CurrentCulture); return(true); } if (target == typeof(int)) { result = convertible.ToInt32(CultureInfo.CurrentCulture); return(true); } if (target == typeof(long)) { result = convertible.ToInt64(CultureInfo.CurrentCulture); return(true); } if (target == typeof(sbyte)) { result = convertible.ToSByte(CultureInfo.CurrentCulture); return(true); } if (target == typeof(float)) { result = convertible.ToSingle(CultureInfo.CurrentCulture); return(true); } if (target == typeof(ushort)) { result = convertible.ToUInt16(CultureInfo.CurrentCulture); return(true); } if (target == typeof(uint)) { result = convertible.ToUInt32(CultureInfo.CurrentCulture); return(true); } if (target == typeof(ulong)) { result = convertible.ToUInt64(CultureInfo.CurrentCulture); return(true); } if (target == typeof(string)) { result = convertible.ToString(CultureInfo.CurrentCulture); return(true); } result = default; return(false); }
public override long convert(IConvertible value) => value.ToInt64(null);
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; } }
public static string ToString(object value) { if (value == null) { return(null); } string str2 = value as string; if (str2 != null) { return(str2); } IConvertible convertible = value as IConvertible; if (convertible != null) { switch (convertible.GetTypeCode()) { case TypeCode.Boolean: return(ToString(convertible.ToBoolean(null))); case TypeCode.Char: return(ToString(convertible.ToChar(null))); case TypeCode.SByte: return(ToString((int)convertible.ToSByte(null))); case TypeCode.Byte: return(ToString(convertible.ToByte(null))); case TypeCode.Int16: return(ToString((int)convertible.ToInt16(null))); case TypeCode.UInt16: return(ToString((uint)convertible.ToUInt16(null))); case TypeCode.Int32: return(ToString(convertible.ToInt32(null))); case TypeCode.UInt32: return(ToString(convertible.ToUInt32(null))); case TypeCode.Int64: return(ToString(convertible.ToInt64(null))); case TypeCode.UInt64: return(ToString(convertible.ToUInt64(null))); case TypeCode.Single: return(ToString(convertible.ToSingle(null))); case TypeCode.Double: return(ToString(convertible.ToDouble(null))); case TypeCode.Decimal: return(ToString(convertible.ToDecimal(null))); case TypeCode.DateTime: return(ToString(convertible.ToDateTime(null))); case (TypeCode.DateTime | TypeCode.Object): return(null); case TypeCode.String: return(convertible.ToString(null)); } } else { char[] chArray = value as char[]; if (chArray != null) { return(new string(chArray)); } } return(convertible.ToString()); }
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; } }
/// <summary> /// Converts object value to invariant format (understood by JavaScript) /// </summary> /// <param name="value">Object value</param> /// <param name="objTypeCode">Object TypeCode</param> /// <param name="safeConversion">Check and remove unusual unicode characters from the result string.</param> /// <returns>Object value converted to string</returns> internal static string XmlConvertToString(IConvertible value, TypeCode objTypeCode, bool safeConversion = false) { if (value == null) { return("null"); } switch (objTypeCode) { case TypeCode.Boolean: return(XmlConvert.ToString(value.ToBoolean(CultureInfo.InvariantCulture))); // boolean as lowercase case TypeCode.Byte: return(XmlConvert.ToString(value.ToByte(CultureInfo.InvariantCulture))); case TypeCode.SByte: return(XmlConvert.ToString(value.ToSByte(CultureInfo.InvariantCulture))); case TypeCode.Int16: return(XmlConvert.ToString(value.ToInt16(CultureInfo.InvariantCulture))); case TypeCode.Int32: return(XmlConvert.ToString(value.ToInt32(CultureInfo.InvariantCulture))); case TypeCode.Int64: return(XmlConvert.ToString(value.ToInt64(CultureInfo.InvariantCulture))); case TypeCode.UInt16: return(XmlConvert.ToString(value.ToUInt16(CultureInfo.InvariantCulture))); case TypeCode.UInt32: return(XmlConvert.ToString(value.ToUInt32(CultureInfo.InvariantCulture))); case TypeCode.UInt64: return(XmlConvert.ToString(value.ToUInt64(CultureInfo.InvariantCulture))); case TypeCode.Single: { float singleValue = value.ToSingle(CultureInfo.InvariantCulture); return(float.IsInfinity(singleValue) ? Convert.ToString(singleValue, CultureInfo.InvariantCulture) : XmlConvert.ToString(singleValue)); } case TypeCode.Double: { double doubleValue = value.ToDouble(CultureInfo.InvariantCulture); return(double.IsInfinity(doubleValue) ? Convert.ToString(doubleValue, CultureInfo.InvariantCulture) : XmlConvert.ToString(doubleValue)); } case TypeCode.Decimal: return(XmlConvert.ToString(value.ToDecimal(CultureInfo.InvariantCulture))); case TypeCode.DateTime: return(XmlConvert.ToString(value.ToDateTime(CultureInfo.InvariantCulture), XmlDateTimeSerializationMode.Utc)); case TypeCode.Char: return(XmlConvert.ToString(value.ToChar(CultureInfo.InvariantCulture))); case TypeCode.String: return(safeConversion ? RemoveInvalidXmlChars(value.ToString(CultureInfo.InvariantCulture)) : value.ToString(CultureInfo.InvariantCulture)); default: return(XmlConvertToStringInvariant(value, safeConversion)); } }
/// <summary> /// Query KhronoApi derived class enumeration names. /// </summary> /// <param name="khronoApiType"> /// A <see cref="Type"/> that specifies the type of the class where to query enumeration names. /// </param> /// <returns> /// It returns a <see cref="Dictionary{Int32, String}"/> that correlates the enumeration value with /// the enumeration name. /// </returns> protected static LogContext QueryLogContext(Type khronoApiType) { if (khronoApiType == null) { throw new ArgumentNullException("khronoApiType"); } LogContext logContext = new LogContext(); Dictionary <Int64, string> enumNames = new Dictionary <Int64, string>(); Dictionary <string, Dictionary <Int64, string> > enumBitmasks = new Dictionary <string, Dictionary <Int64, string> >(); FieldInfo[] fieldInfos = khronoApiType.GetFields(BindingFlags.Public | BindingFlags.Static); foreach (FieldInfo fieldInfo in fieldInfos) { // Enumeration values are defined as const fields if (fieldInfo.IsLiteral == false) { continue; } // Enumeration values have at least one RequiredByFeatureAttribute Attribute[] requiredByFeatureAttribs = fieldInfo.GetCustomAttributes <RequiredByFeatureAttribute>().ToArray(); if ((requiredByFeatureAttribs == null) || (requiredByFeatureAttribs.Length == 0)) { continue; } LogAttribute logAttribute = fieldInfo.GetCustomAttribute <LogAttribute>(); IConvertible fieldInfoValue = (IConvertible)fieldInfo.GetValue(null); Int64 enumValueKey = fieldInfoValue.ToInt64(System.Globalization.NumberFormatInfo.InvariantInfo); // Pure enum if ((logAttribute == null) || (logAttribute.BitmaskName == null)) { // Collect enumeration if (enumNames.ContainsKey(enumValueKey) == false) { enumNames.Add(enumValueKey, fieldInfo.Name); } } // Bitmask enum if ((logAttribute != null) && (logAttribute.BitmaskName != null)) { Dictionary <Int64, string> enumBitmaskNames; if (enumBitmasks.TryGetValue(logAttribute.BitmaskName, out enumBitmaskNames) == false) { enumBitmaskNames = new Dictionary <long, string>(); enumBitmasks.Add(logAttribute.BitmaskName, enumBitmaskNames); } if (enumBitmaskNames.ContainsKey(enumValueKey) == false) { enumBitmaskNames.Add(enumValueKey, fieldInfo.Name); } } } // Componse LogContext logContext.EnumNames = enumNames; logContext.EnumBitmasks = enumBitmasks; return(logContext); }
/// <summary> 将 数字 类型对象转换Json字符串写入Buffer /// </summary> /// <param name="number">数字对象</param> protected virtual void AppendNumber(IConvertible number) { switch (number.GetTypeCode()) { case TypeCode.Decimal: case TypeCode.Double: case TypeCode.Single: if (QuotWrapNumber) { Buffer.Append('"'); Buffer.Append(number.ToString(CultureInfo.InvariantCulture)); Buffer.Append('"'); } else { Buffer.Append(number.ToString(CultureInfo.InvariantCulture)); } break; case TypeCode.Int16: if (QuotWrapNumber) { Buffer.Append('"'); Buffer.Append(number.ToInt16(CultureInfo.InvariantCulture)); Buffer.Append('"'); } else { Buffer.Append(number.ToInt16(CultureInfo.InvariantCulture)); } break; case TypeCode.Int32: case TypeCode.Int64: if (QuotWrapNumber) { Buffer.Append('"'); Buffer.Append(number.ToInt64(CultureInfo.InvariantCulture)); Buffer.Append('"'); } else { Buffer.Append(number.ToInt64(CultureInfo.InvariantCulture)); } break; case TypeCode.SByte: if (QuotWrapNumber) { Buffer.Append('"'); Buffer.Append(number.ToSByte(CultureInfo.InvariantCulture)); Buffer.Append('"'); } else { Buffer.Append(number.ToSByte(CultureInfo.InvariantCulture)); } break; case TypeCode.Byte: if (QuotWrapNumber) { Buffer.Append('"'); Buffer.Append(number.ToByte(CultureInfo.InvariantCulture)); Buffer.Append('"'); } else { Buffer.Append(number.ToByte(CultureInfo.InvariantCulture)); } break; case TypeCode.UInt16: if (QuotWrapNumber) { Buffer.Append('"'); Buffer.Append(number.ToUInt16(CultureInfo.InvariantCulture)); Buffer.Append('"'); } else { Buffer.Append(number.ToUInt16(CultureInfo.InvariantCulture)); } break; case TypeCode.UInt32: case TypeCode.UInt64: if (QuotWrapNumber) { Buffer.Append('"'); Buffer.Append(number.ToUInt64(CultureInfo.InvariantCulture)); Buffer.Append('"'); } else { Buffer.Append(number.ToUInt64(CultureInfo.InvariantCulture)); } break; default: break; } }
/// <summary> /// Converts the <see cref="Object"/> to its JSON string representation. /// </summary> /// <param name="value">The value to convert.</param> /// <returns>A JSON string representation of the <see cref="Object"/>.</returns> public static string ToString(object value) { if (value == null) { return(Null); } IConvertible convertible = value as IConvertible; if (convertible != null) { switch (convertible.GetTypeCode()) { case TypeCode.String: return(ToString(convertible.ToString(CultureInfo.InvariantCulture))); case TypeCode.Char: return(ToString(convertible.ToChar(CultureInfo.InvariantCulture))); case TypeCode.Boolean: return(ToString(convertible.ToBoolean(CultureInfo.InvariantCulture))); case TypeCode.SByte: return(ToString(convertible.ToSByte(CultureInfo.InvariantCulture))); case TypeCode.Int16: return(ToString(convertible.ToInt16(CultureInfo.InvariantCulture))); case TypeCode.UInt16: return(ToString(convertible.ToUInt16(CultureInfo.InvariantCulture))); case TypeCode.Int32: return(ToString(convertible.ToInt32(CultureInfo.InvariantCulture))); case TypeCode.Byte: return(ToString(convertible.ToByte(CultureInfo.InvariantCulture))); case TypeCode.UInt32: return(ToString(convertible.ToUInt32(CultureInfo.InvariantCulture))); case TypeCode.Int64: return(ToString(convertible.ToInt64(CultureInfo.InvariantCulture))); case TypeCode.UInt64: return(ToString(convertible.ToUInt64(CultureInfo.InvariantCulture))); case TypeCode.Single: return(ToString(convertible.ToSingle(CultureInfo.InvariantCulture))); case TypeCode.Double: return(ToString(convertible.ToDouble(CultureInfo.InvariantCulture))); case TypeCode.DateTime: return(ToString(convertible.ToDateTime(CultureInfo.InvariantCulture))); case TypeCode.Decimal: return(ToString(convertible.ToDecimal(CultureInfo.InvariantCulture))); case TypeCode.DBNull: return(Null); } } #if !PocketPC && !NET20 else if (value is DateTimeOffset) { return(ToString((DateTimeOffset)value)); } #endif else if (value is Guid) { return(ToString((Guid)value)); } else if (value is Uri) { return(ToString((Uri)value)); } else if (value is TimeSpan) { return(ToString((TimeSpan)value)); } throw new ArgumentException("Unsupported type: {0}. Use the JsonSerializer class to get the object's JSON representation.".FormatWith(CultureInfo.InvariantCulture, value.GetType())); }
private void AppendConvertible(IConvertible obj) { var @enum = obj as Enum; if (@enum != null) { AppendEnum(@enum); return; } if (obj is IToJson) { AppendCheckLoopRef(obj); return; } switch (obj.GetTypeCode()) { case TypeCode.Boolean: AppendBoolean(obj.ToBoolean(CultureInfo.InvariantCulture)); break; case TypeCode.Byte: AppendByte(obj.ToByte(CultureInfo.InvariantCulture)); break; case TypeCode.Char: AppendChar(obj.ToChar(CultureInfo.InvariantCulture)); break; case TypeCode.DateTime: AppendDateTime(obj.ToDateTime(CultureInfo.InvariantCulture)); break; case TypeCode.Decimal: AppendDecimal(obj.ToDecimal(CultureInfo.InvariantCulture)); break; case TypeCode.Double: AppendDouble(obj.ToDouble(CultureInfo.InvariantCulture)); break; case TypeCode.Int16: AppendInt16(obj.ToInt16(CultureInfo.InvariantCulture)); break; case TypeCode.Int32: AppendInt32(obj.ToInt32(CultureInfo.InvariantCulture)); break; case TypeCode.Int64: AppendInt64(obj.ToInt64(CultureInfo.InvariantCulture)); break; case TypeCode.SByte: AppendSByte(obj.ToSByte(CultureInfo.InvariantCulture)); break; case TypeCode.Single: AppendSingle(obj.ToSingle(CultureInfo.InvariantCulture)); break; case TypeCode.UInt16: AppendUInt16(obj.ToUInt16(CultureInfo.InvariantCulture)); break; case TypeCode.UInt32: AppendUInt32(obj.ToUInt32(CultureInfo.InvariantCulture)); break; case TypeCode.UInt64: AppendUInt64(obj.ToUInt64(CultureInfo.InvariantCulture)); break; default: AppendCheckLoopRef(obj); break; } }
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 }
private Object EvaluateNumericBinary(Object v1, Object v2, JSToken operatorTok) { IConvertible ic1 = Convert.GetIConvertible(v1); IConvertible ic2 = Convert.GetIConvertible(v2); TypeCode t1 = Convert.GetTypeCode(v1, ic1); TypeCode t2 = Convert.GetTypeCode(v2, ic2); switch (t1) { case TypeCode.Empty: return(Double.NaN); case TypeCode.DBNull: return(this.EvaluateNumericBinary(0, v2, operatorTok)); case TypeCode.Char: { Object result; int val = ic1.ToInt32(null); switch (t2) { case TypeCode.Empty: return(Double.NaN); case TypeCode.DBNull: return(NumericBinary.DoOp(val, 0, operatorTok)); case TypeCode.Boolean: case TypeCode.Char: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: result = NumericBinary.DoOp(val, ic2.ToInt32(null), operatorTok); break; case TypeCode.UInt32: case TypeCode.Int64: result = NumericBinary.DoOp((long)val, ic2.ToInt64(null), operatorTok); break; case TypeCode.UInt64: result = NumericBinary.DoOp((double)val, ic2.ToDouble(null), operatorTok); break; case TypeCode.Single: case TypeCode.Double: result = NumericBinary.DoOp((double)ic1.ToInt32(null), ic2.ToDouble(null), operatorTok); break; case TypeCode.String: result = NumericBinary.DoOp(val, Convert.ToNumber(v2, ic2), operatorTok); break; case TypeCode.Object: case TypeCode.Decimal: case TypeCode.DateTime: default: result = null; break; } if (this.operatorTok == JSToken.Minus && result != null && t2 != TypeCode.Char) { return(Convert.Coerce2(result, TypeCode.Char, false)); } else if (result != null) { return(result); } break; } case TypeCode.Boolean: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: { int val = ic1.ToInt32(null); switch (t2) { case TypeCode.Empty: return(Double.NaN); case TypeCode.DBNull: return(NumericBinary.DoOp(val, 0, operatorTok)); case TypeCode.Boolean: case TypeCode.Char: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: return(NumericBinary.DoOp(val, ic2.ToInt32(null), operatorTok)); case TypeCode.UInt32: case TypeCode.Int64: return(NumericBinary.DoOp((long)val, ic2.ToInt64(null), operatorTok)); case TypeCode.UInt64: if (val >= 0) { return(NumericBinary.DoOp((ulong)val, ic2.ToUInt64(null), operatorTok)); } else { return(NumericBinary.DoOp((double)val, ic2.ToDouble(null), operatorTok)); } case TypeCode.Single: case TypeCode.Double: return(NumericBinary.DoOp((double)val, ic2.ToDouble(null), operatorTok)); case TypeCode.Object: case TypeCode.Decimal: case TypeCode.DateTime: case TypeCode.String: break; } break; } case TypeCode.UInt32: { uint val = ic1.ToUInt32(null); switch (t2) { case TypeCode.Empty: return(Double.NaN); case TypeCode.DBNull: return(NumericBinary.DoOp(val, 0, operatorTok)); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.Int32: int val2 = ic2.ToInt32(null); if (val2 >= 0) { return(NumericBinary.DoOp(val, (uint)val2, operatorTok)); } else { return(NumericBinary.DoOp((long)val, (long)val2, operatorTok)); } case TypeCode.Int64: return(NumericBinary.DoOp((long)val, ic2.ToInt64(null), operatorTok)); case TypeCode.Boolean: case TypeCode.Char: case TypeCode.UInt16: case TypeCode.UInt32: return(NumericBinary.DoOp(val, ic2.ToUInt32(null), operatorTok)); case TypeCode.UInt64: return(NumericBinary.DoOp((ulong)val, ic2.ToUInt64(null), operatorTok)); case TypeCode.Single: case TypeCode.Double: return(NumericBinary.DoOp((double)val, ic2.ToDouble(null), operatorTok)); case TypeCode.Object: case TypeCode.Decimal: case TypeCode.DateTime: case TypeCode.String: break; } break; } case TypeCode.Int64: { long val = ic1.ToInt64(null); switch (t2) { case TypeCode.Empty: return(Double.NaN); case TypeCode.DBNull: return(NumericBinary.DoOp(val, 0, operatorTok)); case TypeCode.Boolean: case TypeCode.Char: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: case TypeCode.UInt32: case TypeCode.Int64: return(NumericBinary.DoOp(val, ic2.ToInt64(null), operatorTok)); case TypeCode.UInt64: if (val >= 0) { return(NumericBinary.DoOp((ulong)val, ic2.ToUInt64(null), operatorTok)); } else { return(NumericBinary.DoOp((double)val, ic2.ToDouble(null), operatorTok)); } case TypeCode.Single: case TypeCode.Double: return(NumericBinary.DoOp((double)val, ic2.ToDouble(null), operatorTok)); case TypeCode.Object: case TypeCode.Decimal: case TypeCode.DateTime: case TypeCode.String: break; } break; } case TypeCode.UInt64: { ulong val = ic1.ToUInt64(null); switch (t2) { case TypeCode.Empty: return(Double.NaN); case TypeCode.DBNull: return(NumericBinary.DoOp(val, 0, operatorTok)); case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: long val2 = ic2.ToInt64(null); if (val2 >= 0) { return(NumericBinary.DoOp(val, (ulong)val2, operatorTok)); } else { return(NumericBinary.DoOp((double)val, (double)val2, operatorTok)); } case TypeCode.Boolean: case TypeCode.Char: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: return(NumericBinary.DoOp(val, ic2.ToUInt64(null), operatorTok)); case TypeCode.Single: case TypeCode.Double: return(NumericBinary.DoOp((double)val, ic2.ToDouble(null), operatorTok)); case TypeCode.Object: case TypeCode.Decimal: case TypeCode.DateTime: case TypeCode.String: break; } break; } case TypeCode.Single: case TypeCode.Double: { double d = ic1.ToDouble(null); switch (t2) { case TypeCode.Empty: return(Double.NaN); case TypeCode.DBNull: return(NumericBinary.DoOp(d, 0, operatorTok)); case TypeCode.Boolean: case TypeCode.Char: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: case TypeCode.Int32: return(NumericBinary.DoOp(d, (double)ic2.ToInt32(null), operatorTok)); case TypeCode.UInt32: case TypeCode.Int64: case TypeCode.UInt64: case TypeCode.Single: case TypeCode.Double: return(NumericBinary.DoOp(d, ic2.ToDouble(null), operatorTok)); case TypeCode.Object: case TypeCode.Decimal: case TypeCode.DateTime: case TypeCode.String: break; } break; } case TypeCode.Object: case TypeCode.Decimal: case TypeCode.DateTime: case TypeCode.String: break; } if (v2 == null) { return(Double.NaN); } MethodInfo oper = this.GetOperator(v1.GetType(), v2.GetType()); if (oper != null) { return(oper.Invoke(null, (BindingFlags)0, JSBinder.ob, new Object[] { v1, v2 }, null)); } else { return(NumericBinary.DoOp(v1, v2, ic1, ic2, operatorTok)); } }
// 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"); } }
long IConvertible.ToInt64(IFormatProvider provider) { IConvertible conv = (IConvertible)value; return(conv.ToInt64(provider)); }
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 Object DefaultToType(IConvertible value, Type targetType, IFormatProvider provider) { Debug.Assert(value != null, "[Convert.DefaultToType]value!=null"); if (targetType == null) { throw new ArgumentNullException(nameof(targetType)); } if (ReferenceEquals(value.GetType(), targetType)) { return(value); } if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Boolean])) { return(value.ToBoolean(provider)); } if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Char])) { return(value.ToChar(provider)); } if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.SByte])) { return(value.ToSByte(provider)); } if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Byte])) { return(value.ToByte(provider)); } if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Int16])) { return(value.ToInt16(provider)); } if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.UInt16])) { return(value.ToUInt16(provider)); } if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Int32])) { return(value.ToInt32(provider)); } if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.UInt32])) { return(value.ToUInt32(provider)); } if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Int64])) { return(value.ToInt64(provider)); } if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.UInt64])) { return(value.ToUInt64(provider)); } if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Single])) { return(value.ToSingle(provider)); } if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Double])) { return(value.ToDouble(provider)); } if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Decimal])) { return(value.ToDecimal(provider)); } if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.DateTime])) { return(value.ToDateTime(provider)); } if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.String])) { return(value.ToString(provider)); } if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Object])) { return((Object)value); } // Need to special case Enum because typecode will be underlying type, e.g. Int32 if (ReferenceEquals(targetType, EnumType)) { return((Enum)value); } if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.DBNull])) { throw new InvalidCastException("Object cannot be cast to DBNull."); } // TODO: SR //throw new InvalidCastException(SR.InvalidCast_DBNull); if (ReferenceEquals(targetType, ConvertTypes[(int)TypeCode.Empty])) { throw new InvalidCastException("Object cannot be cast to Empty."); } // TODO: SR //throw new InvalidCastException(SR.InvalidCast_Empty); throw new InvalidCastException(string.Format("Invalid cast from '{0}' to '{1}'.", value.GetType().FullName, targetType.FullName)); // TODO: SR //throw new InvalidCastException(string.Format(SR.InvalidCast_FromTo, value.GetType().FullName, targetType.FullName)); }
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)); }
private Boolean CallInterfaceMethods(Enum en) { Boolean pass = true; IConvertible icon1 = (IConvertible)en; if (!icon1.ToBoolean(null)) { pass = false; } if (icon1.ToByte(null) != 1) { pass = false; } if (icon1.ToChar(null) != (Char)1) { pass = false; } try{ icon1.ToDateTime(null); pass = false; }catch (InvalidCastException) { }catch (Exception) { pass = false; } if (icon1.ToDecimal(null) != 1) { pass = false; } if (icon1.ToDouble(null) != 1) { pass = false; } if (icon1.ToInt16(null) != 1) { pass = false; } if (icon1.ToInt32(null) != 1) { pass = false; } if (icon1.ToInt64(null) != 1) { pass = false; } if (icon1.ToSByte(null) != 1) { pass = false; } if (icon1.ToSingle(null) != 1f) { pass = false; } if (icon1.ToString(null) != "ONE") { pass = false; } if (icon1.ToUInt16(null) != 1) { pass = false; } if (icon1.ToUInt32(null) != 1) { pass = false; } if (icon1.ToUInt64(null) != 1) { pass = false; } return(pass); }
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); }
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 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); }
/// <summary> /// Returns the canonical form of an expression that results in the given constant at runtime. /// </summary> /// <param name="convertible">The value that the resulting expression must evaluate to at runtime.</param> /// <param name="originalInstruction">An instruction that will result in the given constant at runtime. The result of this method is a canonical form of this instruction.</param> private Instruction GetAsLoadConstant(IConvertible convertible, Instruction originalInstruction) { Contract.Requires(convertible != null); Contract.Requires(originalInstruction != null); Contract.Ensures(Contract.Result <Instruction>() != null); Instruction result = originalInstruction; var operation = originalInstruction.Operation; var location = originalInstruction.Operation.Location; TypeCode tc = convertible.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 = convertible.ToInt64(null); if (int.MinValue <= n && n <= int.MaxValue) { result = new Instruction() { Operation = new Operation() { OperationCode = OperationCode.Ldc_I4, Value = (int)n, Location = location } } } ; else { result = new Instruction() { Operation = new Operation() { OperationCode = OperationCode.Ldc_I8, Value = n, Location = location } } }; break; case TypeCode.UInt64: result = new Instruction() { Operation = new Operation() { OperationCode = OperationCode.Ldc_I8, Value = (long)convertible.ToUInt64(null), Location = location } }; break; case TypeCode.Single: result = new Instruction() { Operation = new Operation() { OperationCode = OperationCode.Ldc_R4, Value = convertible.ToSingle(null), Location = location } }; break; case TypeCode.Double: result = new Instruction() { Operation = new Operation() { OperationCode = OperationCode.Ldc_R8, Value = convertible.ToDouble(null), Location = location } }; break; case TypeCode.String: result = new Instruction() { Operation = new Operation() { OperationCode = OperationCode.Ldstr, Value = convertible.ToString(null), Location = location } }; break; } result.Type = originalInstruction.Type; return(this.GetCanonicalExpression(result)); }
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); }
long GetInt64(int i) { IConvertible c = (IConvertible)GetValue(i); return(c.ToInt64(CultureInfo.CurrentCulture)); }
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 }
/// <summary>Returns a <see langword="Double" /> value that corresponds to the specified object. </summary> /// <param name="Value">Required. Object to convert to a <see langword="Double" /> value.</param> /// <param name="NumberFormat">A <see cref="T:System.Globalization.NumberFormatInfo" /> object that defines how numeric values are formatted and displayed, depending on the culture.</param> /// <returns>The <see langword="Double" /> value corresponding to <paramref name="Value" />.</returns> public static double FromObject(object Value, NumberFormatInfo NumberFormat) { if (Value == null) { return(0.0); } IConvertible ValueInterface = Value as IConvertible; if (ValueInterface != null) { switch (ValueInterface.GetTypeCode()) { case TypeCode.Boolean: return((double)-(ValueInterface.ToBoolean((IFormatProvider)null) ? 1 : 0)); case TypeCode.Byte: if (Value is byte) { return((double)(byte)Value); } return((double)ValueInterface.ToByte((IFormatProvider)null)); case TypeCode.Int16: if (Value is short) { return((double)(short)Value); } return((double)ValueInterface.ToInt16((IFormatProvider)null)); case TypeCode.Int32: if (Value is int) { return((double)(int)Value); } return((double)ValueInterface.ToInt32((IFormatProvider)null)); case TypeCode.Int64: if (Value is long) { return((double)(long)Value); } return((double)ValueInterface.ToInt64((IFormatProvider)null)); case TypeCode.Single: if (Value is float) { return((double)(float)Value); } return((double)ValueInterface.ToSingle((IFormatProvider)null)); case TypeCode.Double: if (Value is double) { return((double)Value); } return(ValueInterface.ToDouble((IFormatProvider)null)); case TypeCode.Decimal: return(DoubleType.DecimalToDouble(ValueInterface)); case TypeCode.String: return(DoubleType.FromString(ValueInterface.ToString((IFormatProvider)null), NumberFormat)); } } throw new InvalidCastException(Utils.GetResourceString("InvalidCast_FromTo", Utils.VBFriendlyName(Value), "Double")); }
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 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 }
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); }
private static object InternalNegObj(object obj, IConvertible conv, TypeCode tc) { switch (tc) { case TypeCode.Empty: return 0; case TypeCode.Boolean: if (obj is bool) { return -((short) -(((bool) obj) > false)); } return -((short) -(conv.ToBoolean(null) > false)); case TypeCode.Byte: if (obj is byte) { return (short) -((byte) obj); } return (short) -conv.ToByte(null); case TypeCode.Int16: int num4; if (obj is short) { num4 = 0 - ((short) obj); } else { num4 = 0 - conv.ToInt16(null); } if ((num4 >= -32768) && (num4 <= 0x7fff)) { return (short) num4; } return num4; case TypeCode.Int32: long num5; if (obj is int) { num5 = 0L - ((int) obj); } else { num5 = 0L - conv.ToInt32(null); } if ((num5 >= -2147483648L) && (num5 <= 0x7fffffffL)) { return (int) num5; } return num5; case TypeCode.Int64: try { if (obj is long) { return (0L - ((long) obj)); } return (0L - conv.ToInt64(null)); } catch (StackOverflowException exception) { throw exception; } catch (OutOfMemoryException exception2) { throw exception2; } catch (ThreadAbortException exception3) { throw exception3; } catch (Exception) { return decimal.Negate(conv.ToDecimal(null)); } break; case TypeCode.Single: goto Label_01B9; case TypeCode.Double: if (obj is double) { return -((double) obj); } return -conv.ToDouble(null); case TypeCode.Decimal: break; case TypeCode.String: { string str = obj as string; if (str == null) { return -DoubleType.FromString(conv.ToString(null)); } return -DoubleType.FromString(str); } default: throw GetNoValidOperatorException(obj); } try { if (obj is decimal) { return decimal.Negate((decimal) obj); } return decimal.Negate(conv.ToDecimal(null)); } catch (StackOverflowException exception4) { throw exception4; } catch (OutOfMemoryException exception5) { throw exception5; } catch (ThreadAbortException exception6) { throw exception6; } catch (Exception) { return -conv.ToDouble(null); } Label_01B9: if (obj is float) { return -((float) obj); } return -conv.ToSingle(null); }