public ushort ToUInt16(object value) { if (value == null) { throw new ArgumentNullException(nameof(value)); } return(SConvert.ToUInt16(value, CultureInfo.InvariantCulture)); }
private static void RegisterDecimalConversions( ITypeConverterRegistry registry) { registry.Register <decimal, short>(from => SysConv.ToInt16(from)); registry.Register <decimal, int>(from => SysConv.ToInt32(from)); registry.Register <decimal, long>(from => SysConv.ToInt64(from)); registry.Register <decimal, ushort>(from => SysConv.ToUInt16(from)); registry.Register <decimal, uint>(from => SysConv.ToUInt32(from)); registry.Register <decimal, ulong>(from => SysConv.ToUInt64(from)); registry.Register <decimal, float>(from => SysConv.ToSingle(from)); registry.Register <decimal, double>(from => SysConv.ToDouble(from)); registry.Register <decimal, string>(from => from.ToString("E", CultureInfo.InvariantCulture)); }
private static void RegisterDoubleConversions( ITypeConverterRegistry registry) { registry.Register <double, byte>(from => SysConv.ToByte(from)); registry.Register <double, short>(from => SysConv.ToInt16(from)); registry.Register <double, int>(from => SysConv.ToInt32(from)); registry.Register <double, long>(from => SysConv.ToInt64(from)); registry.Register <double, ushort>(from => SysConv.ToUInt16(from)); registry.Register <double, uint>(from => SysConv.ToUInt32(from)); registry.Register <double, ulong>(from => SysConv.ToUInt64(from)); registry.Register <double, decimal>(from => SysConv.ToDecimal(from)); registry.Register <double, float>(from => SysConv.ToSingle(from)); registry.Register <double, string>(from => from.ToString(CultureInfo.InvariantCulture)); }
private static void RegisterSingleConversions( DefaultTypeConverter registry) { registry.Register <float, byte>(from => SysConv.ToByte(from)); registry.Register <float, short>(from => SysConv.ToInt16(from)); registry.Register <float, int>(from => SysConv.ToInt32(from)); registry.Register <float, long>(from => SysConv.ToInt64(from)); registry.Register <float, ushort>(from => SysConv.ToUInt16(from)); registry.Register <float, uint>(from => SysConv.ToUInt32(from)); registry.Register <float, ulong>(from => SysConv.ToUInt64(from)); registry.Register <float, decimal>(from => SysConv.ToDecimal(from)); registry.Register <float, double>(from => SysConv.ToDouble(from)); registry.Register <float, string>(from => from.ToString(CultureInfo.InvariantCulture)); }
public ushort ToUInt16(object value) => SystemConvert.ToUInt16(value);
protected static internal object ConvertValue(Type type, NSJSValue value) { if (type == null) { throw new ArgumentNullException("type"); } object o = FetchValue(type, value); if (type == typeof(int)) { o = (o == null ? 0 : Converter.ToInt32(o)); } else if (type == typeof(uint)) { o = (o == null ? 0u : Converter.ToUInt32(o)); } else if (type == typeof(short)) { o = (o == null ? (short)0 : Converter.ToInt16(o)); } else if (type == typeof(ushort)) { o = (o == null ? (ushort)0 : Converter.ToUInt16(o)); } else if (type == typeof(sbyte)) { o = (o == null ? (sbyte)0 : Converter.ToSByte(o)); } else if (type == typeof(byte)) { o = (o == null ? (byte)0 : Converter.ToByte(o)); } else if (type == typeof(long)) { o = (o == null ? 0L : Converter.ToInt64(o)); } else if (type == typeof(ulong)) { o = (o == null ? 0ul : Converter.ToUInt64(o)); } else if (type == typeof(float)) { o = (o == null ? 0f : Converter.ToSingle(o)); } else if (type == typeof(double)) { o = (o == null ? 0d : Converter.ToDouble(o)); } else if (type == typeof(decimal)) { o = (o == null ? 0m : Converter.ToDecimal(o)); } else if (type == typeof(char)) { o = (o == null ? '\0' : Converter.ToChar(o)); } else if (type == typeof(DateTime)) { long ticks = 0; if (o is long) { ticks = (long)o; } else if (o != null) { ticks = Converter.ToInt64(o); } o = NSJSDateTime.LocalDateToDateTime(ticks); } else if (type == typeof(string)) { if (o == null) { o = null; } else if (!(o is string)) { o = o.ToString(); } } else if (typeof(NSJSValue).IsAssignableFrom(type)) { return(type.IsInstanceOfType(value) ? value : null); } return(o); }
/// <internalonly/> ushort IConvertible.ToUInt16(IFormatProvider provider) { return(Convert.ToUInt16(m_value)); }
// Special coersion rules for primitives, enums and pointer. private static Exception ConvertOrWidenPrimitivesEnumsAndPointersIfPossible(object srcObject, EETypePtr srcEEType, EETypePtr dstEEType, CheckArgumentSemantics semantics, out object?dstObject) { if (semantics == CheckArgumentSemantics.SetFieldDirect && (srcEEType.IsEnum || dstEEType.IsEnum)) { dstObject = null; return(CreateChangeTypeException(srcEEType, dstEEType, semantics)); } if (dstEEType.IsPointer) { Exception exception = ConvertPointerIfPossible(srcObject, srcEEType, dstEEType, semantics, out IntPtr dstIntPtr); if (exception != null) { dstObject = null; return(exception); } dstObject = dstIntPtr; return(null); } if (!(srcEEType.IsPrimitive && dstEEType.IsPrimitive)) { dstObject = null; return(CreateChangeTypeException(srcEEType, dstEEType, semantics)); } CorElementType dstCorElementType = dstEEType.CorElementType; if (!srcEEType.CorElementTypeInfo.CanWidenTo(dstCorElementType)) { dstObject = null; return(CreateChangeTypeArgumentException(srcEEType, dstEEType)); } switch (dstCorElementType) { case CorElementType.ELEMENT_TYPE_BOOLEAN: bool boolValue = Convert.ToBoolean(srcObject); dstObject = dstEEType.IsEnum ? Enum.ToObject(dstEEType, boolValue ? 1 : 0) : boolValue; break; case CorElementType.ELEMENT_TYPE_CHAR: char charValue = Convert.ToChar(srcObject); dstObject = dstEEType.IsEnum ? Enum.ToObject(dstEEType, charValue) : charValue; break; case CorElementType.ELEMENT_TYPE_I1: sbyte sbyteValue = Convert.ToSByte(srcObject); dstObject = dstEEType.IsEnum ? Enum.ToObject(dstEEType, sbyteValue) : sbyteValue; break; case CorElementType.ELEMENT_TYPE_I2: short shortValue = Convert.ToInt16(srcObject); dstObject = dstEEType.IsEnum ? Enum.ToObject(dstEEType, shortValue) : shortValue; break; case CorElementType.ELEMENT_TYPE_I4: int intValue = Convert.ToInt32(srcObject); dstObject = dstEEType.IsEnum ? Enum.ToObject(dstEEType, intValue) : intValue; break; case CorElementType.ELEMENT_TYPE_I8: long longValue = Convert.ToInt64(srcObject); dstObject = dstEEType.IsEnum ? Enum.ToObject(dstEEType, longValue) : longValue; break; case CorElementType.ELEMENT_TYPE_U1: byte byteValue = Convert.ToByte(srcObject); dstObject = dstEEType.IsEnum ? Enum.ToObject(dstEEType, byteValue) : byteValue; break; case CorElementType.ELEMENT_TYPE_U2: ushort ushortValue = Convert.ToUInt16(srcObject); dstObject = dstEEType.IsEnum ? Enum.ToObject(dstEEType, ushortValue) : ushortValue; break; case CorElementType.ELEMENT_TYPE_U4: uint uintValue = Convert.ToUInt32(srcObject); dstObject = dstEEType.IsEnum ? Enum.ToObject(dstEEType, uintValue) : uintValue; break; case CorElementType.ELEMENT_TYPE_U8: ulong ulongValue = Convert.ToUInt64(srcObject); dstObject = dstEEType.IsEnum ? Enum.ToObject(dstEEType, (long)ulongValue) : ulongValue; break; case CorElementType.ELEMENT_TYPE_R4: if (srcEEType.CorElementType == CorElementType.ELEMENT_TYPE_CHAR) { dstObject = (float)(char)srcObject; } else { dstObject = Convert.ToSingle(srcObject); } break; case CorElementType.ELEMENT_TYPE_R8: if (srcEEType.CorElementType == CorElementType.ELEMENT_TYPE_CHAR) { dstObject = (double)(char)srcObject; } else { dstObject = Convert.ToDouble(srcObject); } break; default: Debug.Fail("Unexpected CorElementType: " + dstCorElementType + ": Not a valid widening target."); dstObject = null; return(CreateChangeTypeException(srcEEType, dstEEType, semantics)); } Debug.Assert(dstObject.GetEETypePtr() == dstEEType); return(null); }
ushort IConvertible.ToUInt16 (IFormatProvider provider) { return Convert.ToUInt16 (Value, provider); }
ushort IConvertible.ToUInt16(IFormatProvider provider) { return(Convert.ToUInt16((float)this)); }
/// <internalonly/> ushort IConvertible.ToUInt16(IFormatProvider provider) { return(Convert.ToUInt16(this, provider)); }