public IComparable EvaluateComparable(IEvaluateContext context) { IConvertible result = Evaluate(context); if (result == null) { throw new LoadDataException("Could not evaluate " + _source); } if (result is EnumValue) { return((EnumValue)result); } if (result.GetTypeCode() == TypeCode.String) { return(result.ToString(CultureInfo.CurrentCulture)); } else if (result.GetTypeCode() == TypeCode.UInt32) { uint value = result.ToUInt32(CultureInfo.CurrentCulture); return((int)value); } try { return(result.ToInt32(CultureInfo.CurrentCulture)); } catch (OverflowException e) { throw new LoadDataException(e.Message); } }
// Convert an object into a date value. public static DateTime FromObject(Object Value) { #if !ECMA_COMPAT IConvertible ic = (Value as IConvertible); if (ic != null) { if (ic.GetTypeCode() != TypeCode.String) { return(ic.ToDateTime(null)); } else { return(FromString(ic.ToString(null))); } } #else if (Value is DateTime) { return((DateTime)Value); } #endif throw new InvalidCastException (String.Format (S._("VB_InvalidCast"), (Value != null ? Value.GetType().ToString() : "null"), "System.DateTime")); }
static void Main(string[] args) { var s = string.Empty; if ((s + "").Length == 0) { } IConvertible conv = s as IConvertible; if (conv != null) { switch (conv.GetTypeCode()) { case TypeCode.Boolean: break; case TypeCode.Int32: break; case TypeCode.Object: break; case TypeCode.String: break; default: break; } } }
// Convert an object into a char array value. public static char[] FromObject(Object Value) { if (Value != null) { if (Value is char[]) { return((char[])Value); } #if !ECMA_COMPAT IConvertible ic = (Value as IConvertible); if (ic != null && ic.GetTypeCode() == TypeCode.String) { return(ic.ToString(null).ToCharArray()); } #else if (Value is String) { return(((String)Value).ToCharArray()); } #endif else { throw new InvalidCastException (String.Format (S._("VB_InvalidCast"), Value.GetType(), "char[]")); } } else { return(new char [0]); } }
public static double ToNumber(object o) { if (o == null) { return(0); } if (o == Undefined) { return(Double.NaN); } IConvertible c = o as IConvertible; if (c == null) { // We may handle Nullable<> here but since // it is a mess, I give up. return(Double.NaN); } switch (c.GetTypeCode()) { case TypeCode.Boolean: return(ToNumber((bool)o)); case TypeCode.Char: case TypeCode.String: return(ToNumber((string)o)); case TypeCode.DateTime: return(ToNumber((DateTime)o)); default: return(Convert.ToDouble(o)); } }
TypeCode IConvertible.GetTypeCode() { if (_value == null) { return(TypeCode.Empty); } #if !NET20 if (_value is DateTimeOffset) { return(TypeCode.DateTime); } #endif #if !(NET20 || NET35 || PORTABLE40) if (_value is BigInteger) { return(TypeCode.Object); } #endif IConvertible convertable = _value as IConvertible; if (convertable == null) { return(TypeCode.Object); } return(convertable.GetTypeCode()); }
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); } }
public static bool IsIntegralMinusOne(ICompileTimeConstant constExpression) { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.SByte: return(ic.ToSByte(null) == -1); case System.TypeCode.Int16: return(ic.ToInt16(null) == -1); case System.TypeCode.Int32: return(ic.ToInt32(null) == -1); case System.TypeCode.Int64: return(ic.ToInt64(null) == -1); case System.TypeCode.Byte: return(ic.ToByte(null) == byte.MaxValue); case System.TypeCode.UInt16: return(ic.ToUInt16(null) == ushort.MaxValue); case System.TypeCode.UInt32: return(ic.ToUInt32(null) == uint.MaxValue); case System.TypeCode.UInt64: return(ic.ToUInt64(null) == ulong.MaxValue); } return(false); }
public static bool IsIntegralZero(ICompileTimeConstant constExpression) { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.SByte: return(ic.ToSByte(null) == 0); case System.TypeCode.Int16: return(ic.ToInt16(null) == 0); case System.TypeCode.Int32: return(ic.ToInt32(null) == 0); case System.TypeCode.Int64: return(ic.ToInt64(null) == 0); case System.TypeCode.Byte: return(ic.ToByte(null) == 0); case System.TypeCode.UInt16: return(ic.ToUInt16(null) == 0); case System.TypeCode.UInt32: return(ic.ToUInt32(null) == 0); case System.TypeCode.UInt64: return(ic.ToUInt64(null) == 0); case System.TypeCode.Boolean: return(!ic.ToBoolean(null)); } return(false); }
public static CompileTimeConstant /*?*/ SetBooleanTrue(ICompileTimeConstant constExpression) { Contract.Requires(constExpression != null); IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(null); } CompileTimeConstant result = new CompileTimeConstant(constExpression); switch (ic.GetTypeCode()) { case System.TypeCode.SByte: result.Value = (SByte)1; break; case System.TypeCode.Int16: result.Value = (Int16)1; break; case System.TypeCode.Int32: result.Value = (Int32)1; break; case System.TypeCode.Int64: result.Value = (Int64)1; break; case System.TypeCode.Byte: result.Value = (Byte)1; break; case System.TypeCode.UInt16: result.Value = (UInt16)1; break; case System.TypeCode.UInt32: result.Value = (UInt32)1; break; case System.TypeCode.UInt64: result.Value = (UInt64)1; break; default: return(null); } return(result); }
public static float FromObject (Object Value, NumberFormatInfo NumberFormat) { #if !ECMA_COMPAT if (Value != null) { IConvertible ic = (Value as IConvertible); if (ic != null) { if (ic.GetTypeCode() != TypeCode.String) { return(ic.ToSingle(NumberFormat)); } else { return(FromString(ic.ToString(null), NumberFormat)); } } else { throw new InvalidCastException (String.Format (S._("VB_InvalidCast"), Value.GetType(), "System.Single")); } } else { return(0.0f); } #else return((float)(DoubleType.FromObject(Value, NumberFormat))); #endif }
/// <summary> /// Check if a value is a numeric type /// </summary> /// <param name="value"></param> /// <returns></returns> static public bool IsNumeric(IConvertible value) { if (value == null) { return(false); } switch (value.GetTypeCode()) { case TypeCode.Byte: case TypeCode.Decimal: case TypeCode.Double: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: case TypeCode.SByte: case TypeCode.Single: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: return(true); } return(false); }
public void TestGetTypeCode() { JulianDate date = new JulianDate(100); IConvertible convertible = date as IConvertible; Assert.AreEqual(TypeCode.Object, convertible.GetTypeCode()); }
private ITypeReference GetTypeForConstant() { foreach (ILocation location in this.methodDefinition.Locations) { IILLocation /*?*/ mbLocation = location as IILLocation; if (mbLocation != null) { var doc = mbLocation.Document as MethodBodyDocument; if (doc != null) { ITypeReference result = doc.GetTypeFromToken(this.pdbConstant.token); if (result is Dummy) { //TODO: error continue; } return(result); } } } IPlatformType platformType = this.methodDefinition.Type.PlatformType; IConvertible ic = this.pdbConstant.value as IConvertible; if (ic == null) { return(platformType.SystemObject); } switch (ic.GetTypeCode()) { case TypeCode.Boolean: return(platformType.SystemBoolean); case TypeCode.Byte: return(platformType.SystemUInt8); case TypeCode.Char: return(platformType.SystemChar); case TypeCode.Decimal: return(platformType.SystemDecimal); case TypeCode.Double: return(platformType.SystemFloat64); case TypeCode.Int16: return(platformType.SystemInt16); case TypeCode.Int32: return(platformType.SystemInt32); case TypeCode.Int64: return(platformType.SystemInt64); case TypeCode.SByte: return(platformType.SystemInt8); case TypeCode.Single: return(platformType.SystemFloat64); case TypeCode.String: return(platformType.SystemString); case TypeCode.UInt16: return(platformType.SystemUInt16); case TypeCode.UInt32: return(platformType.SystemUInt32); case TypeCode.UInt64: return(platformType.SystemUInt64); default: return(platformType.SystemObject); } }
public void DateConvert() { DateTime now = DateTime.Now; int days = (int)(now.Date - new DateTime(0L, DateTimeKind.Utc).Date).TotalDays; Date today = now.GetDate(); IConvertible dateTimeConvertible = days; IConvertible dateConvertible = today; Assert.AreEqual(dateTimeConvertible.GetTypeCode(), dateConvertible.GetTypeCode()); TestConversion(dateTimeConvertible.ToBoolean, dateConvertible.ToBoolean); TestConversion(dateTimeConvertible.ToByte, dateConvertible.ToByte); TestConversion(dateTimeConvertible.ToChar, dateConvertible.ToChar); TestConversion(dateTimeConvertible.ToDateTime, dateConvertible.ToDateTime); TestConversion(dateTimeConvertible.ToDecimal, dateConvertible.ToDecimal); TestConversion(dateTimeConvertible.ToDouble, dateConvertible.ToDouble); TestConversion(dateTimeConvertible.ToInt16, dateConvertible.ToInt16); TestConversion(dateTimeConvertible.ToInt32, dateConvertible.ToInt32); TestConversion(dateTimeConvertible.ToInt64, dateConvertible.ToInt64); TestConversion(dateTimeConvertible.ToSByte, dateConvertible.ToSByte); TestConversion(dateTimeConvertible.ToSingle, dateConvertible.ToSingle); TestConversion(dateTimeConvertible.ToString, dateConvertible.ToString); TestConversion(dateTimeConvertible.ToUInt16, dateConvertible.ToUInt16); TestConversion(dateTimeConvertible.ToUInt32, dateConvertible.ToUInt32); TestConversion(dateTimeConvertible.ToUInt64, dateConvertible.ToUInt64); TestConversion(p => dateTimeConvertible.ToType(typeof(double), p), p => dateConvertible.ToType(typeof(double), p)); }
private object TryConvertFilterValue(object filterValue) { object retVal = filterValue; if (filterValue.GetType() != typeof(T)) { IConvertible convertibleValue = filterValue as IConvertible; if (convertibleValue != null) { switch (convertibleValue.GetTypeCode()) { case TypeCode.DateTime: retVal = Convert.ToDateTime(convertibleValue); break; case TypeCode.Int32: retVal = Convert.ToInt32(convertibleValue); break; //TODO: Impl other types } } else { throw new FormatException("invalid object typ or undefinede"); } } return(retVal); }
/// <summary> /// Returns true if the given constant expression contains a finite numeric value. In other words, infinities and NaN are excluded. /// </summary> /// <param name="constExpression"></param> /// <returns></returns> public static bool IsFiniteNumeric(ICompileTimeConstant constExpression) { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.SByte: case System.TypeCode.Int16: case System.TypeCode.Int32: case System.TypeCode.Int64: case System.TypeCode.Byte: case System.TypeCode.UInt16: case System.TypeCode.UInt32: case System.TypeCode.UInt64: return(true); case System.TypeCode.Double: var d = ic.ToDouble(null); return(!(Double.IsNaN(d) || Double.IsInfinity(d))); case System.TypeCode.Single: var s = ic.ToSingle(null); return(!(Single.IsNaN(s) || Single.IsInfinity(s))); } return(false); }
public static bool IsNumericZero(ICompileTimeConstant constExpression) { IConvertible /*?*/ ic = constExpression.Value as IConvertible; if (ic == null) { return(false); } switch (ic.GetTypeCode()) { case System.TypeCode.SByte: return(ic.ToSByte(null) == 0); case System.TypeCode.Int16: return(ic.ToInt16(null) == 0); case System.TypeCode.Int32: return(ic.ToInt32(null) == 0); case System.TypeCode.Int64: return(ic.ToInt64(null) == 0); case System.TypeCode.Byte: return(ic.ToByte(null) == 0); case System.TypeCode.UInt16: return(ic.ToUInt16(null) == 0); case System.TypeCode.UInt32: return(ic.ToUInt32(null) == 0); case System.TypeCode.UInt64: return(ic.ToUInt64(null) == 0); case System.TypeCode.Single: return(ic.ToSingle(null) == 0); case System.TypeCode.Double: return(ic.ToDouble(null) == 0); case System.TypeCode.Decimal: return(ic.ToDecimal(null) == 0); } return(false); }
static IConvertible CheckNumericPromotion(object value) { IConvertible convertible = (IConvertible)value; switch (convertible.GetTypeCode()) { case TypeCode.Byte: return(convertible); case TypeCode.SByte: return(convertible); case TypeCode.Int16: return(convertible); case TypeCode.Int32: return(convertible); case TypeCode.Int64: return(convertible); case TypeCode.UInt16: return(convertible); case TypeCode.UInt32: return(convertible); case TypeCode.UInt64: return(convertible); case TypeCode.Single: return(convertible); case TypeCode.Double: return(convertible); case TypeCode.Boolean: return(convertible); } throw new InvalidCastException(); }
internal static bool IsJsonPrimitive(object value) { if (value == null) { return(true); } IConvertible convertible = value as IConvertible; if (convertible != null) { return(IsJsonPrimitiveTypeCode(convertible.GetTypeCode())); } #if !PocketPC && !NET20 if (value is DateTimeOffset) { return(true); } #endif if (value is byte[]) { return(true); } return(false); }
internal static bool IsJsonPrimitive(object value) { if (value == null) { return(true); } IConvertible convertible = value as IConvertible; if (convertible != null) { return(IsJsonPrimitiveTypeCode(convertible.GetTypeCode())); } if (value is DateTimeOffset) { return(true); } if (value is byte[]) { return(true); } if (value is Uri) { return(true); } if (value is TimeSpan) { return(true); } if (value is Guid) { return(true); } return(false); }
public static bool TryConvert(object value, bool convertFromString, out DateTime dateTimeValue) { dateTimeValue = new DateTime(); try { IConvertible convertible = value as IConvertible; if (convertible != null) { TypeCode typeCode = convertible.GetTypeCode(); if (typeCode == TypeCode.DateTime) { dateTimeValue = convertible.ToDateTime((IFormatProvider)CultureInfo.InvariantCulture); return(true); } if (convertFromString) { if (typeCode == TypeCode.String) { return(DateTime.TryParse((string)value, (IFormatProvider)CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTimeValue)); } } } } catch (FormatException ex) { } catch (InvalidCastException ex) { } catch (OverflowException ex) { } return(false); }
private static bool AreEqual(IConvertible x, IConvertible y, float precission) { var xc = x.GetTypeCode(); var yc = y.GetTypeCode(); if (xc == TypeCode.Decimal || yc == TypeCode.Decimal) { var xf = y.ToDecimal(CULTURE.InvariantCulture); var yf = y.ToDecimal(CULTURE.InvariantCulture); return(Math.Abs((double)(xf - yf)) < precission); } if (xc == TypeCode.Double || yc == TypeCode.Double) { var xf = y.ToDouble(CULTURE.InvariantCulture); var yf = y.ToDouble(CULTURE.InvariantCulture); return(Math.Abs(xf - yf) < precission); } if (xc == TypeCode.Single || yc == TypeCode.Single) { var xf = y.ToSingle(CULTURE.InvariantCulture); var yf = y.ToSingle(CULTURE.InvariantCulture); return(Math.Abs(xf - yf) < precission); } if (xc == yc) { return(Object.Equals(x, y)); } return(false); }
private void AppendInteger(IConvertible value) { switch (value.GetTypeCode()) { case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: this.sb.Append(value.ToInt32(null)); return; case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.UInt32: this.sb.Append(value.ToUInt32(null)); return; case TypeCode.Int64: this.sb.Append(value.ToInt64(null)); return; case TypeCode.UInt64: this.sb.Append(value.ToUInt64(null)); return; default: return; } }
public static bool IsNumericType(object item) { IConvertible convertible = item as IConvertible; if ((object)convertible != null) { switch (convertible.GetTypeCode()) { case TypeCode.Boolean: 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.Single: case TypeCode.Double: case TypeCode.Decimal: return(true); } } return(false); }
// Convert an object into a byte value. public static byte FromObject(Object Value) { #if !ECMA_COMPAT if (Value != null) { IConvertible ic = (Value as IConvertible); if (ic != null) { if (ic.GetTypeCode() != TypeCode.String) { return(ic.ToByte(null)); } else { return(FromString(ic.ToString(null))); } } else { throw new InvalidCastException (String.Format (S._("VB_InvalidCast"), Value.GetType(), "System.Byte")); } } else { return(0); } #else return(checked ((byte)(LongType.FromObject(Value)))); #endif }
//(note: o1 and o2 must both be of type Int32/Int64/Decimal/Double) internal static TypeCode ToSameType(ref IConvertible o1, ref IConvertible o2) { TypeCode tc1 = o1.GetTypeCode(); TypeCode tc2 = o2.GetTypeCode(); if (tc1 == tc2) { return(tc1); } if (tc1 == TypeCode.DBNull || tc2 == TypeCode.DBNull) { return(TypeCode.DBNull); } // is it ok to make such assumptions about the order of an enum? if (tc1 < tc2) { o1 = (IConvertible)ConvertExtensions.ChangeType(o1, tc2); return(tc2); } else { o2 = (IConvertible)ConvertExtensions.ChangeType(o2, tc1); return(tc1); } }
/// <summary> 数字 类型对象转换Json字符串写入Buffer /// </summary> /// <param name="number">数字对象</param> protected virtual void AppendNumber(IConvertible number) { switch (number.GetTypeCode()) { case TypeCode.Decimal: case TypeCode.Double: case TypeCode.Single: Buffer.Append(number.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)); break; case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: case TypeCode.SByte: Buffer.Append(number.ToInt64(System.Globalization.NumberFormatInfo.InvariantInfo)); break; case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: Buffer.Append(number.ToUInt64(System.Globalization.NumberFormatInfo.InvariantInfo)); break; default: break; } }
public static bool TryConvert(object value, bool convertFromString, out long intValue) { intValue = 0L; try { IConvertible convertible = value as IConvertible; if (convertible != null) { TypeCode typeCode = convertible.GetTypeCode(); if (ValueHelper.IsNumeric(typeCode)) { intValue = convertible.ToInt64((IFormatProvider)CultureInfo.InvariantCulture); return(true); } if (convertFromString) { if (typeCode == TypeCode.String) { return(long.TryParse((string)value, NumberStyles.Any, (IFormatProvider)CultureInfo.InvariantCulture, out intValue)); } } } } catch (FormatException ex) { } catch (InvalidCastException ex) { } catch (OverflowException ex) { } return(false); }
/// <summary>Determines if given item is numeric.</summary> /// <param name="item">Object to evaluate.</param> /// <returns>Result of evaluation as a <see cref="bool"/>.</returns> public static bool IsNumeric(object item) { IConvertible convertible = item as IConvertible; if ((object)convertible != null) { switch (convertible.GetTypeCode()) { case TypeCode.Boolean: 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.Single: case TypeCode.Double: case TypeCode.Decimal: return(true); case TypeCode.Char: case TypeCode.String: double result; return(double.TryParse(convertible.ToString(null), out result)); } } return(false); }
//extends to Int32/Int64/Decimal/Double internal static IConvertible Unify (IConvertible o) { switch (o.GetTypeCode()) { case TypeCode.Char: case TypeCode.SByte: case TypeCode.Byte: case TypeCode.Int16: case TypeCode.UInt16: return (IConvertible)Convert.ChangeType (o, TypeCode.Int32); case TypeCode.UInt32: return (IConvertible)Convert.ChangeType (o, TypeCode.Int64); case TypeCode.UInt64: return (IConvertible)Convert.ChangeType (o, TypeCode.Decimal); case TypeCode.Single: return (IConvertible)Convert.ChangeType (o, TypeCode.Double); default: return o; } }
//(note: o1 and o2 must both be of type Int32/Int64/Decimal/Double) internal static TypeCode ToSameType (ref IConvertible o1, ref IConvertible o2) { TypeCode tc1 = o1.GetTypeCode(); TypeCode tc2 = o2.GetTypeCode(); if (tc1 == tc2) return tc1; if (tc1 == TypeCode.DBNull || tc2 == TypeCode.DBNull) return TypeCode.DBNull; // is it ok to make such assumptions about the order of an enum? if (tc1 < tc2) { o1 = (IConvertible)Convert.ChangeType (o1, tc2); return tc2; } else { o2 = (IConvertible)Convert.ChangeType (o2, tc1); return tc1; } }
internal static IConvertible Negative (IConvertible o) { switch (o.GetTypeCode()) { case TypeCode.Int32: return -((int)o); case TypeCode.Int64: return -((long)o); case TypeCode.Double: return -((double)o); case TypeCode.Decimal: return -((decimal)o); default: return DBNull.Value; } }
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); }
//! 设置绘图命令选项(bool/int/float类型的值) public bool SetOption(string key, IConvertible value) { if (key == null || value == null) { return false; } if (value.GetTypeCode() == TypeCode.String) { string str = Convert.ToString(value).ToLower(); int intValue; double doubleValue; if (str.CompareTo("true") == 0) return SetOption(key, true); if (str.CompareTo("false") == 0) return SetOption(key, false); if (int.TryParse(str, out intValue)) return SetOption(key, intValue); if (double.TryParse(str, out doubleValue)) return SetOption(key, doubleValue); CoreView.setOptionString(key, Convert.ToString(value)); return true; } if (key == "contextActionEnabled") { View.ContextActionEnabled = Convert.ToBoolean(value); } else if (key == "zoomEnabled") { ZoomEnabled = Convert.ToBoolean(value); } else switch (value.GetTypeCode()) { case TypeCode.Boolean: CoreView.setOptionBool(key, Convert.ToBoolean(value)); break; case TypeCode.Int32: case TypeCode.UInt32: CoreView.setOptionInt(key, Convert.ToInt32(value)); break; case TypeCode.Single: case TypeCode.Double: CoreView.setOptionFloat(key, Convert.ToSingle(value)); break; default: Debug.Assert(false, key); return false; } return true; }
protected TypeCode GetValueType(IConvertible left, IConvertible right) { const int INTEGRAL_8 = 0x0001; const int UINTEGRAL_8 = 0x0002; const int INTEGRAL_16 = 0x0004; const int UINTEGRAL_16 = 0x0008; const int INTEGRAL_32 = 0x0010; const int UINTEGRAL_32 = 0x0020; const int INTEGRAL_64 = 0x0040; const int UINTEGRAL_64 = 0x0080; const int FLOATING_POINT = 0x0100; const int DECIMAL = 0x0200; const int STRING = 0x0400; int leftResult = 0; switch (left.GetTypeCode()) { case TypeCode.Boolean: leftResult = INTEGRAL_8; break; case TypeCode.Char: leftResult = UINTEGRAL_16; break; case TypeCode.SByte: leftResult = INTEGRAL_8; break; case TypeCode.Byte: leftResult = UINTEGRAL_8; break; case TypeCode.Int16: leftResult = INTEGRAL_16; break; case TypeCode.UInt16: leftResult = UINTEGRAL_16; break; case TypeCode.Int32: leftResult = INTEGRAL_32; break; case TypeCode.UInt32: leftResult = UINTEGRAL_32; break; case TypeCode.Int64: leftResult = INTEGRAL_64; break; case TypeCode.UInt64: leftResult = UINTEGRAL_64; break; case TypeCode.Single: case TypeCode.Double: leftResult = FLOATING_POINT; break; case TypeCode.Decimal: leftResult = DECIMAL; break; default: leftResult = STRING; break; } int rightResult = 0; switch (right.GetTypeCode()) { case TypeCode.Boolean: rightResult = INTEGRAL_8; break; case TypeCode.Char: rightResult = UINTEGRAL_16; break; case TypeCode.SByte: rightResult = INTEGRAL_8; break; case TypeCode.Byte: rightResult = UINTEGRAL_8; break; case TypeCode.Int16: rightResult = INTEGRAL_16; break; case TypeCode.UInt16: rightResult = UINTEGRAL_16; break; case TypeCode.Int32: rightResult = INTEGRAL_32; break; case TypeCode.UInt32: rightResult = UINTEGRAL_32; break; case TypeCode.Int64: rightResult = INTEGRAL_64; break; case TypeCode.UInt64: rightResult = UINTEGRAL_64; break; case TypeCode.Single: case TypeCode.Double: rightResult = FLOATING_POINT; break; case TypeCode.Decimal: rightResult = DECIMAL; break; default: rightResult = STRING; break; } int result = leftResult | rightResult; if ((int)(result & STRING) == STRING) { return TypeCode.String; } if ((int)(result & DECIMAL) == DECIMAL) { return TypeCode.Decimal; } if ((int)(result & FLOATING_POINT) == FLOATING_POINT) { return TypeCode.Double; } if ((int)(result & UINTEGRAL_64) == UINTEGRAL_64) { return TypeCode.UInt64; } if ((int)(result & INTEGRAL_64) == INTEGRAL_64) { return TypeCode.Int64; } if ((int)(result & UINTEGRAL_32) == UINTEGRAL_32) { return TypeCode.UInt32; } if ((int)(result & INTEGRAL_32) == INTEGRAL_32) { return TypeCode.Int32; } if ((int)(result & UINTEGRAL_16) == UINTEGRAL_16) { return TypeCode.UInt16; } if ((int)(result & INTEGRAL_16) == INTEGRAL_16) { return TypeCode.Int16; } if ((int)(result & UINTEGRAL_8) == UINTEGRAL_8) { return TypeCode.Byte; } if ((int)(result & INTEGRAL_8) == INTEGRAL_8) { return TypeCode.SByte; } return TypeCode.String; }
/// <summary> /// Negates a <see cref="T:IConvertible"/> value by round tripping through /// the System.Decimal type. /// </summary> /// <param name="value">The value to negate.</param> /// <param name="formatProvider">The culture information.</param> /// <returns>The negated value as the original input type.</returns> private static object Negate(IConvertible value, IFormatProvider formatProvider) { TypeCode inputType = value.GetTypeCode(); decimal input = value.ToDecimal(formatProvider); decimal output = Decimal.Negate(input); return System.Convert.ChangeType(output, inputType, formatProvider); }
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; } }
public static void WriteIConvertible(IConvertible input) { Console.WriteLine("{0}: {1}", input.GetTypeCode(), input.ToString(null).ToLower()); }
public static void GetTypeCode_Primitives(TypeCode expected, IConvertible convertible) { Assert.Equal(expected, convertible.GetTypeCode()); }
public static IConvertible CheckNumericPromotion(IConvertible convertible) { if (IsPromotableNumeric(convertible.GetTypeCode())) return convertible; throw new InvalidCastException(); }
internal static TypeCode GetTypeCode(Object ob, IConvertible ic){ if (ob == null) return TypeCode.Empty; if (ic == null) return TypeCode.Object; return ic.GetTypeCode(); }
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; } }
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; }
/* public static object [] VarArgs (object [] args, int offset, int n) { throw new NotImplementedException (); } */ internal static TypeCode GetTypeCode (object obj, IConvertible convertible) { if (obj == null) return TypeCode.Empty; else if (convertible == null) return TypeCode.Object; else return convertible.GetTypeCode (); }
// // Utility methods // internal static TypeCode GetTypeCode (object obj, IConvertible ic) { if (obj == null) return TypeCode.Empty; else if (ic == null) return TypeCode.Object; else return ic.GetTypeCode (); }
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; } }
/// <summary> 数字 类型对象转换Json字符串写入Buffer /// </summary> /// <param name="number">数字对象</param> protected virtual void AppendNumber(IConvertible number) { switch (number.GetTypeCode()) { case TypeCode.Decimal: case TypeCode.Double: case TypeCode.Single: Buffer.Append(number.ToString(System.Globalization.NumberFormatInfo.InvariantInfo)); break; case TypeCode.Int16: Buffer.Append(number.ToInt16(System.Globalization.NumberFormatInfo.InvariantInfo)); break; case TypeCode.Int32: case TypeCode.Int64: Buffer.Append(number.ToInt64(System.Globalization.NumberFormatInfo.InvariantInfo)); break; case TypeCode.SByte: Buffer.Append(number.ToSByte(System.Globalization.NumberFormatInfo.InvariantInfo)); break; case TypeCode.Byte: Buffer.Append(number.ToByte(System.Globalization.NumberFormatInfo.InvariantInfo)); break; case TypeCode.UInt16: Buffer.Append(number.ToUInt16(System.Globalization.NumberFormatInfo.InvariantInfo)); break; case TypeCode.UInt32: case TypeCode.UInt64: Buffer.Append(number.ToUInt64(System.Globalization.NumberFormatInfo.InvariantInfo)); break; default: break; } }
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; }