public sbyte ToSByte(object value) { if (value == null) { throw new ArgumentNullException(nameof(value)); } return(SConvert.ToSByte(value, CultureInfo.InvariantCulture)); }
sbyte IConvertible.ToSByte(IFormatProvider?provider) { return(Convert.ToSByte(this, provider)); }
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); }
public sbyte ToSByte(object value) => SystemConvert.ToSByte(value);
sbyte IConvertible.ToSByte(IFormatProvider provider) { return(Convert.ToSByte(m_value)); }
public void Serialize(GenericWriter writer) { writer.WriteFlag(Flag); switch (Flag) { case DataType.Null: break; case DataType.Bool: writer.Write(Convert.ToBoolean(Value)); break; case DataType.Char: writer.Write(Convert.ToChar(Value)); break; case DataType.Byte: writer.Write(Convert.ToByte(Value)); break; case DataType.SByte: writer.Write(Convert.ToSByte(Value)); break; case DataType.Short: writer.Write(Convert.ToInt16(Value)); break; case DataType.UShort: writer.Write(Convert.ToUInt16(Value)); break; case DataType.Int: writer.Write(Convert.ToInt32(Value)); break; case DataType.UInt: writer.Write(Convert.ToUInt32(Value)); break; case DataType.Long: writer.Write(Convert.ToInt64(Value)); break; case DataType.ULong: writer.Write(Convert.ToUInt64(Value)); break; case DataType.Float: writer.Write(Convert.ToSingle(Value)); break; case DataType.Decimal: writer.Write(Convert.ToDecimal(Value)); break; case DataType.Double: writer.Write(Convert.ToDouble(Value)); break; case DataType.String: writer.Write(Convert.ToString(Value)); break; case DataType.DateTime: writer.Write(Convert.ToDateTime(Value)); break; case DataType.TimeSpan: writer.Write((TimeSpan)Value); break; } }
public static sbyte Parse(string s) { // check for null string is carried out in native code return(Convert.ToSByte(s)); }
// 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); }
/// <internalonly/> sbyte IConvertible.ToSByte(IFormatProvider provider) { return(Convert.ToSByte(GetValue(), CultureInfo.CurrentCulture)); }
sbyte IConvertible.ToSByte (IFormatProvider provider) { return Convert.ToSByte (Value, provider); }
sbyte IConvertible.ToSByte(IFormatProvider provider) { return(Convert.ToSByte((float)this)); }
internal static Object CheckArgument(Object srcObject, EETypePtr dstEEType, CheckArgumentSemantics semantics) { if (srcObject == null) { // null -> default(T) if (dstEEType.IsValueType && !RuntimeImports.RhIsNullable(dstEEType)) { return(Runtime.RuntimeImports.RhNewObject(dstEEType)); } else { return(null); } } else { EETypePtr srcEEType = srcObject.EETypePtr; if (RuntimeImports.AreTypesAssignable(srcEEType, dstEEType)) { return(srcObject); } if (RuntimeImports.RhIsInterface(dstEEType)) { ICastable castable = srcObject as ICastable; Exception castError; if (castable != null && castable.IsInstanceOfInterface(new RuntimeTypeHandle(dstEEType), out castError)) { return(srcObject); } } if (!((srcEEType.IsEnum || srcEEType.IsPrimitive) && (dstEEType.IsEnum || dstEEType.IsPrimitive))) { throw CreateChangeTypeException(srcEEType, dstEEType, semantics); } RuntimeImports.RhCorElementType dstCorElementType = dstEEType.CorElementType; if (!srcEEType.CorElementTypeInfo.CanWidenTo(dstCorElementType)) { throw CreateChangeTypeArgumentException(srcEEType, dstEEType); } Object dstObject; switch (dstCorElementType) { case RuntimeImports.RhCorElementType.ELEMENT_TYPE_BOOLEAN: dstObject = Convert.ToBoolean(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_CHAR: dstObject = Convert.ToChar(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I1: dstObject = Convert.ToSByte(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I2: dstObject = Convert.ToInt16(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I4: dstObject = Convert.ToInt32(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_I8: dstObject = Convert.ToInt64(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U1: dstObject = Convert.ToByte(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U2: dstObject = Convert.ToUInt16(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U4: dstObject = Convert.ToUInt32(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_U8: dstObject = Convert.ToUInt64(srcObject); break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_R4: if (srcEEType.CorElementType == RuntimeImports.RhCorElementType.ELEMENT_TYPE_CHAR) { dstObject = (float)(char)srcObject; } else { dstObject = Convert.ToSingle(srcObject); } break; case RuntimeImports.RhCorElementType.ELEMENT_TYPE_R8: if (srcEEType.CorElementType == RuntimeImports.RhCorElementType.ELEMENT_TYPE_CHAR) { dstObject = (double)(char)srcObject; } else { dstObject = Convert.ToDouble(srcObject); } break; default: Debug.Assert(false, "Unexpected CorElementType: " + dstCorElementType + ": Not a valid widening target."); throw CreateChangeTypeException(srcEEType, dstEEType, semantics); } if (dstEEType.IsEnum) { Type dstType = ReflectionCoreNonPortable.GetRuntimeTypeForEEType(dstEEType); dstObject = Enum.ToObject(dstType, dstObject); } Debug.Assert(dstObject.EETypePtr == dstEEType); return(dstObject); } }